簡體   English   中英

重復一個while循環Python

[英]Repeating a while loop Python

所以我正在嘗試制作這個石頭剪刀布游戲,我想制作一個 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)

你需要改變

print("type 'quit' to quit")

feedback = input("type 'quit' to quit")

我也會改變

feedback = input("Welcome to my rock, paper scissors game, type 'rock', 'paper' or 'scissors': ")

print("Welcome to my rock, paper scissors game")

那么在循環的第一行應該有

feedback = input("Rock, paper or scissors")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM