如果用户未从列表中输入值,则在Python中循环
我想出了一个简单的代码,使用如果用户未从列表中输入值,则在Python中循环,python,python-3.x,Python,Python 3.x,我想出了一个简单的代码,使用input和random函数玩一个石头、纸、棍子和石头的游戏。在下面的代码中,我试图找出如果用户输入的整数不是有效的选择,如何循环my_input import random import time def my_input(): #validinput = False #while not validinput: while True: try: mychoice = int(input("You
input
和random
函数玩一个石头、纸、棍子和石头的游戏。在下面的代码中,我试图找出如果用户输入的整数不是有效的选择,如何循环my_input
import random
import time
def my_input():
#validinput = False
#while not validinput:
while True:
try:
mychoice = int(input("Your choice? (1. paper, 2. scissor, 3. stick or 4. stone) "))
except ValueError:
print('You did not enter a valid input. Enter again.')
continue
else:
if mychoice == 1:
return 'paper'
elif mychoice == 2:
return 'scissor'
elif mychoice == 3:
return 'stick'
elif mychoice == 4:
return 'stone'
else:
return 'invalid choice'
def randomized():
choice = ['paper', 'scissor', 'stick', 'stone']
return random.choice(choice)
def game():
user_continue = True
while user_continue:
myinput = my_input()
print('YOU: ' + str(myinput))
randomval = randomized()
time.sleep(1)
print('COMPUTER: ' + randomval)
won = 'You won!'
lost = 'Sorry, you lost.'
draw = "It's a draw."
if myinput == 'paper' and randomval == 'scissor':
time.sleep(1)
print(lost)
elif myinput == 'scissor' and randomval == 'paper':
time.sleep(1)
print(won)
elif myinput == 'paper' and randomval == 'stick':
time.sleep(1)
print(lost)
elif myinput == 'stick' and randomval == 'paper':
time.sleep(1)
print(won)
elif myinput == 'paper' and randomval == 'stone':
time.sleep(1)
print(won)
elif myinput == 'stone' and randomval == 'paper':
time.sleep(1)
print(lost)
elif myinput == 'scissor' and randomval == 'stick':
time.sleep(1)
print(lost)
elif myinput == 'stick' and randomval == 'scissor':
time.sleep(1)
print(won)
elif myinput == 'scissor' and randomval == 'stone':
time.sleep(1)
print(lost)
elif myinput == 'stick' and randomval == 'stone':
time.sleep(1)
print(lost)
elif myinput == 'stone' and randomval == 'stick':
time.sleep(1)
print(won)
else:
print(draw)
#continue looping until user says no
user_yn = str.upper(input('Do you want to continue? Press Enter or type any value other than N '))
if user_yn != 'N':
print('continuing...')
continue
else:
break
game()
如果您有任何其他缩短此代码的建议,我们将不胜感激。您可以使用字典进行选择: mychoice={1:“纸”,2:“剪刀”,3:“棍子”}
这将浓缩一些。也将有助于您的随机函数您可以使用字典进行mychoice: mychoice={1:“纸”,2:“剪刀”,3:“棍子”} 这将浓缩一些。也将有助于你的随机功能你就快到了 简短回答: 用
print('invalid choice')
替换return'invalid choice'
解释:
将输入的输入值与期望的值1到4进行比较后,您将使用return语句从无限循环返回。如果该值是所需的值,则此操作有效,但即使对于无效值,也会返回“无效值”。因此,不要返回它,而是打印相同的消息给用户,因为它在无限循环中,菜单再次打印,提示用户再次输入他的选择。在用户输入1到4之间的所需值之前,这不会退出循环。您就快到了
简短回答:
用print('invalid choice')
替换return'invalid choice'
解释:
将输入的输入值与期望的值1到4进行比较后,您将使用return语句从无限循环返回。如果该值是所需的值,则此操作有效,但即使对于无效值,也会返回“无效值”。因此,不要返回它,而是打印相同的消息给用户,因为它在无限循环中,菜单再次打印,提示用户再次输入他的选择。在用户输入1到4之间的所需值之前,这不会退出循环。这样,当您循环时,您可以使用dict mychoice[1]==纸张等,然后您可以说p1是人类,p2是计算机。然后检查mychoice==mydict[1]和randomval==mydict[2]…winner==p2。。。从那里开始。那样的话,你就不需要完整的。。虽然我真的不知道怎么做,但我只是想告诉你这个想法。这样,当你循环时,你可以使用dict mychoice[1]==纸张等,然后你可以说p1是人类,p2是计算机。然后检查mychoice==mydict[1]和randomval==mydict[2]…winner==p2。。。从那里开始。那样的话,你就不需要完整的。。虽然我真的不知道该怎么做,但我只是想表达一下我的想法。发布你的代码以获得改进建议。确保代码完整且工作正常。发布代码以获取改进建议。确保代码完整且工作正常。