I'm trying to set up the below code to ask the user to enter two integers (num_sides & num_rolls). If the user doesn't enter an integer for either input, then the code should print the statement "Enter an integer".
I find this code only tests the num_sides but doesn't test if num_rolls is an integer. What's going on here?
Thanks in advance for your help!
def rolldice():
while True:
while True:
num_sides = input("Enter number of sides for die: ")
num_rolls = input("Enter number of rolls for die: ")
try:
if int(num_sides) != num_sides or int(num_rolls) != num_rolls:
break
break
except(ValueError):
print("Enter an integer")
True
num_sidesis being cast to anintand being compared to its original form (a string), which will never beTrue.breaks both inside and outside yourif? And what did you expectTrueon a line by itself to do for you?int. Are you also trying to check whether the numbers are integers, or is that not important? I mean, if someone puts in 3.3, do you want it to throw an exception (test whether the number is an integer) or just cast to 3 (which is what the current answers do)?inputreturns a string andint('3.3')would return ValueError. (If OP isn't using Py3, I strongly advise to switch toraw_inputinstead ofinput) The behavior you describe happens when applyingintto afloatthough, but not its literal.