Python代码上的错误输出
好的,我是一名编程初学者(显然是Python),我的代码中有一个问题。我刚刚自己编写了代码,使用了我在教程中学到的所有东西。我正在制作一个石头剪刀游戏。实际上,它正在工作,但当我按下numpad'3'时,它会显示my else语句中的代码。有人能帮我吗Python代码上的错误输出,python,python-3.5,Python,Python 3.5,好的,我是一名编程初学者(显然是Python),我的代码中有一个问题。我刚刚自己编写了代码,使用了我在教程中学到的所有东西。我正在制作一个石头剪刀游戏。实际上,它正在工作,但当我按下numpad'3'时,它会显示my else语句中的代码。有人能帮我吗 import random playerScore = 0 cpuScore = 0 def rpsGame(): userInput = int(input("Choose (1)rock (2)paper (3)scissors
import random
playerScore = 0
cpuScore = 0
def rpsGame():
userInput = int(input("Choose (1)rock (2)paper (3)scissors: "))
global playerScore
global cpuScore
cpuGuess = random.randint(1, 4)
if userInput == 1 and cpuGuess == 1:
print("A draw! rock with a rock!")
print("Player Score:", playerScore, "CPU Score:", cpuScore)
elif userInput == 1 and cpuGuess == 2:
print("CPU scores! paper never beats rock!")
cpuScore += 1
print("Player Score:", playerScore, "CPU Score:", cpuScore)
elif userInput == 1 and cpuGuess == 3:
print("Player scores! rock beats sharp scissors!")
playerScore += 1
print("Player Score:", playerScore, "CPU Score:", cpuScore)
elif userInput == 2 & cpuGuess == 1:
print("Player! paper never beats rock!")
playerScore += 1
print("Player Score:", playerScore, "CPU Score:", cpuScore)
elif userInput == 2 & cpuGuess == 2:
print("A draw! paper with a paper!")
print("Player Score:", playerScore, "CPU Score:", cpuScore)
elif userInput == 2 & cpuGuess == 3:
print("CPU scores! paper is cut with scissors!")
cpuScore += 1
print("Player Score:", playerScore, "CPU Score:", cpuScore)
elif userInput == 3 & cpuGuess == 1:
print("CPU scores! scissors can't cut rock!")
cpuScore += 1
print("Player Score:", playerScore, "CPU Score:", cpuScore)
elif userInput == 3 & cpuGuess == 2:
print("Player scores! Scissors beat paper")
playerScore += 1
print("Player Score:", playerScore, "CPU Score:", cpuScore)
elif userInput == 3 & cpuGuess == 3:
print("A draw! scissors with scissors!")
print("Player Score:", playerScore, "CPU Score:", cpuScore)
else:
print("Error")
while playerScore != 3 or cpuScore != 3:
rpsGame()
if playerScore >= 3:
print("Player wins!")
if cpuScore >= 3:
print("You lose! The opponent won!")
文章中有一个链接,是“错误”的图片。我知道,我在最后的else语句中写了错误,所以我会知道是否有任何问题。
很抱歉有这么长的“else-if”语句,我会在观看和阅读更多Python教程时对其进行更改。感谢您考虑:D查看random.randint函数。遗憾的是,这与范围或切片不同。所以它进入else语句的原因是cpuGuess==4
另外,看看PEP8,我不知道为什么,但每次我看到这个upy downy hilly案例时,我都会有点畏缩。您的CPU猜测值在1到4之间。将4替换为3
cpuGuess=random.randint(1,4)
应该是:
cpuGuess=random.randint(1,3)
这应该是可行的(我添加了一些代码,只允许输入为1、2或3的数字)
随机导入
playerScore=0
cpuScore=0
def rpsGame():
尽管如此:
尝试:
userInput=int(输入(“选择(1)石头(2)布(3)剪刀:”)
除值错误外:
打印(“那不是一个数字!”)
其他:
如果1=3:
打印(“玩家获胜!”)
如果cpuScore>=3:
打印(“你输了!对手赢了!”)
在从和切换到和的elif
的中途。为什么?randint
包含两个端点,因此大约四分之一的时间,您的代码会让计算机选择4,这与任何if
/elif
条件都不匹配。哇..没有看到elif和&符号,谢谢注意。回答你的问题,是因为我的电脑在我写代码时坏了,当我再次打开它时,代码保存了一半,我想用“and”代替“elif”,所以我写了“and”。
import random
playerScore = 0
cpuScore = 0
def rpsGame():
while True:
try:
userInput = int(input("Choose (1)rock (2)paper (3)scissors: "))
except ValueError:
print("That\'s not a number!")
else:
if 1 <= userInput < 3:
break
else:
print("Out of range. Try again")
global playerScore
global cpuScore
cpuGuess = random.randint(1, 3)
if userInput == 1 and cpuGuess == 1:
print("A draw! rock with a rock!")
print("Player Score:", playerScore, "CPU Score:", cpuScore)
elif userInput == 1 and cpuGuess == 2:
print("CPU scores! paper never beats rock!")
cpuScore += 1
print("Player Score:", playerScore, "CPU Score:", cpuScore)
elif userInput == 1 and cpuGuess == 3:
print("Player scores! rock beats sharp scissors!")
playerScore += 1
print("Player Score:", playerScore, "CPU Score:", cpuScore)
elif userInput == 2 and cpuGuess == 1:
print("Player! paper never beats rock!")
playerScore += 1
print("Player Score:", playerScore, "CPU Score:", cpuScore)
elif userInput == 2 and cpuGuess == 2:
print("A draw! paper with a paper!")
print("Player Score:", playerScore, "CPU Score:", cpuScore)
elif userInput == 2 and cpuGuess == 3:
print("CPU scores! paper is cut with scissors!")
cpuScore += 1
print("Player Score:", playerScore, "CPU Score:", cpuScore)
elif userInput == 3 and cpuGuess == 1:
print("CPU scores! scissors can't cut rock!")
cpuScore += 1
print("Player Score:", playerScore, "CPU Score:", cpuScore)
elif userInput == 3 and cpuGuess == 2:
print("Player scores! Scissors beat paper")
playerScore += 1
print("Player Score:", playerScore, "CPU Score:", cpuScore)
elif userInput == 3 and cpuGuess == 3:
print("A draw! scissors with scissors!")
print("Player Score:", playerScore, "CPU Score:", cpuScore)
else:
print("Error")
while playerScore != 3 or cpuScore != 3:
rpsGame()
if playerScore >= 3:
print("Player wins!")
if cpuScore >= 3:
print("You lose! The opponent won!")