岩石纸剪刀陷入循环

时间:2017-11-18 03:59:10

标签: python function

我正在制作摇滚,纸,剪刀。

在3次胜负之后,我希望它离开while循环但是它一直在运行。

##################
# import modules #
##################

from random import randint

# Declaration of variables #

pcCounter, counter = 0, 0
choice = ""
pc = 0

################
# Main program #
################
print("Welcome to Dags Rock,Paper,Scissors")
print("We will be playing a best out of 5")
choice = input("Select rock, paper or scissors")

while counter != 3 or pcCounter != 3:
    if choice == "rock" or choice == "Rock":
        pc = randint(1,3)
        if pc == 1:
            print("You have tied")
            choice = input("Enter either rock, paper or scissors")
        elif pc == 2:
            print("You have lost, Paper beats rock")
            pcCounter += 1
            choice = input("Enter either rock, paper or scissors")
        else:
            print("You have won, rock beats scissors")
            counter += 1
            choice = input("Enter either rock, paper or scissors")
    elif choice == "paper" or choice == "Paper":
        pc = randint(1,3)
        if pc == 1:
            print("You have won, paper beats rock")
            counter += 1
            choice = input("Enter either rock, paper or scissors")
        elif pc == 2:
            print("You have tied")
            choice = input("Enter either rock, paper or scissors")
        else:
            print("You have lost, scissors beats paper")
            pcCounter += 1
            choice = input("Enter either rock, paper or scissors")
    else:
        pc = randint(1,3)
        if pc == 1:
            print("You have lost, rock beats scissors")
            pcCounter += 1
            choice = input("Enter either rock, paper or scissors")
        elif pc == 2:
            print("You have won, scissors beats paper")
            Counter += 1
            choice = input("Enter either rock, paper or scissors")
        else:
            print("You have tied")
            choice = input("Enter either rock, paper or scissors")
#Checks if you won or lost            
if counter == 3:
    print("Congratz you won :)")
else:
    print("Darn you lost :(")enter code here

2 个答案:

答案 0 :(得分:2)

您的while测试不对。变化:

while counter != 3 or pcCounter != 3:

为:

while counter != 3 and pcCounter != 3:

只有当两个计数器都不是3时,才想继续循环。

答案 1 :(得分:0)

在你的其他条件下,为什么逆变量与资本'C'?不应该反击吗?

else:
        pc = randint(1,3)
        if pc == 1:
            print("You have lost, rock beats scissors")
            pcCounter += 1
            choice = input("Enter either rock, paper or scissors")
        elif pc == 2:
            print("You have won, scissors beats paper")
            Counter += 1
            choice = input("Enter either rock, paper or scissors")
        else:
            print("You have tied")
            choice = input("Enter either rock, paper or scissors")