[英]Python - Rock Paper Scissors - Include Input User Name and Score Counter
I'm trying to write a somewhat easy version of Rock Paper Scissors in Python 3 for elementary-middle school aged kids to understand easily and hopefully reproduce. 我正在尝试用Python 3编写一个稍微容易一些的剪刀石头布版本,以供初中年龄的孩子轻松理解并希望复制。
Besides the basic game, I want to incorporate the option for them to input the names for a player1 and player2, using %s so the program will print it back out. 除了基本游戏外,我还希望他们使用%s为他们输入选项来输入player1和player2的名称,以便程序将其打印出来。 I keep getting this error in my 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'**
I'm also trying to include the score counter which updates itself every round (player1 vs player2). 我还试图包括一个计分计数器,该计分器每轮都会更新(玩家1与玩家2)。 Usually it resets to 0 per round for win/tie/lose. 通常,每局赢/平局/输局它重置为0。
Please help me to see where the code went wrong. 请帮助我看看代码哪里出错了。 Thanks! 谢谢!
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)
You're trying to format the return value of your print function. 您正在尝试格式化打印函数的返回值。 Instead, to format the string you're printing, try: 相反,要格式化要打印的字符串,请尝试:
print("%s, what do you choose? Rock (1), Paper (2), or Scissors(3)?" % player1)
for the first statement, for example. 例如,对于第一个语句。 The formatting should occur inside the parenthesis. 格式化应在括号内进行。
In order to convert your input value to an integer, try: 为了将您的输入值转换为整数,请尝试:
choice1 = int(input("> "))
Currently, you're resetting the score to zero at the start of the while loop. 当前,您将在while循环开始时将分数重置为零。 To stop your score counters from resetting, put the 要阻止得分计数器重置,请在
player1score = 0
player2score = 0
before the while loop. 在while循环之前。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.