简体   繁体   中英

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?

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")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM