2

这是我第一次发布和使用这个网站,如果这不是最好的提问方式,很抱歉。我曾尝试自己研究并在此论坛上寻找答案,但我发现的每个显示器要么与我自己的语言不同,要么有不同的问题。我遇到的问题是游戏在终端中运行,就像我想要的一样,除了当涉及平局时,它不会注册平局,只是开始一个新游戏“选择你的选项或键入退出退出”在下一个线。我使用 Thonny 作为我的编辑器,并遵循了调试过程,它似乎只是“跳过”了 tie 命令。我该如何解决这个问题,或者是否有一些信息或文档可以指点我以帮助我理解我的问题。谢谢

    #rock, paper, scissors
import random, sys
wins = 0
losses = 0
ties = 0
print('Let\'s play a game')
print('rock. paper. scissors')

while True:
    print('choose your option or type quit to exit')
    print('%s wins, %s losses, %s ties'%(wins, losses, ties))
    while True:
        playeroption = input()
        if playeroption == 'quit':
            sys.exit() # Quit the program.
        if playeroption == 'rock' or playeroption == 'paper' or playeroption == 'scissors':
            break # Break out of the player input loop.
        
    randomNumber = random.randint(0,2) #selecting computer options
    if randomNumber == 0:
        AIoption = 'Rock'
    elif randomNumber == 1:
        AIoption = 'Scissors'
    elif randomNumber == 2:
        AIoption = 'Paper'       
    if playeroption == AIoption: #player vs computer outcomes
        print('draw, i choose ' + str( AIoption) +'!')
        ties = ties + 1
        continue
    elif AIoption == 'Paper' and playeroption == 'rock':
        print('better luck next time i choose ' + (  AIoption))
        losses = losses + 1
        continue
    elif AIoption == 'Rock' and playeroption == 'scissors':
        print('better luck next time i choose ' + str(AIoption))
        losses = losses + 1
        continue
    elif AIoption == 'Scissors' and playeroption == 'paper':
        print('better luck next time i choose ' + str(AIoption))
        losses = losses + 1
        continue
    elif AIoption == 'Paper' and playeroption == 'scissors':
        print('you win human. I choose ' + (  AIoption))
        wins = wins + 1
        continue
    elif AIoption == 'Rock' and playeroption == 'paper':
        print('you win human. I choose ' + str(AIoption))
        wins = wins + 1
        continue
    elif AIoption == 'Scissors' and playeroption == 'rock':
        print('you win human. I choose ' + str(AIoption))
        wins = wins + 1
        continue
4

1 回答 1

1

这里的问题是,您的playeroption值每次都是小写的,并且您将其与大写的值进行比较,因此您if playeroption == AIoption永远不会返回 true。如果您想忽略区分大小写,可以执行以下操作:

if playeroption.lower() == AIoption.lower()

所以python会让你的字符串都小写,然后比较它,第一个字符是小写还是大写都没有关系。

您也可以将该.lower()功能添加到您的if playeroption == 'rock' or playeroption == 'paper' or playeroption == 'scissors': 喜好if playeroption.lower() == 'rock' ...中,然后用户可以输入他的选项,并且单词以小写还是大写开头都没有关系。

于 2020-07-08T06:04:33.900 回答