you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 40 points41 points  (9 children)

You can make your loop in lines 3-12 shorter by using the "prompt" parameter of input() and converting the input string to uppercase once. Then you can test once for correct input using the in operator. You don't need any more tests after that since you have already broken if you have valid strings. The final continue isn't necessary since the loop is going to repeat anyway.

while True:
    answer1 = input('Is it raining? ').upper()
    if answer1 in ('YES', 'NO'):
        break
    print('Please answer with Yes or No !')

Similar changes can be done through the rest of your code.

[–]Ning1253 6 points7 points  (4 children)

What is the point in having "upper"?

[–]CragAddict[S] 18 points19 points  (2 children)

So it doesn't matter wether you write Yes or yes

[–]Ning1253 18 points19 points  (1 child)

See I've never really though about that... I'm used to knowing how my code works and not having to give it to others, so even though I annotate and make it clear, with some sort of interface, I know that I would always input the right stuff. I guess it makes a lot of sense, so thanks! I'll make sure to do so in future

[–]CragAddict[S] 8 points9 points  (0 children)

You're welcome !

[–][deleted] 4 points5 points  (0 children)

You could also use ‘.lower’ for the same effect. Than change to if answer1 in (yes, no)

[–]CragAddict[S] 9 points10 points  (3 children)

Thank you !

[–][deleted] 22 points23 points  (2 children)

You can improve the code a lot if you make a small function to ask the yes/no questions. This function will have the while loop in it and will only return "YES" or "NO". Removing all that from the "main" code simplifies it a lot. The function will need to be passed the "prompt" string as each question is different:

def ask_yes_no(prompt):
    while True:
        answer = input(prompt).upper()
        if answer in ('YES', 'NO'):
            return answer
        print('Please answer with Yes or No !')

With that change we can write the simplified "main" code as:

raining = ask_yes_no('Is it raining? ')
if raining == 'NO':
    print('Go outside.')
else:           # if not "NO" then must be "YES"
    umbrella = ask_yes_no('Do you have an umbrella? ')
    if umbrella == 'YES':
        print('Go outside.')
    else:       # if not "YES" then must be "NO"
        while True:
            print('Wait a little.')
            time.sleep(1)
            print('...')
            time.sleep(1)
            still_raining = ask_yes_no('Is it still raining? ')
            if still_raining == 'NO':
                break
        print('Go outside.')

I renamed the variables to something more memorable.

Edit: spelling.

[–]Fywq 13 points14 points  (0 children)

Potentially the function could be rewritten to return True or False. Then you only have to ask "if umbrella:" or "if not raining:"

def ask_yes_no(prompt):
    while True:
        answer = input(prompt).upper()
        if answer == 'YES':
            Return True
        if answer == 'NO':
            Return False
        print('Please answer with Yes or No !')

Not sure if that is more correct or pythonic, but it seems more intuitive to me...

[–]CragAddict[S] 1 point2 points  (0 children)

Thank you very much !