I am currently learning python and i cant seen to figure out how to get a if - then statement for a dice to work (randomint).
I tried
if roll_dice < 10:
print ("dang, you got a low roll, try again")
else:
roll_dice > 10
print ("nice! you got a high roll")
roll_dice = 10
print ("10!")
It says "ValueError: don't know how to compare 'function' and 'int' "
CodePudding user response:
Maybe you want it to work this way:
if roll_dice < 10:
print ("dang, you got a low roll, try again")
elif roll_dice > 10:
print ("nice! you got a high roll")
else:
print ("10!")
or
if roll_dice < 10:
print ("dang, you got a low roll, try again")
elif roll_dice > 10:
print ("nice! you got a high roll")
elif roll_dice == 10:
print ("10!")
CodePudding user response:
Sounds like roll_dice
is a function, so you need to call it to get an answer, so roll_dice < 10
is meaningless. It would be like asking print < 10
or abs < 10
.
Try this:
roll = roll_dice()
if roll < 10:
print("dang, you got a low roll, try again")
elif roll > 10:
print ("nice! you got a high roll")
else:
assert(roll == 10)
print("10!")
CodePudding user response:
Here's another version of the code in one line - using the ternary operator. This sort of coding is not advised, but illustrates another way of coding this.
print ("dang, you got a low roll, try again" if roll_dice < 10 else "nice! you got a high roll" if roll_dice > 10 else "10!" )
The real issue with the original question is that the syntax of the if
statement is incorrect.
CodePudding user response:
I don't know how you are feeding your variable (roll_dice). But if it's through a keyboard input you can do this.
Remember also to compare a roll_dice >10.
You have to do this int(roll_dice).
It changes it from string to integer.