所以我有一个石头剪刀游戏。当用户完成他们指定的#轮次时,系统会提示他们是否要继续,以及他们想要多少轮次。然而,无论我选择什么,循环都会永远持续下去。忽略if语句来结束循环。 继承我的代码的while循环部分:
while again == True:
print("It is ",name,"'s", " turn. What do you pick (R)ock, (P)aper, or (S)cissors?: ", sep = '',end = "")
print()
print()
user = str.lower(input())
print()
if user != "s" and user != "r" and user != "p":
print("Invalid input. Try again.")
print()
continue
else:
if user == "r":
userchoice = "rock"
rock()
print()
print(name,"picks Rock!")
if user == "p":
userchoice = "paper"
paper()
print()
print(name,"picks Paper!")
elif user == "s":
userchoice = "scissors"
X()
print()
print(name,"picks Scissors!")
print("Now it's computers turn....")
print('.')
time.sleep(1)
print('.')
time.sleep(1)
print('.')
print()
cpu = random.randint(1,3)
if cpu == 1:
cpuchoice = "rock"
rock()
print()
print(" Computer picks rock!")
if cpu == 2:
cpuchoice = "paper"
paper()
print()
print("Computer picks paper!")
elif cpu ==3:
cpuchoice = "scissors"
X()
print()
print("Computer picks scissors!")
if cpuchoice == "rock" and userchoice == "scissors" or (cpuchoice == "paper" and userchoice == "rock") or cpuchoice == "scissors" and userchoice == "paper":
print("Computer wins this round!")
cscore += 1
elif cpuchoice == userchoice:
print("Draw!")
else:
print(name,"Wins this round!")
uscore +=1
print()
itt += 1
print(itt)
print(rounds)
print(again)
print(uscore)
print(cscore)
if itt == rounds and uscore == cscore:
print("It's still a tie! Let's go one more round")
print()
itt -= 1
elif cscore + uscore == rounds:
if uscore > cscore:
print(name,"wins!")
else:
print("Computer wins!")
again = input("Play again? Enter 0 to quit.Anything else to continue")
if again != "0":
rounds = input("How many rounds this time?")
itt = 0
uscore = 0
cscore = 0
again = True
else:
again = False
答案 0 :(得分:0)
你再次缩进!= 0'块已关闭,我不知道Python在解析空格时使用的确切规则,但它可能是导致问题的原因。
答案 1 :(得分:0)
我怀疑您的问题与支票elif cscore + uscore == rounds:
有关。如果你打了一个领带并跑了一轮,你的两个分数之后就不会正确加起来。由于您正在检查确切的相等性,因此代码的最后部分将无法运行,游戏也不会结束。
虽然您可以使用>=
代替==
来解决这个问题,但我认为直接检查itt
(应该更好地命名,如round_counter
)可能比将分数加在一起更有意义。或者哎呀,你可以像这样重组整个事情:
if itt >= rounds:
if uscore == cscore:
# tied, play another round
else:
# game over, say who won, ask about playing again