Python number guessing game.
from random import randint
ran_no = randint(1,10)
print(ran_no)#for checking the output
try:
guess = int(input('\nYour guess between 1 to 10:- '))
except ValueError:
print("\n\tPlease enter numbers only\n")
quit
if type(guess) == int:
if guess == ran_no:
print("\n\t\tWhola, Your guess is correct!\n")
elif guess > 10:
print("\n\t\tUghh, Your guess is above limit!\n")
elif guess < 1:
print("\n\t\tUghh, Your guess is below limit!\n")
else:
print("\n\t\tUghh, Try again...\n")
else:
print("\n\tPlease enter numbers only\n")
When i use try-except when except condition occurs i don't know know how should i end code, please help
i think you need to add () after quit
from random import randint
ran_no = randint(1,10)
print(ran_no)#for checking the output
try:
guess = int(input('\nYour guess between 1 to 10:- '))
except ValueError:
print("\n\tPlease enter numbers only\n")
quit()
if type(guess) == int:
if guess == ran_no:
print("\n\t\tWhola, Your guess is correct!\n")
elif guess > 10:
print("\n\t\tUghh, Your guess is above limit!\n")
elif guess < 1:
print("\n\t\tUghh, Your guess is below limit!\n")
else:
print("\n\t\tUghh, Try again...\n")
else:
print("\n\tPlease enter numbers only\n")
你可以使用“exit(0)”而不是退出
See if this helps:
from random import randint
ran_no = randint(1,10)
print(ran_no)#for checking the output
try:
guess = input('\nYour guess between 1 to 10:- ')
if type(int(guess)) == int:
guess =(int(guess))
if guess == ran_no:
print("\n\t\tWhola, Your guess is correct!\n")
elif guess > 10:
print("\n\t\tUghh, Your guess is above limit!\n")
elif guess < 1:
print("\n\t\tUghh, Your guess is below limit!\n")
else:
print("\n\t\tUghh, Try again...\n")
else:
print("\n\tPlease enter numbers only\n")
except ValueError:
print("\n\tPlease enter numbers only\n")
quit()
Sample output 1:
Sample output 2:
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.