how can i add two randome numbers

This is the place for queries that don't fit in any of the other categories.

how can i add two randome numbers

Postby drewster1123 » Thu Jun 12, 2014 2:06 pm

ive tried making dice and ive succeeded except for adding the two random numbers(the two rolled dice) to add up the sum of the dice

here is what i have so far:
Code: Select all
import random

min=1
max=12

roll_again = 'yes'

while roll_again == 'yes' or roll_again == 'y':
    print ('shake, shake, shake!')
    print ('you got a:')
    print random.randint(min,max)
    print random.randint(min,max)

    print ('the sum of your two numbers are below:')

    roll_again = raw_input ('roll the dice again')
drewster1123
 
Posts: 7
Joined: Wed Jun 11, 2014 10:19 pm

Re: how can i add two randome numbers

Postby Crimson King » Thu Jun 12, 2014 2:31 pm

Hello drewster1123, welcome to the forum. Be sure to read user guideline.

Instead of using
Code: Select all
 print random.randint(min,max)
assign the result of throwing the die to a variable:

Code: Select all
die1 = random.randint(min,max)
die2 = random.randint(min,max)


Then you can print them separately, print the sum, etc.

Good luck!
User avatar
Crimson King
 
Posts: 118
Joined: Fri Mar 08, 2013 2:42 pm
Location: Buenos Aires, Argentina

Re: how can i add two randome numbers

Postby drewster1123 » Thu Jun 12, 2014 8:12 pm

thanks Crimson King but how would you print out the sum of the two dice. i assigned dice1 and dice2 to a random number i just need it to print out the sum of both added together
drewster1123
 
Posts: 7
Joined: Wed Jun 11, 2014 10:19 pm

Re: how can i add two randome numbers

Postby drewster1123 » Thu Jun 12, 2014 8:33 pm

k i figured it out but when i repeat it (because in my code you have a choice to roll again)it sais the same numbers over and over. so it picks a random number the first time but if you repeat it it sais the same thing

this is what i have so far

Code: Select all
import random

min=1
max=12

roll_again = 'yes'
die1=random.randint(min,max)
die2=random.randint(min,max)

while roll_again == 'yes' or roll_again == 'y':
    print ('shake, shake, shake!')
    print ('you got a:')
    print die1
    print die2

    print ('the sum of your two numbers are below:')
    print die1 + die2
    roll_again = raw_input ('roll the dice again')
   
drewster1123
 
Posts: 7
Joined: Wed Jun 11, 2014 10:19 pm

Re: how can i add two randome numbers

Postby Crimson King » Thu Jun 12, 2014 8:41 pm

drewster1123 wrote:k i figured it out but when i repeat it (because in my code you have a choice to roll again)it sais the same numbers over and over. so it picks a random number the first time but if you repeat it it sais the same thing

this is what i have so far

Code: Select all
import random

min=1
max=12

roll_again = 'yes'
die1=random.randint(min,max)
die2=random.randint(min,max)

while roll_again == 'yes' or roll_again == 'y':
    print ('shake, shake, shake!')
    print ('you got a:')
    print die1
    print die2

    print ('the sum of your two numbers are below:')
    print die1 + die2
    roll_again = raw_input ('roll the dice again')
   


You get that output because the throw of the dice is outside the while loop. Put the
Code: Select all
die1=random.randint(min,max)
die2=random.randint(min,max)
after the 'you got a:' print and it should be fine.
User avatar
Crimson King
 
Posts: 118
Joined: Fri Mar 08, 2013 2:42 pm
Location: Buenos Aires, Argentina

Re: how can i add two randome numbers

Postby drewster1123 » Thu Jun 12, 2014 10:06 pm

ok thank you
drewster1123
 
Posts: 7
Joined: Wed Jun 11, 2014 10:19 pm


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 6 guests