I am making a text based adventure game in python 3 and I was wondering what the simplest loop is. Using the code I have, it continues to print "whats the number"
even when you put the correct number, also giving 9
as input doesnt work. It also doesn't work when I give ("8","9")
. Here is my code :
print("whats the number?")
required_number = ("8" or "9")
while True:
number = input()
if number == required_number:
print ("GOT IT")
else: print ("Wrong number try again")
Try this :
print("whats the number?")
required_number = [8,9]
while True:
number = int(input())
if number in required_number :
print('GOT IT')
break
else:
print('Wrong number try again')
Sample output in shell :
whats the number?
5
Wrong number try again
2
Wrong number try again
4
Wrong number try again
8
GOT IT
print("whats the number?")
required_number = [8,9]
while True: number = input()
if number in required_number:
print ("GOT IT")
break
else:
print ("Wrong number try again")
The word you are looking for in place of ==
is in
since required_number
is a tuple you're looking to see if the input is in
required_number
. Also the correct syntax for the tuple would be using a comma not or
.
I also would make required_number
plural to be a more accurate description of what it holds, and you probably want to use integers and not strings.
required_numbers = (8, 9)
while True:
number = int(input("whats the number?"))
if number in required_numbers:
print("GOT IT")
break #Stop asking
else:
print ("Wrong number try again")
input
treats it as a str
in Python 3.x
Preferably use a list
for the required numbers
Using in
to check for the number in the required_numbers
Put it in a try block to catch value error exceptions.
Hence:
required_number = [8,9] # a list of integer types
while True:
try:
number = int(input("whats the number? ")) # Using `int` to convert the `str`
if number in required_number:
print ("GOT IT")
break # break out when the number is found
else:
print ("Wrong number try again")
except ValueError:
print("Invalid Input, Please enter an integer only.")
Note:
==
determines if the values areequal
, whilein
operator iterates over the list of elements and returnsTrue
orFalse
.
OUTPUT :
whats the number? g
Invalid Input, Please enter an integer only.
whats the number? abc
Invalid Input, Please enter an integer only.
whats the number? 3
Wrong number try again
whats the number? 9
GOT IT
If your required_number
or the input will accommodate a string
, then you can use this:
required_number = [8,9]
required_number = str(required_number)
number = None
while True:
number = input("Write a number: ")
if number in required_number:
print ("GOT IT")
else:
print ("Wrong number try again")
Output:
Write a number: 3
Wrong number try again
Write a number: 8
GOT IT
Write a number: Hi
Wrong number try again
Try this Method
print('Enter a Number:')
required_number = ['8','9']
while True:
number = input()
if number in required_number:
print ("GOT IT")
break
else:
print ("Wrong number try again")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.