Python中的剪刀石头布和剪刀

时间:2020-04-27 19:31:48

标签: python python-3.x

我是编码的新手,我创建的Rock Paper and 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()

2 个答案:

答案 0 :(得分:1)

您的问题似乎是由在函数内部调用该函数引起的。这为我解决了这个问题:

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 [],比输入所有选项要容易一些。

希望这会有所帮助!

答案 1 :(得分:0)

代替

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循环的下一次迭代(返回到{ {1}})。还要注意,我认为还有另一个错误,如果user_choice = ...user_choice,则实际上不会返回结果。