我坚持试图让它显示计算机和播放器的胜利次数。我尝试使用while
语句,但它们似乎从不工作,所以我有if
语句不允许我过去1.需要帮助理解需要在第57,58,66行修改的内容和69.
selection = {"0" : "Rock", "1" : "Paper", "2" : "Scissors", "3" : "Lizard", "4" : "Spock"}
def RPSLS():
Player = random.randrange(5)
Computer = random.randrange(5)
print "\n"
def Selection(Player):
if Player == 0:
Player = selection["0"]
elif Player == 1:
Player = selection["1"]
elif Player == 2:
Player = selection["2"]
elif Player == 3:
Player = selection["3"]
else:
Player = selection["4"]
print "Player chooses", Player
return Player
Selection(Player)
def Selection(Computer):
if Computer == 0:
Computer = selection["0"]
elif Computer == 1:
Computer = selection["1"]
elif Computer == 2:
Computer = selection["2"]
elif Computer == 3:
Computer = selection["3"]
else:
Computer = selection["4"]
print "Computer chooses", Computer
return Computer
Selection(Computer)
def Wins():
Difference = (Player - Computer) % 5
Playerwins =+ 0
Computerwins =+ 0
if Difference == 0:
print "Player and Computer tie!"
elif Difference <= 2:
Playerwins =+ 1
print "Player Wins!"
else:
Computerwins =+ 1
print "Computer Wins!"
print "\n"
print "Player Wins:", Playerwins
print "Computer Wins:", Computerwins
return Wins
Wins()
Loop = 0
while Loop != 10:
RPSLS()
Loop += 1
raw_input("\n\nPress the enter key to exit.")
答案 0 :(得分:1)
您的代码在范围,语法,命名约定等方面存在很多问题。我建议您阅读有关Python best practices的教程。但是,如果不进行完全重写,则需要进行最少量的更改才能使代码正常工作。
import random
selection = {0 : "Rock", 1 : "Paper", 2 : "Scissors", 3 : "Lizard", 4 : "Spock"}
Playerwins = 0
Computerwins = 0
def RPSLS():
global Playerwins, Computerwins
Player = random.randrange(5)
Computer = random.randrange(5)
print "Player chooses", selection[Player]
print "Computer chooses", selection[Computer]
Difference = (Player - Computer) % 5
if Difference == 0:
print "Player and Computer tie!"
elif Difference <= 2:
Playerwins += 1
print "Player Wins!"
else:
Computerwins += 1
print "Computer Wins!"
print "Player Wins:", Playerwins
print "Computer Wins:", Computerwins
print "\n"
Loop = 0
while Loop != 10:
RPSLS()
Loop += 1
raw_input("\n\nPress the enter key to exit.")
另外,我不认为usual rules你的Rock-Paper-Scissors-Lizard-Spock规则是正确的。根据你的规则Spock击败蜥蜴。看看这个solution。