2017-11-18 117 views
0

我正在做岩石,纸,剪刀。岩石剪刀卡在while循环

经过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

您的while测试是不正确的。变化:

while counter != 3 or pcCounter != 3: 

到:

while counter != 3 and pcCounter != 3: 

要继续循环只有在没有计数器为3

+0

没错这就是问题所在,感谢一吨,只是一个初学者的错误,我想:d –

+0

注意,有,是由Shivin指出了一个额外的错误:在一个地方,你'计数器+ = 1'而比'counter + = 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") 
+0

这不是问题的根源,但这是一个很好的抓住,谢谢一吨:) –