所以我试图跟踪每个玩家在一轮中玩什么,以及谁在我的R / P / S计划中获胜。有没有办法在一个可能无限重复的循环中做到这一点?这是代码
import random
Round = 0
Player_Score = 0
Computer_Score = 0
while Player_Score < 5 and Computer_Score < 5:
Player_object = input("Would you like to choose R, P, or S?")
Computer_object = random.sample("RPS", 1)[0]
if Player_object == "R" or Player_object == "r":
if Computer_object == "R":
print("You have chosen " +Player_object+ " and the Computer chose " +str(Computer_object)+ ".You have tied with the Computer and neither of you have scored a point.")
elif Computer_object == "P":
Computer_Score += 1
print("You have chosen " +Player_object+ " and the Computer chose " +str(Computer_object)+ ". You have been beaten by the Computer and it has scored a point.")
else:
Player_Score += 1
print("You have chosen " +Player_object+ " and the Computer chose " +str(Computer_object)+ ".You have beaten the Computer and you have scored a point.")
if Player_object == "P" or Player_object == "p":
if str(Computer_object) == "R":
Player_Score += 1
print("You have chosen " +Player_object+ " and the Computer chose " +str(Computer_object)+ ".You have beaten the Computer and you have scored a point.")
elif str(Computer_object) == "P":
print("You have chosen " +Player_object+ " and the Computer chose " +str(Computer_object)+ ". You have tied with the Computer and neither of you have scored a point.")
else:
Computer_Score += 1
print("You have chosen " +Player_object+ " and the Computer chose " +str(Computer_object)+ ".You have been beaten by the Computer and it has scored a point.")
if Player_object == "S" or Player_object == "s":
if str(Computer_object) == "R":
print("You have chosen " +Player_object+ " and the Computer chose " +str(Computer_object)+ ".You have been beaten by the Computer and it has scored a point.")
elif str(Computer_object) == "P":
Computer_Score += 1
print("You have chosen " +Player_object+ " and the Computer chose " +str(Computer_object)+ ". You have beaten the Computer and you have scored a point.")
else:
Player_Score += 1
print("You have chosen " +Player_object+ " and the Computer chose " +str(Computer_object)+ ".You have tied with the Computer and neither of you have scored a point.")
if Computer_Score == 5 and Player_Score != 5:
print("The Computer has won!")
if Player_Score == 5 and Computer_Score != 5:
print("You have won and beaten the computer")
R = "Rock"
r = "Rock"
P = "Paper"
p = "Paper"
S = "Scissors"
s = "Scissors"
答案 0 :(得分:0)
处理这类事情的最简单方法是使用一个列表,您可以在每个回合中存储所需的信息。
在循环之前,创建一个空列表
Rounds = []
并在循环结束时附加相关信息。 您可以使用元组将多条信息添加为列表中的单个条目。例如,要添加每个玩家播放的内容,您可以编写
Rounds.append((Player_object, Computer_object))
如果您想跟踪谁赢得该回合,请将此信息添加到if
/ else
的变量中,然后将其添加到元组中。
循环结束后,您可以通过访问此列表来访问每轮的信息。列表索引从0
开始,因此要访问第一轮的信息,您需要编写Rounds[0]
。元组中的每个元素也可以通过索引访问,因此可以使用Player_object
访问第1轮的Rounds[0][0]
。
如果您需要更多信息,请尝试在Python中搜索列表和元组。
答案 1 :(得分:0)
对于每一轮,你想存储两件事,对吧?球员和他们的动作,以及他们的胜利者。在一轮中,玩家只移动一次,因此我们需要在我们的数据结构中使用(玩家,移动)元组。可能会有很多玩家参加一轮比赛,因此使用list
最合适。因此,在每一轮中,你都可以得到(玩家,移动)元组的列表。
由于每一轮你都想跟踪胜利者,你可以制作一个代表一轮的元组:( [球员和动作列表],获胜者)。
最后,你想要一份这些清单!在游戏循环外定义此列表,但在每个循环结束时将一轮的数据附加到此列表。 ħ
希望这会有所帮助:)
答案 2 :(得分:-2)
无限循环......你走了
import random
Round = {}
while True:
cnt = 1
ans = raw_input("Wanna Play RPS (y/n) ? ")
if ans == 'y' or ans == 'Y':
Player_Score = 0
Computer_Score = 0
while Player_Score < 5 and Computer_Score < 5:
Player_object = raw_input("Would you like to choose R, P, or S? ")
Computer_object = random.sample("RPS", 1)[0]
if Player_object == "R" or Player_object == "r":
if Computer_object == "R":
print("You have chosen " + Player_object + " and the Computer chose " + str(
Computer_object) + ".You have tied with the Computer and neither of you have scored a point.")
elif Computer_object == "P":
Computer_Score += 1
print("You have chosen " + Player_object + " and the Computer chose " + str(
Computer_object) + ". You have been beaten by the Computer and it has scored a point.")
else:
Player_Score += 1
print("You have chosen " + Player_object + " and the Computer chose " + str(
Computer_object) + ".You have beaten the Computer and you have scored a point.")
if Player_object == "P" or Player_object == "p":
if str(Computer_object) == "R":
Player_Score += 1
print("You have chosen " + Player_object + " and the Computer chose " + str(
Computer_object) + ".You have beaten the Computer and you have scored a point.")
elif str(Computer_object) == "P":
print("You have chosen " + Player_object + " and the Computer chose " + str(
Computer_object) + ". You have tied with the Computer and neither of you have scored a point.")
else:
Computer_Score += 1
print("You have chosen " + Player_object + " and the Computer chose " + str(
Computer_object) + ".You have been beaten by the Computer and it has scored a point.")
if Player_object == "S" or Player_object == "s":
if str(Computer_object) == "R":
print("You have chosen " + Player_object + " and the Computer chose " + str(
Computer_object) + ".You have been beaten by the Computer and it has scored a point.")
elif str(Computer_object) == "P":
Computer_Score += 1
print("You have chosen " + Player_object + " and the Computer chose " + str(
Computer_object) + ". You have beaten the Computer and you have scored a point.")
else:
Player_Score += 1
print("You have chosen " + Player_object + " and the Computer chose " + str(
Computer_object) + ".You have tied with the Computer and neither of you have scored a point.")
if Computer_Score == 5 and Player_Score != 5:
Round[cnt] = "Computer"
print("The Computer has won!")
elif Player_Score == 5 and Computer_Score != 5:
Round[cnt] = "Player"
print("You have won and beaten the computer")
else:
break
cnt += 1
R = "Rock"
r = "Rock"
P = "Paper"
p = "Paper"
S = "Scissors"
s = "Scissors"
for i in Round:
print "Round ", i, "won by ", Round[i], "\n"