Python皮石头纸和剪刀

hgc7kmma  于 2022-12-10  发布在  Python
关注(0)|答案(2)|浏览(182)

我是个编程新手,我创建的石头剪刀布游戏并没有按预期运行。如果有人输入单词rock、paper或scissors,程序就会按预期运行。但是,当有人输入单词rock、paper或scissors以外的单词时,程序会说“我不明白,请再试一次”,并提示用户输入另一个输入,它确实做到了。但是程序并没有按预期继续工作,而是结束了。代码如下:

# 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()
px9o7tmv

px9o7tmv1#

您的问题似乎是由于在函数中调用函数而引起的。这为我解决了问题:

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>>> ')

这样,如果计算机不喜欢输入,它可以只要求另一个。此外,我会建议使用if user_choice.lower() in [],只是比键入所有选项容易一点。
希望这对你有帮助!

xa9qqrwz

xa9qqrwz2#

代替

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

continue将导致程序跳过循环的其余部分(这里,跳过return),并继续while循环的下一次迭代(返回到user_choice = ...)。还请注意,我认为还有另一个bug,如果user_choice"q",则实际上不会返回结果。

相关问题