[英]Repeating a while loop Python
so i am trying to make this rock paper scissors game, i want to make a while loop that terminates when user input is "quit" but it doesnt seem to work, it either fails to repeat or continues indefinitly, any idea why?所以我正在尝试制作这个石头剪刀布游戏,我想制作一个 while 循环,当用户输入“退出”时终止,但它似乎不起作用,它要么无法重复,要么无限地继续,知道为什么吗?
def game():
computer_wins = 0
player_wins = 0
feedback = input("Welcome to my rock, paper scissors game, type 'rock', 'paper' or 'scissors': ")
checked_feedback = " "
while checked_feedback != "quit":
checked_feedback = check_word_input(feedback, words, badwords)
#line above reffers to a function which tests the input for
#words like "rock", "paper" or "scissors", if false prints
# "invalid input" if true, returns input. if profanity,
#returns custom message
computer = random.choice(["rock","paper","scissors"])
if checked_feedback == computer:
print("its a tie!")
if (checked_feedback == 'rock' and computer == 'scissors') or (checked_feedback == 'paper' and computer == 'rock') or (checked_feedback == 'scissors' and computer == 'paper'):
player_wins += 1
print("you won!, your current score is now %d" %player_wins)
else:
computer_wins += 1
print("you lost!, opponents current score is now %d" %computer_wins)
print("type 'quit' to quit")
checked_feedback = check_word_input(feedback, words, badwords)
break #when this line is replaced with continue or left out, it gets stuck on infinite
#loop, else terminates after one iteration
print("computer score: ", computer_wins)
print("player score: ", player_wins)
you need to change你需要改变
print("type 'quit' to quit")
to至
feedback = input("type 'quit' to quit")
Also I would change我也会改变
feedback = input("Welcome to my rock, paper scissors game, type 'rock', 'paper' or 'scissors': ")
to至
print("Welcome to my rock, paper scissors game")
then at the first line of the loop should have那么在循环的第一行应该有
feedback = input("Rock, paper or scissors")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.