gpt4 book ai didi

带名称输入的 Python 石头/剪刀/布

转载 作者:行者123 更新时间:2023-11-30 23:01:58 27 4
gpt4 key购买 nike

我正在用Python创建一个石头剪刀布游戏。我的方法如下。然而,我不想打印玩家 1 和玩家 2,而是想让它们成为真实的名字。因此,可以说 John Wins 或 Joe Wins。如何实现每次打印玩家姓名的 raw_input 。

player1 = raw_input('Enter rock/paper/scissors: ')
player2 = raw_input('Enter rock/paper/scissors: ')

if (player1 == player2):
print "The game is a Tie"
elif (player1 == 'rock' and player2 == 'scissors'):
print "player 1 wins"
elif (player1 == 'rock' and player2 == 'paper'):
print "player 2 wins"
elif (player1 == 'paper' and player2 == 'rock'):
print "player 1 wins"
elif (player1 == 'paper' and player2 == 'scissors'):
print "player 2 wins"
elif (player1 == 'scissors' and player2 == 'paper'):
print "player 1 wins"
elif (player1 == 'scissors' and player2 == 'rock'):
print "player 2 wins"
else:
print "Invalid input"

最佳答案

只需添加两个包含玩家姓名的变量:

player1_name = raw_input('Player1 please enter your name: ')
player2_name = raw_input('Player2 please enter your name: ')

然后,当您想宣布获胜者时,请使用:

print (player1_name + 'wins')
print (player2_name + 'wins')

关于带名称输入的 Python 石头/剪刀/布,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34708229/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com