I'm trying to make the function dice randomly roll from 1-6. But when I try to add the different numbers from the function, it says that one and two are undefined. I'm not sure to how solve this, and I'm pretty new at coding.
import random
def dice(name):
name = random.randint(1,6)
print name
dice('one')
dice('two')
dicesum = float(one) + float(two)
message = raw_input('guess the number. ')
if dicesum == message:
print "You guess right! You win!"
if dicesum != message:
print "You guess wrong! You lose!"
You probably want one
and two
to represent the values returned by the dice roll:
import random
def roll_dice():
value = random.randint(1, 6)
print(value)
return value
roll_one = roll_dice()
roll_two = roll_dice()
dice_sum = roll_one + roll_two
guess = int(input('guess the number. ')) # cast to int so it can be compared to dice_sum
if dice_sum == guess:
print("You guess right! You win!")
else:
print("You guess wrong! You lose!")
I changed the variable names and function names to what I think is a better representation of what they are and do.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments