簡體   English   中英

如果 function 內部發生了應該停止迭代的事情,我該如何停止 while 循環?

[英]How do I stop a while loop if something happens inside a function that should stop the itterations?

各位程序員,您好,我是 python 的初學者,幾個月前。 我決定開始自己的小項目,以幫助我了解 Python 的整個開發過程。 我簡要了解了所有基本語法,但我想知道如何在 function 調用 while 循環結束時進行一些操作。

我正在創建一個簡單的終端號碼猜謎游戲,它由玩家多次嘗試猜測 1 到 10 之間的數字(我目前將其設置為 1 以測試代碼中的某些內容)。

如果玩家得到正確的數字,則該級別應該結束,然后玩家將進入下一個級別的游戲。 我試圖創建一個變量並做出一個真正的錯誤陳述,但我無法在 while 循環內操作 function 中的變量。

我想知道如何才能使游戲在玩家獲得正確的數字時結束,我將在此處包含我的代碼,以便你們有更多的上下文:

import random
import numpy
import time

def get_name(time):

    name = input("Before we start, what is your name? ")

    time.sleep(2)
    print("You said your name was: " + name)


# The Variable 'tries' is the indication of how many tries you have left
tries = 1

while tries < 6:

    def try_again(get_number, random, time):

        # This is to ask the player to try again
        answer = (input(" Do you want to try again?"))
        time.sleep(2)

        if answer == "yes":

            print("Alright!, well I am going to guess that you want to play again")
            time.sleep(1)

            print("You have used up: " + str(tries) + " Of your tries. Remember, when you use 5 tries without getting the correct number, the game ends")

        else:
            print("Thank you for playing the game, I hope you have better luck next time")
    

    def find_rand_num(get_number, random, time):

        num_list = [1,1]
        number = random.choice(num_list)

        # Asks the player for the number
        ques = (input("guess your number, since this is the first level you need to choose a number between 1 and 10  "))
        print(ques)

        if ques == str(number):
            time.sleep(2)

            print("Congratulations! You got the number correct!")
            try_again(get_number, random, time)


        elif input != number:
            time.sleep(2)

            print("Oops, you got the number wrong")
            try_again(get_number, random, time)
            
    def get_number(random, try_again, find_rand_num, time):
        
        # This chooses the number that the player will have to guess  
        time.sleep(3)              
        print("The computer is choosing a random number between 1 and 10... beep beep boop")
        time.sleep(2)

        find_rand_num(get_number, random, time)


    if tries < 2:
        get_name(time)
        tries += 1
        get_number(random, try_again, find_rand_num, time)

    else:
        tries += 1
        get_number(random, try_again, find_rand_num, time)


    if tries > 5:
        break

對於代碼中的一些格式,我深表歉意,我盡我所能看起來和我的 IDE 中的一樣准確。 我父親通常會幫助我解決這些類型的問題,但在這一點上,我似乎比我父親了解更多 python,因為他從事前端 web 開發。 所以,回到我原來的問題,如果這個聲明:

    if ques == str(number):
        time.sleep(2)

        print("Congratulations! You got the number correct!")
        try_again(get_number, random, time)

是真的,while 循環結束了嗎? 另外,我的代碼看起來如何? 我花了一些時間使它看起來整潔,從專家的角度來看我很感興趣。 我曾經在編程中讀到過,少即是多,所以我試圖用我的代碼少花錢多辦事。

感謝您花時間閱讀本文,如果你們中的某些人對我的問題有任何解決方案,我將不勝感激。 祝你有美好的一天!

您的代碼中有太多錯誤。 首先,您從未使用過在函數中傳遞的參數,所以我看不出有理由讓它們留在那里。 然后你需要從你的函數中返回一些東西來使用它們來打破條件(例如True/False )。 最后,我想在您的情況下單獨調用函數要方便得多,因為您需要在繼續之前進行一些檢查(不在彼此內部)。 所以,這是我最終得到的代碼:

import random
import time

def get_name():
    name = input("Before we start, what is your name? ")

    time.sleep(2)
    print("You said your name was: " + name)


def try_again():
    answer = (input("Do you want to try again? "))
    time.sleep(2)

    # Added return True/False to check whether user wants to play again or not
    if answer == "yes":
        print("Alright!, well I am going to guess that you want to play again")
        time.sleep(1)

        print("You have used up: " + str(tries) + " Of your tries. Remember, when you use 5 tries without getting the correct number, the game ends")
        return True

    else:
        print("Thank you for playing the game, I hope you have better luck next time")
        return False


# Joined get_number and find_random_number since get_number was doing nothing than calling find_rand_num
def find_rand_num():
    time.sleep(3)              
    print("The computer is choosing a random number between 1 and 10... beep beep boop")
    time.sleep(2)

    num_list = [1,1]
    number = random.choice(num_list)

    ques = (input("guess your number, since this is the first level you need to choose a number between 1 and 10  "))
    print(ques)

    if ques == str(number):
        time.sleep(2)
        print("Congratulations! You got the number correct!")
        # Added return to check if correct answer is found or not
        return "Found"
    elif input != number:
        time.sleep(2)
        print("Oops, you got the number wrong")


tries = 1
while tries < 6:
    if tries < 2:
        get_name()
    
    res = find_rand_num()
    if res == "Found":
        break

    checker = try_again()
    if checker is False:
        break
    
    # Removed redundant if/break since while will do it itself
    tries += 1

暫無
暫無

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

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