Anvit
Anvit

Reputation: 228

Program Returning nothing

I saw this Flowchart and decided to make a program out of it. The problem is, it only returns "Go Outside" if I enter "no" the first time. All others return "None". Im using Python 2.7

def waitawhile():
    print "Wait a while"
    rain2 = raw_input("Is it still raining?")
    if rain2.lower() == "no":
        return "Go Outside"
    elif rain2.lower() == "yes":
        waitawhile()
def Raining():
    print "Is it raining?"
    rain = raw_input()
    if rain.lower() == "no":
        return "Go Outside"
    elif rain.lower() == "yes":
        print "Have Umbrella?"
        umbrella = raw_input()
        if umbrella.lower == "yes":
            return "Go Outside"
        elif umbrella.lower() == "no":
            waitawhile()
print Raining()

Upvotes: 0

Views: 55

Answers (2)

EbraHim
EbraHim

Reputation: 2359

Your program have three problem, in the following they are fixed:

def waitawhile():
    print "Wait a while"
    rain2 = raw_input("Is it still raining?")
    if rain2.lower() == "no":
        return "Go Outside"
    elif rain2.lower() == "yes":
        return waitawhile()
def Raining():
    print "Is it raining?"
    rain = raw_input()
    if rain.lower() == "no":
        return "Go Outside"
    elif rain.lower() == "yes":
        print "Have Umbrella?"
        umbrella = raw_input()
        if umbrella.lower() == "yes":
            return "Go Outside"
        elif umbrella.lower() == "no":
            return waitawhile()
print Raining()

Works as below:

>>> ================================ RESTART ================================
>>> 
Is it raining?
yes
Have Umbrella?
yes
Go Outside
>>> ================================ RESTART ================================
>>> 
Is it raining?
yes
Have Umbrella?
no
Wait a while
Is it still raining?yes
Wait a while
Is it still raining?no
Go Outside
>>>

The problems in your program are logical error, so the interpreter will not show you a syntax error:

  1. You used .lower instead of .lower() in the if condition, and it will be always false.
  2. You ignored returns of waitawhile() method while you must return them to print method.

Upvotes: 0

Tom Karzes
Tom Karzes

Reputation: 24102

The problem is with your calls to waitawhile (from both Raining and from waitawhile itself). After calling it, you're discarding the return value and returning nothing. To fix it, change the calls from:

waitawhile()

to:

return waitawhile()

Make sure that, for both functions, there is no way to reach the end of the function without executing a return statement.

Upvotes: 2

Related Questions