好的,我是编程的初学者(显然是在Python中),我的代码中存在问题。我自己编写了代码,使用了我在教程中学到的所有东西。我正在制作一个摇滚纸剪刀游戏。实际上,它正在工作,但是当我按下numpad'3'时,它会显示我的其他声明中的代码。有人可以帮我这个吗?
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!")
the python shell with the outputs of my program
帖子中有一个链接,就是'错误'的图片。我知道,我在最后的其他声明中写了错误,所以我知道是否有任何问题。 对于长的“else if”语句,我会在观看和阅读更多有关Python的教程时更改它。感谢您考虑:D
答案 0 :(得分:0)
您的CPU猜测是CPU猜测在1和4之间。将4替换为3。
cpuGuess = random.randint(1, 4)
应该是:
cpuGuess = random.randint(1, 3)
答案 1 :(得分:0)
这应该有用(我添加了一些代码,只允许输入为1,2或3的数字)。
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!")
答案 2 :(得分:-1)
查看random.randint函数。可悲的是它与范围或切片不一样。所以它进入else语句的原因是cpuGuess == 4。
另外,看看PEP8,我不知道为什么,但每当我看到这种暴躁的丘陵情况时,我只会畏缩一点。