在Python中的石头布和剪刀

我是编码新手,我创建的 “石头、纸、剪刀 “游戏不能如期运行。如果有人输入 “石头、纸或剪刀 “这个词,那么程序就能正常工作。然而,当有人输入了除石头、纸或剪刀以外的其他单词时,程序应该说 “我不明白,请再试一次”,并提示用户输入另一个输入,它确实这样做了,但随后程序没有继续按原计划工作,而是结束了。下面是代码。

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

解决方案:

你的问题似乎是由于在函数内部调用函数引起的。这样我就解决了。

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 [],只是比输入所有的选项要简单一些。

希望这能帮助你

给TA打赏
共{{data.count}}人
人已打赏
未分类

循环一个 "If Else "语句,用于比较R中的每日covid增长。

2022-9-13 14:07:37

未分类

如何设置报表仪表盘的数据库

2022-9-13 14:07:39

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索