【问题标题】:Rock paper scissors code not consistently printing outcome剪刀石头布代码打印结果不一致
【发布时间】:2021-07-26 07:22:03
【问题描述】:

这是我为一个非常基本的石头剪刀布游戏编写的代码。 但是,当我运行代码时。它并不总是打印游戏的结果,即。你赢了!,你输或平。我不太确定为什么会这样。救命!![1]

`

start_game = input('Type start to start game:').lower()
import random
def play():
    user = input("'rock' , 'paper', 'scissors' ").lower()
    if user == 'quit':
        print("Game ended :(")
        exit()
    computer = random.choice(['rock', 'paper','scissors'])
    print(computer)
    if user == computer:
        return 'tie'
    elif not user == is_win(user, computer):
        return 'You win!'
    else:
        return 'You lost!'

def is_win(player,opponent):
    if (player == 'rock'and opponent == 'scissors') or (player == 'scissors'and opponent == 'paper') or \
            (player=='paper' and opponent == 'rock'):
        return True
    print(play())

while True:
    if start_game != 'start':
        start_game = input('Sorry i dont understand please try again:')
    else:
        print(play())

输出: 输入 start 开始游戏:srt 对不起,我不明白,请再试一次:开始 “摇滚”、“纸”、“剪刀”摇滚 纸

`

【问题讨论】:

    标签: python


    【解决方案1】:

    决定获胜者的逻辑有点错误。对象user 只包含用户的选择。函数is_win 应该返回一个布尔值。当用户获胜时,您确实返回True,但当他失败时,您正在调用原始调用函数。

    start_game = input('Type start to start game:').lower()
    import random
    def play():
        user = input("'rock' , 'paper', 'scissors' ").lower()
        if user == 'quit':
            print("Game ended :(")
            exit()
        computer = random.choice(['rock', 'paper','scissors'])
        print(computer)
        if user == computer:
            return 'tie'
        elif is_win(user, computer):
            return 'You win!'
        else:
            return 'You lost!'
    
    def is_win(player,opponent):
        if (player == 'rock'and opponent == 'scissors') or (player == 'scissors'and opponent == 'paper') or \
                (player=='paper' and opponent == 'rock'):
            return True
        return False
    
    while True:
        if start_game != 'start':
            start_game = input('Sorry i dont understand please try again:')
        else:
            print(play())
    

    尝试进行上述更改,看看它是否适合您。

    【讨论】:

    • 非常感谢!!我只是想知道为什么我在 return False 之前不需要 else: 语句?
    • @Harini 因为如果'if'语句为真,那么函数将返回并且函数的其余部分将不会被执行。所以有一个“其他”不会有任何区别
    【解决方案2】:

    你在定义 is_win() 函数之前使用它!

    在播放函数之前定义函数!

    考虑一下

    start_game = input('Type start to start game:').lower()
    import random
    
    def is_win(player,opponent):
        if (player == 'rock'and opponent == 'scissors') or (player == 'scissors'and opponent == 'paper') or \
                (player=='paper' and opponent == 'rock'):
            return True
        print(play())
    
    def play():
        user = input("'rock' , 'paper', 'scissors' ").lower()
        if user == 'quit':
            print("Game ended :(")
            exit()
        computer = random.choice(['rock', 'paper','scissors'])
        print(computer)
        if user == computer:
            return 'tie'
        elif not user == is_win(user, computer):
            return 'You win!'
        else:
            return 'You lost!'
    
    
    while True:
        if start_game != 'start':
            start_game = input('Sorry i dont understand please try again:')
        else:
            print(play())
    

    【讨论】:

    • 这不是问题。他们在另一个函数中调用函数“is_win”。这两个函数都已定义,并且可以在编译器到达“while True”时使用。
    【解决方案3】:

    print(play()) 是干什么用的?您正在未完成的一轮中运行另一轮游戏。我认为您应该返回 False 而不是那个,并更改“elif”条件:

    start_game = input('Type start to start game:').lower()
    import random
    def play():
        user = input("'rock' , 'paper', 'scissors' ").lower()
        if user == 'quit':
            print("Game ended :(")
            exit()
        computer = random.choice(['rock', 'paper','scissors'])
        print(computer)
        if user == computer:
            return 'tie'
        elif is_win(user, computer):
            return 'You win!'
        else:
            return 'You lost!'
    
    def is_win(player,opponent):
        if (player == 'rock'and opponent == 'scissors') or (player == 'scissors'and opponent == 'paper') or \
                (player=='paper' and opponent == 'rock'):
            return True
        return False
    
    while True:
        if start_game != 'start':
            start_game = input('Sorry i dont understand please try again:')
        else:
            print(play())
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-17
      • 2021-01-10
      • 2018-04-06
      • 1970-01-01
      相关资源
      最近更新 更多