简体   繁体   中英

What should I do at that point in Python?

I'm trying to learn Python on python.org

I found this code and I don't know what it means. I ran it but nothing happened.

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)

So, what does this code do?

Here function is defined ask_ok(), which takes input prompt . so as below you can run this code in any of the python IDE you have.

Line 1 will call the function with prompt = "do you want to continue" (any message you can write here). after the function is called it enters into loop, which will check if input is ('y', 'ye', 'yes') then it will retrun TRUE OR if input is 'n', 'no', 'nop', 'nope' then it will return false BUT if the input is anything other than ('y', 'ye', 'yes', 'n', 'no', 'nop', 'nope') values then loop will continue and print reminder="Please try again!"

If you will see the loop

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

till 5 times loop will allow you to enter input 6th time it will throw exception ValueError('invalid user response') .

the loop will continue maximum 5 times)

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("do you want to continue") #line 1

for practice you can change values in function definition. I would suggest first go through basics like if conditions, loops, exception, functions then you will be better to keep going.

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

There is no output because you've defined a method but not called it.If you follow it up with a method call while passing some arguments, it will return a Boolean value or print the reminder till you're out of retries (when it will raise the 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

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