Python - 用 try/except 猜数字
Python - Guess the number with try/except
嗨,我是 python 的新人,我搜索了一些小挑战来练习和学习更多,现在我正在做一个 'Guess the number' 脚本,它有效,但现在我想添加当用户输入的不是整数时,try/except 块,这是我现在的代码:
from random import *
print('Guess the number')
def check_correct(u_guess, num):
while True:
if u_guess < num:
print('Guess is high!')
while True:
try:
u_guess = int(input('What number do you think is? (Between 1 and 5): '))
except ValueError:
print('Invalid number try again!')
elif u_guess > num:
print('Guess is low!')
while True:
try:
u_guess = int(input('What number do you think is? (Between 1 and 5): '))
except ValueError:
print('Invalid number try again!')
elif u_guess == num:
print('You got it!')
break
def guess():
num = randint(1, 5)
u_guess = None
while u_guess == None:
try:
u_guess = int(input('What number do you think is? (Between 1 and 5): '))
check_correct(u_guess, num)
except ValueError:
print('Invalid number try again!')
guess()
这是我的输出:
$ python Project2.py
Guess the number
What number do you think is? (Between 1 and 5): 1
Guess is high!
What number do you think is? (Between 1 and 5): 2
What number do you think is? (Between 1 and 5): 2
What number do you think is? (Between 1 and 5):
Invalid number try again!
What number do you think is? (Between 1 and 5): Traceback (most recent call last):
File "Project2.py", line 35, in <module>
guess()
File "Project2.py", line 31, in guess
check_correct(u_guess, num)
File "Project2.py", line 11, in check_correct
u_guess = int(input('What number do you think is? (Between 1 and 5): '))
EOFError
check_correct
唯一应该做的就是 return 如果猜测正确则为 True,否则为 False。它还可以输出描述比较结果的消息。
import random # Never use from <whatever import *
print('Guess the number')
def check_correct(u_guess, num):
if u_guess < num: # You had the roles of u_guess and num reversed
print('Guess is low!')
return False
elif u_guess > num:
print('Guess is high')
return False
else: # u_guess == num
print('You got it!')
return True
guess
处理所有用户输入,调用 check_correct
来确定是否
外循环应该终止。内部循环继续,直到 int(uguess)
确实 不 引发异常。
def guess():
num = random.randint(1, 5)
while True:
while True:
u_guess = input('What number do you think it is? (Between 1 and 5)')
try:
u_guess = int(u_guess)
break
except ValueError:
print('Invalid number, try again!')
if check_correct(u_guess, num):
break
guess()
嗨,我是 python 的新人,我搜索了一些小挑战来练习和学习更多,现在我正在做一个 'Guess the number' 脚本,它有效,但现在我想添加当用户输入的不是整数时,try/except 块,这是我现在的代码:
from random import *
print('Guess the number')
def check_correct(u_guess, num):
while True:
if u_guess < num:
print('Guess is high!')
while True:
try:
u_guess = int(input('What number do you think is? (Between 1 and 5): '))
except ValueError:
print('Invalid number try again!')
elif u_guess > num:
print('Guess is low!')
while True:
try:
u_guess = int(input('What number do you think is? (Between 1 and 5): '))
except ValueError:
print('Invalid number try again!')
elif u_guess == num:
print('You got it!')
break
def guess():
num = randint(1, 5)
u_guess = None
while u_guess == None:
try:
u_guess = int(input('What number do you think is? (Between 1 and 5): '))
check_correct(u_guess, num)
except ValueError:
print('Invalid number try again!')
guess()
这是我的输出:
$ python Project2.py
Guess the number
What number do you think is? (Between 1 and 5): 1
Guess is high!
What number do you think is? (Between 1 and 5): 2
What number do you think is? (Between 1 and 5): 2
What number do you think is? (Between 1 and 5):
Invalid number try again!
What number do you think is? (Between 1 and 5): Traceback (most recent call last):
File "Project2.py", line 35, in <module>
guess()
File "Project2.py", line 31, in guess
check_correct(u_guess, num)
File "Project2.py", line 11, in check_correct
u_guess = int(input('What number do you think is? (Between 1 and 5): '))
EOFError
check_correct
唯一应该做的就是 return 如果猜测正确则为 True,否则为 False。它还可以输出描述比较结果的消息。
import random # Never use from <whatever import *
print('Guess the number')
def check_correct(u_guess, num):
if u_guess < num: # You had the roles of u_guess and num reversed
print('Guess is low!')
return False
elif u_guess > num:
print('Guess is high')
return False
else: # u_guess == num
print('You got it!')
return True
guess
处理所有用户输入,调用 check_correct
来确定是否
外循环应该终止。内部循环继续,直到 int(uguess)
确实 不 引发异常。
def guess():
num = random.randint(1, 5)
while True:
while True:
u_guess = input('What number do you think it is? (Between 1 and 5)')
try:
u_guess = int(u_guess)
break
except ValueError:
print('Invalid number, try again!')
if check_correct(u_guess, num):
break
guess()