我想寫一個輕鬆的版本的岩石剪刀在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)
Score1和2未初始化。我認爲你的印刷品需要額外的括號。另外,不要將循環中的分數歸零。 – Stefan