与无限循环有关的元组

时间:2017-11-21 13:57:13

标签: python set tuples

我被建议使用一个元组来创建一个方法来跟踪我的回合,但我很困惑我将如何执行这样的事情。我想跟踪玩家的对象和计算机的对象以及谁赢得了每一轮,但是通过我的程序设计,我无法理解如何在不必重复的情况下实现这一目标while循环10次并以这种方式跟踪胜利者

import random
Rounds = []
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")
Rounds = Rounds.append((Player_object_set, Computer_object_set))
R = "Rock"
r = "Rock"
P = "Paper"
p = "Paper"
S = "Scissors"
s = "Scissors"
print(Rounds[0][0])

1 个答案:

答案 0 :(得分:0)

元组是不可变的,这意味着您无法在现有元组中添加新元素,但您可以使用列表执行此操作。这就是为什么你可以使用你已经准备好的列表(我不知道它应该做什么)并将对象保存在那里。在循环结束时,您可以遍历该列表以提取它们。如果这不是您要求的,请更具体。这段代码可以进一步优化,但我不确定你是否有足够的Python和编程知识来理解最终代码 - 它可能有点过分。

我希望这段代码是您正在寻找的。请注意,您可能仍希望在运行时告诉用户结果,以便他知道发生了什么。此外,您可能需要检查输入以处理不需要的字母或字符串(例如Q或任何非p,r或s的字母)。

import random
Rounds = []
Player_Score = 0
Computer_Score = 0

#Tuples to contain possible outcomes
COMPUTER_WON = ("SR", "RP", "PS")
PLAYER_WON = ("RS", "PR", "SP")

while Player_Score < 5 and Computer_Score < 5:
    objects = input("Would you like to choose R, P, or S?").upper() + str(random.sample("RPS", 1)[0]) #Upper converts input to uppercase
#objects could be tuple instead of string if you changed "+" in the line above with ",", but tuple with two strings of fixed sizes (1) wouldn't make sense
#In that case, you should change COMPUTER_WON and PLAYER_WON to look like this (("S","R"), ("R","P"), ("P","S"))
    Rounds.append(objects)
    if objects in COMPUTER_WON: #if this string "objects" exists in tuple "COMPUTER_WON" i.e. if that element is one of the outcomes where computer wins:
        Computer_Score += 1
    elif objects in PLAYER_WON:
        Player_Score += 1

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")
for i in Rounds: #For each element in list "Rounds" do the following:
    if i in COMPUTER_WON: #if that element is one of the outcomes where computer wins:
        print("You have chosen " + i[0] + " and the Computer chose " + i[1]+ ". You have been beaten by the Computer and it has scored a point.")
    elif i in PLAYER_WON:
        print("You have chosen " + i[0] + " and the Computer chose " + i[1]+ ". You have beaten the Computer and you have scored a point.")
    else:
        print("You have chosen " + i[0] + " and the Computer chose " + i[1]+ ". You have tied with the Computer and neither of you have scored a point.")