Ray*_*d3s 5 python loops function
我正在制作一个摇滚纸剪刀游戏,并遇到了问题decisioncycle().我要做的是要求用户输入一个选项usercycle(),让计算机生成一个随机选择gamecycle(),然后确定谁赢得了回合并跟踪每个结果的输赢数.似乎决定何时随机工作.
import random
class rpsgame:
rps= ["rock", "paper","scissors"]
wincount=0
losecount=0
def usercycle(self):
userchoice = input("rock, paper, scissor.....")
print("SHOOT")
return userchoice
def gamecycle(self):
computerchoice = random.choice(rpsgame.rps)
return computerchoice
def decisioncycle(self):
if rpsgame.usercycle(self) == rpsgame.rps[0] and rpsgame.gamecycle(self) == rpsgame.rps[1]:
print("paper beats rock, you lose!")
rpsgame.losecount +=1
elif rpsgame.usercycle(self) == rpsgame.rps[1] and rpsgame.gamecycle(self) == rpsgame.rps[0]:
print("paper beats rock, you win!")
rpsgame.wincount+=1
elif rpsgame.usercycle(self) == rpsgame.rps[0] and rpsgame.gamecycle(self) == rpsgame.rps[2]:
print("rock beats scissors, you win!")
rpsgame.wincount+=1
elif rpsgame.usercycle(self) == rpsgame.rps[2] and rpsgame.gamecycle(self) == rpsgame.rps[0]:
print("rock beats scissors, you lose!")
rpsgame.losecount+=1
elif rpsgame.usercycle(self) == rpsgame.rps[1] and rpsgame.gamecycle(self) == rpsgame.rps[2]:
print("scissors beats paper, you lose!")
rpsgame.losecount+=1
elif rpsgame.usercycle(self) == rpsgame.rps[2] and rpsgame.gamecycle(self) == rpsgame.rps[1]:
print("scissors beats paper, you win!")
rpsgame.wincount+=1
elif rpsgame.usercycle(self) == rpsgame.gamecycle(self):
print("it's a tie!!!")
print("wins {}, losses {}".format(rpsgame.wincount, rpsgame.losecount))
while True:
rg = rpsgame()
rg.usercycle()
rg.gamecycle()
rg.decisioncycle()
Run Code Online (Sandbox Code Playgroud)
我认为我的问题出在decisioncycle()中.这是我在课堂上的第一次尝试,因为游戏正在使用全局变量,但我在这里读到,这对未来来说是一种不好的做法.
您在每种情况下都要求新的用户输入。您可能只想读一次,然后每次都进行比较,例如
user_choice = self.usercicle()
game_choice = self.gamecycle()
if(user_choice == self.rps[0] and game_choice == self.rps[1]):
print "Paper beats rock, you lose!"
self.losecount += 1
elif( user_choice...
Run Code Online (Sandbox Code Playgroud)
等等