【问题标题】:How to print if first condition is not met?如果不满足第一个条件,如何打印?
【发布时间】:2017-06-08 20:50:18
【问题描述】:

我正在练习条件和逻辑运算符。

如何让下面的石头、纸、剪刀游戏打印“这不是一个有效的对象选择”。在玩家 1 的输入之后,如果玩家 1 输入了一个无效的对象?现在,直到两个玩家都输入一个对象,该字符串才被打印出来。

另外,有什么建议可以让下面的代码更优雅吗?

player1 = input('Player 1? ')
player2 = input('Player 2? ')

if (player1.lower() == 'rock' and
    player2.lower() == 'rock'):
    print('Tie.')
elif (player1.lower() == 'rock' and
    player2.lower() == 'paper'):
    print('Player 2 wins.')
elif (player1.lower() == 'rock' and
    player2.lower() == 'scissors'):
    print('Player 1 wins.')
elif (player1.lower() == 'paper' and
    player2.lower() == 'paper'):
    print('Tie.')
elif (player1.lower() == 'paper' and
    player2.lower() == 'scissors'):
    print('Player 2 wins.')
elif (player1.lower() == 'paper' and
    player2.lower() == 'rock'):
    print('Player 1 wins.')
elif (player1.lower() == 'scissors' and
    player2.lower() == 'scissors'):
    print('Tie.')
elif (player1.lower() == 'scissors' and
    player2.lower() == 'rock'):
    print('Player 2 wins.')
elif (player1.lower() == 'scissors' and
    player2.lower() == 'paper'):
    print('Player 1 wins.')
else:
    print('This is not a valid object selection.')

【问题讨论】:

    标签: python-3.x


    【解决方案1】:

    问每个玩家选择的功能怎么样?只有当玩家输入一个有效的选择时,他们才被允许通过代码继续你的逻辑语句。

    def get_choice(Player_number):
        print('Player', Player_number, 'please enter your choice: ', end='')
        while True:
            choice = input().lower()        #lower converts input to all lowercase
            if choice in ('rock', 'paper', 'scissors'):   # check if valid choice 
                return choice   # if valid return choice
            else:
                print('Incorrect choice, try again: ', end='')  # else print this and start loop agan
    
    player1 = get_choice('1')
    player2 = get_choice('2')
    

    对于逻辑部分,请注意如果 player1 选择 == 玩家 2 选择,这将替换代码中的 3 个 elif 块,即。

    if player1 == player2:
        print('tie')
    

    现在替换 pl==rock 和 p2 == rock、p1 == scissors 和 p2 == scissors 等。

    编辑:

    1) 如果将 2 中的打印语句移动到 4 中的输入内部,问题是您不能再指定 ,end='' 参数,因为它不适用于输入。正如你所说,它会产生 SyntaxError。以这种方式在终端中运行时,代码看起来更清晰,因为它们都排成一行,但请自己测试一下。

    2) end='' 在 print 语句的末尾打印一个空字符串。您将此与 end='\n' 混淆,后者在打印后打印新行。由于我的方式在打印后将光标保持在同一行,因此它与输入对齐以使其看起来不错,请参见上面的问题。注意python中的print语句默认passes,end='\n'。这就是为什么

    print('hello')    #same as print('hello', end='\n')
    print('world)
    hello
    world
    
    print('hello', end='')
    print('world', end='')
    helloworld
    

    3) while True 循环的计算结果总是为 true。因此,while 循环的主体将不断循环 - 但是,它可以使用“返回选择”函数从整个函数中返回。离开此函数的唯一方法是了解到达 return 语句所需的逻辑步骤。在这种情况下,除非选择在那个元组中('rock', 'paper', 'scisors'),否则它只会打印无效条目再试一次,完成块。但是,当 while 仍然为真时,循环将再次开始并要求用户再次输入。这将重复,直到选择了一个有效的选项。

    4) 你可以将它连接到 player_number 例如..

    print('Player', Player_number + ',' , 'please enter your choice: ', end='')
    

    【讨论】:

    • 谢谢!还没有使用函数,但我想我了解大部分语法。不过有几个问题:1)是否可以将第 2 行的 print() 中的代码以某种方式插入第 4 行的 input() 中?我试过了,但收到语法错误。 2)我认为默认使用 end='' 会在 print 语句之后创建一个新行?但这似乎适得其反。你能解释一下为什么吗? 3) 为什么在第 3 行使用 while 循环?不是只需要跑一遍代码吗?
    • 4) 如果我想在第 2 行的 Player_number 后面加一个逗号,那是如何实现的?我不知道如何避免空格。
    • 检查我的编辑,我已尝试回答问题。请采纳我的回答,谢谢!
    猜你喜欢
    • 2015-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多