簡體   English   中英

那時我應該在 Python 中做什么?

[英]What should I do at that point in Python?

我正在嘗試在 python.org 上學習 Python

我找到了這段代碼,我不知道它是什么意思。 我運行它,但什么也沒發生。

def ask_ok(prompt, retries=4, reminder='Please try again!'):
    while True:
        ok = input(prompt)
        if ok in ('y', 'ye', 'yes'):
            return True
        if ok in ('n', 'no', 'nop', 'nope'):
            return False
        retries = retries - 1
        if retries < 0:
            raise ValueError('invalid user response')
        print(reminder)

那么,這段代碼有什么作用呢?

這里定義了 ask_ok() 函數,它接受輸入prompt 因此,如下所示,您可以在您擁有的任何 Python IDE 中運行此代碼。

第 1 行將調用帶有 prompt = "do you want to continue" 的函數(您可以在此處寫入任何消息)。 函數被調用后它進入循環,它將檢查輸入是否為 ('y', 'ye', 'yes') 然后它將重新運行TRUE OR 如果輸入為 'n', 'no', 'nop', 'nope' 然后它會返回 false 但是如果輸入不是 ('y', 'ye', 'yes', 'n', 'no', 'nop', 'nope') 值然后循環將繼續並打印提醒="請再試一次!"

如果你會看到循環

 retries = retries - 1  # reties =4
    if retries < 0: 
        raise ValueError('invalid user response') 

直到 5 次循環將允許您輸入第 6 次輸入,它將拋出異常 ValueError('invalid user response') 。

循環將最多持續 5 次)

def ask_ok(prompt, retries=4, reminder='Please try again!'):#funcDefintion
while True: 
    ok = input(prompt) 
    if ok in ('y', 'ye', 'yes'): 
        return True 
    if ok in ('n', 'no', 'nop', 'nope'): 
        return False 

    retries = retries - 1 
    if retries < 0: 
        raise ValueError('invalid user response') 
    print(reminder)

ask_ok("你要繼續嗎") #line 1

為了練習,您可以更改函數定義中的值。 我建議先學習基礎知識,比如 if 條件、循環、異常、函數,然后你會更好地繼續下去。

# you're definition a method `ask_ok` which takes `prompt` as a required argument
# and the rest as optional arguments with default values
def ask_ok(prompt, retries=4, reminder='Please try again!'):

    # while (condition) starts a perpetual loop, broken with control flow
    # statements like `return` or `break` or errors raised.
    while True:
        ok = input(prompt) # ask for user input after displaying `prompt`
        if ok in ('y', 'ye', 'yes'): # checking if the input value is in a tuple
            return True # this returns a Boolean value True and breaks out of the method
        if ok in ('n', 'no', 'nop', 'nope'): # see above
            return False # see above

        # you're iterating for number of times input will be asked
        # if input is in neither tuple; you could use `retries -= 1` as well
        retries = retries - 1 

        # if you're out of retries, you raise an error and break out of the method
        if retries < 0:
            raise ValueError('invalid user response')
        # this prints the reminder if the earlier conditions aren't met
        # and till you're out of retries
        print(reminder)

沒有輸出,因為你已經定義了一個方法但沒有調用它。如果你在傳遞一些參數的同時進行方法調用,它將返回一個布爾值或打印提醒,直到你重試結束(當它將引發 ValueError)。

>>ask_ok("Enter Input: ")
# Enter Input: no
# False

>>ask_ok("Enter your Input: ")
# Enter your Input: y
# True

>>ask_ok("Input: ", 2, "Try Again")
# Input: FIFA
# Input: World
# Input: Cup
# Try Again

暫無
暫無

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

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