Python-岩石剪刀 - 包括输入用户名和评分计数器



我试图在Python 3中为小学中学的孩子们写一个简单的岩石剪刀版本,以便容易理解,希望能够复制。

除了基本游戏外,我还想将选项合并为输入player1和player2的名称,使用%s,以便该程序将其重新打印出来。我一直在O/P中遇到此错误:

Player 1 name: me
Player 2 name: you
%s, what do you choose? Rock (1), Paper (2), or Scissors(3)?
**Traceback (most recent call last):
  File "C:/Users/xyz/PycharmProjects/rps/scorekeeping.py", line 11, in <module>
    print("%s, what do you choose? Rock (1), Paper (2), or Scissors(3)?") % player1
TypeError: unsupported operand type(s) for %: 'NoneType' and 'str'**

我还试图包括每回合的分数计数器(player1 vs player2)。通常,它以获胜/领带/输球为每轮0。

请帮助我看看代码在哪里出错。谢谢!


player1 = input("Player 1 name: ")
player2 = input("Player 2 name: ")
while 1:
    player1score = 0
    player2score = 0
    print("%s, what do you choose? Rock (1), Paper (2), or Scissors(3)?") % player1
    choice1 = input("> ")
    print("%s, what do you choose? Rock (1), Paper (2), or Scissors(3)?") % player2
    choice2 = input("> ")
    if choice1 == choice2 :
        print("Its's a tie.")
    elif choice1 - choice2 == 1 or choice2 - choice1 == 2 :
        print("%s wins.") % player1
        score1 = score1 + 1
    else:
        print("%s wins.") % player2
        score2 = score2 + 1
    print("%s: %d points. %s: %d points.") % (player1, score1, player2, score2)

您正在尝试格式化打印功能的返回值。相反,要格式化要打印的字符串,请尝试:

print("%s, what do you choose? Rock (1), Paper (2), or Scissors(3)?" % player1)

例如,第一个语句。格式应发生在括号内。

为了将您的输入值转换为整数,请尝试:

choice1 = int(input("> "))

当前,您将分数重置为零循环开始时。要阻止您的分数计数器重置,请放置

player1score = 0
player2score = 0

while循环。

相关内容

  • 没有找到相关文章

最新更新