Issue with Python While loop
Issue with Python While loop
Okay so I basically want my code to work by asking the user for input on ROLL variable, if it's a number that is specified as a die (6,8,10,12,20) then that all works, it picks a random number and outputs it, easy. However I cannot make the loop quit, so it says while ROLL is not equal to 'quit', do the things. I'll post the code below, but idk how to make it work so that when I enter 'quit' it closes, right now when I do it it outputs the 'else' statement.
#!/usr/bin/env Python3
ROLL = ''
while ROLL != 'quit':
import random
ROLL=input("What sided Die would you like to roll(You may enter quit to close)? ")
if ROLL == 6:
print(random.randint(1, 6))
elif ROLL == 8:
print(random.randint(1, 8))
elif ROLL == 10:
print(random.randint(1,10))
elif ROLL == 12:
print(random.randint(1, 12))
elif ROLL == 20:
print(random.randint(1, 20))
else:
print("That's not an option please choose a die...")
print("Thank you")
I'm using 3 and to be honest I have no clue what you mean by that. I figure when I enter 'quit' the program would say "Okay, so ROLL is now quit, so I should kill the loop"
– JW92
Sep 16 '18 at 20:06
I should prolly remark that I'm just now learning to program in a real language. I'm teaching myself Python and Ruby. I taught myself how to use Linux and write simple stuff in Bash, but idk what your comment is supposed to tell me. Sorry I'm so newb lol
– JW92
Sep 16 '18 at 20:07
3 Answers
3
I have tried to create a python 2 / 3 answer.
python 2 input
interprets your input, converting to integers if possible for instance. So you cannot have an integer AND a string. When entering quit
, since quit
isn't known by the interpreter, you get a NameError
input
quit
quit
NameError
The only way to make it work would be to type "quit"
. But that'd be python 2 only still... Let's try to make it portable now.
"quit"
python 3 fails when comparing strings to numbers, because input
returns strings.
input
You'll have a hard time creating a code which works for both versions. I'd suggest this:
import random
# detect/create a portable raw_input for python 2/3
try:
raw_input
except NameError:
raw_input = input
while True:
ROLL=raw_input("What sided Die would you like to roll(You may enter quit to close)? ")
if ROLL.isdigit():
ROLL = int(ROLL)
if ROLL in [6,8,10,12,20]:
print(random.randint(1, ROLL))
elif ROLL == "quit":
break
else:
print("That's not an option please choose a die...")
The first try
/except
block is there for python 2/3 compatibility. If raw_input
exists, use it, else define it as input
for python 3. From now on, raw_input
is used, and returns strings not integers.
try
except
raw_input
input
raw_input
Now we have to add this: if ROLL.isdigit()
tests if the string could be converted as an integer. If it's possible, it converts it.
if ROLL.isdigit()
Now we test if the reply is contained in the choice list, and if it is, we use ROLL
for our random (avoids the ton of elif
statements).
ROLL
elif
The loop has been turned to inconditional too, no need to initialize ROLL
at start. Just break
if quit
is entered.
ROLL
break
quit
Okay that makes more sense, thank you so much. I'm really loving the idea of designing my own programs and simple games, but I've found myself unduly angry a few too many times, I'm gonna check this out in a moment; I can have my computer at work but Football has us busy. I'll come back and let you know if it worked out.
– JW92
Sep 16 '18 at 20:11
Also thanks for showing me that shorthand array you did, instead of a bunch if/elif, the if ROLL in [array] is much more clean, I really appreciate that
– JW92
Sep 16 '18 at 20:12
THANKS! That worked out perfectly, I'll take note of some of the changes you made.
– JW92
Sep 16 '18 at 20:15
There's an easy fix. But first, the code, as posted above, does not work. Since ROLL is a string, the numbers in the if/elif statements need to be strings, too.
The solution (one easy solution) is to have an extra if statement for the "quit" case and then finish the while loop early using "continue", like so:
import random
ROLL = ''
while ROLL != 'quit':
ROLL=input("What sided Die would you like to roll(You may enter quit to close)? ")
if ROLL == 'quit':
continue
elif ROLL == '6':
print(random.randint(1, 6))
elif ROLL == '8':
print(random.randint(1, 8))
elif ROLL == '10':
print(random.randint(1,10))
elif ROLL == '12':
print(random.randint(1, 12))
elif ROLL == '20':
print(random.randint(1, 20))
else:
print("That's not an option please choose a die...")
print("Thank you")
The "continue" will make the while loop start over, and thereby checking if ROLL is "quit". Since it is, it will terminate the loop and say "Thank you".
The main error in your code is that input
returns a string and you are comparing it to integers. By example '6'
is not equal to 6
.
input
'6'
6
Although, you could simplify your code greatly by comparing the input to a tuple of allowed value. This will be both shorter and more extendable.
from random import randint
while True:
roll = input("What sided Die would you like to roll(You may enter quit to close)? ")
if roll == 'quit':
break
elif roll in ('6', '8', '10', '12', '20'):
print(randint(1, int(roll)))
else:
print("That's not an option please choose a die...")
print("Thank you")
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy
python 2 interprets your input. you cannot have an integer AND a string. python 3 would fail when comparing to numbers, python 2 fails when interpreting "quit"
– Jean-François Fabre♦
Sep 16 '18 at 20:01