Python 巨蟒中的石头布和剪刀
我对编码还不熟悉,我创建的石头剪刀布游戏并没有按预期的那样工作。 如果有人输入单词“石头”、“布”或“剪刀”,那么程序将按预期运行。 然而,当某人输入除石头、布或剪刀以外的单词时,程序应说“我不懂,请再试一次”,并提示用户输入另一个输入,它确实输入了,但随后程序结束,而不是继续按预期工作。代码如下:Python 巨蟒中的石头布和剪刀,python,python-3.x,Python,Python 3.x,我对编码还不熟悉,我创建的石头剪刀布游戏并没有按预期的那样工作。 如果有人输入单词“石头”、“布”或“剪刀”,那么程序将按预期运行。 然而,当某人输入除石头、布或剪刀以外的单词时,程序应说“我不懂,请再试一次”,并提示用户输入另一个输入,它确实输入了,但随后程序结束,而不是继续按预期工作。代码如下: # The game of Rock Paper Scissors import random choices = ['rock', 'paper', 'scissors'] computer_
# The game of Rock Paper Scissors
import random
choices = ['rock', 'paper', 'scissors']
computer_choice = random.choice(choices)
selections = 'The computer chose ' + computer_choice + ' so'
computer_score = 0
user_score = 0
def choose_option():
user_choice = input('Please choose Rock, Paper or Scissors. (q to quit)\n>>> ')
while user_choice != 'q':
if user_choice in ['ROCK', 'Rock', 'rock', 'R', 'r']:
user_choice = 'rock'
elif user_choice in ['PAPER', 'Paper', 'paper', 'P', 'p']:
user_choice = 'paper'
elif user_choice in ['SCISSORS','Scissors', 'scissors', 'S', 's']:
user_choice = 'scissors'
else:
print("I don't understand, please try again.")
choose_option()
return user_choice
user_choice = choose_option()
while user_choice != 'q':
if user_choice == computer_choice:
print(selections + ' it\'s a tie')
elif (user_choice == 'rock' and computer_choice == 'scissors'):
user_score += 1
print(selections + ' you won! :)')
elif (user_choice == 'paper' and computer_choice == 'rock'):
user_score += 1
print(selections + ' you won! :)')
elif (user_choice == 'scissors' and computer_choice == 'paper'):
user_score += 1
print(selections + ' you won! :)')
elif (user_choice == 'rock' and computer_choice == 'paper'):
computer_score += 1
print(selections + ' you lost :(')
elif (user_choice == 'paper' and computer_choice == 'scissors'):
computer_score += 1
print(selections + ' you lost :(')
elif (user_choice == 'scissors' and computer_choice == 'rock'):
computer_score += 1
print(selections + ' you lost :(')
else:
break
print('You: ' + str(user_score) + " VS " + "Computer: " + str(computer_score))
computer_choice = random.choice(choices)
selections = 'The computer chose ' + computer_choice + ' so'
user_choice = choose_option()
而不是
else:
print("I don't understand, please try again.")
choose_option()
你可以用
while user_choice != 'q':
user_choice = input('Please choose Rock, Paper or Scissors. (q to quit)\n>>> ')
if user_choice in ['ROCK', 'Rock', 'rock', 'R', 'r']:
user_choice = 'rock'
elif user_choice in ['PAPER', 'Paper', 'paper', 'P', 'p']:
user_choice = 'paper'
elif user_choice in ['SCISSORS','Scissors', 'scissors', 'S', 's']:
user_choice = 'scissors'
else:
print("I don't understand, please try again.")
continue
return user_choice
继续
将导致程序跳过循环的其余部分(此处跳过返回
),并在循环时继续下一次迭代(返回到用户选择=…
)。还要注意的是,我认为还有一个bug,如果用户选择
是“q”
,结果实际上不会返回。您的问题似乎是由于调用函数内部的函数引起的。这为我解决了问题:
def choose_option():
user_choice = input('Please choose Rock, Paper or Scissors. (q to quit)\n>>> ')
while user_choice != 'q':
if user_choice in ['ROCK', 'Rock', 'rock', 'R', 'r']:
user_choice = 'rock'
return user_choice
elif user_choice in ['PAPER', 'Paper', 'paper', 'P', 'p']:
return user_choice
elif user_choice in ['SCISSORS','Scissors', 'scissors', 'S', 's']:
return user_choice
else:
while user_choice.lower() not in ["rock", "paper", "scissors", "s","r","p","q"]:
print("I don't understand, please try again.")
user_choice = input('Please choose Rock, Paper or Scissors. (q to quit)\n>>> ')
这样,如果计算机不喜欢输入,它可以请求另一个输入。
我还建议使用
如果user\u choice.lower()在[]
中,只比键入所有选项简单一点
希望这有帮助 您需要从递归调用返回,即else
子句中的return choose_option()
。作为补充说明,您可以使用['rock','r']中的if user_choice.lower():
甚至使用类似正则表达式的-if re.search(r'(剪刀),user_choice,flags=re.i)