我正在研究 Python,几周前我创建了一个游戏,用户需要猜测用户自己定义的间隔之间的数字。现在我正在学习 Unittest,我决定为游戏编写一个测试模块。然而,由于它需要来自用户的 4 个输入(其中两个定义了将生成随机数的范围,一个是用户的猜测,最后一个是一个 Y/N 问题,供用户决定他是否想要继续。
import random
def main():
print('Welcome to the guess game!')
while True:
try:
low_param = int(input('Please enter the lower number: '))
high_param = int(input('Please enter the higher number: '))
if high_param <= low_param:
print('No, first the lower number, then the higher number!')
else:
break
except:
print('You need to enter a number!')
while True:
try:
result = random.randint(low_param, high_param)
guess = int(input(f'Please enter a number between {low_param} and {high_param}: '))
if low_param <= guess <= high_param:
if result == guess:
print('Nice, dude!')
break
else:
print ('Not yet, chap')
while True:
try_again = input('Would you like to try again? (Y/N) ')
if try_again.lower() == 'n':
break
elif try_again.lower() == 'y':
print('If you consider yourself capable...')
break
else:
pass
if try_again.lower() == 'n':
print('Ok, maybe next time, pal :v')
break
else:
print(f'Your guess must be between {low_param} and {high_param}')
except:
print('Are you sure you entered a number?')
if __name__ == '__main__':
main()
在测试中,我想创建一些方法来验证以下情况:
1 - low_param 或 high_param 不是数字 2 - low_param 高于 high_param 3 - 猜测高于 high_param 4 - 猜测低于 low_param 5 - 猜测是字符串 6 - try_again 既不是 Y 也不是 N
我设法在第一种方法上模拟了一个输入,但是我不知道如何将 print 语句断言为情况输出。对于其他情况,我需要模拟多个输入,然后我就卡住了。
我该如何解决这两个问题?
互换的青春
相关分类