程序什么都没有

时间:2016-04-23 10:56:43

标签: python python-2.7 windows-10

我看到了这个Flowchart,并决定用它制作一个程序。问题是,如果我第一次输入“no”,它只返回“Go Outside”。所有其他人都返回“无”。我正在使用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()

2 个答案:

答案 0 :(得分:2)

问题在于您拨打waitawhile(来自Rainingwaitawhile本身)。在调用之后,您将丢弃返回值并且不返回任何内容。要解决此问题,请更改以下来电:

waitawhile()

为:

return waitawhile()

确保对于这两个函数,如果不执行return语句,就无法到达函数的末尾。

答案 1 :(得分:0)

你的程序有三个问题,下面它们已修复:

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

如下工作:

>>> ================================ 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
>>>

程序中的问题是逻辑错误,因此解释器不会显示语法错误:

  1. 您在if条件中使用了.lower而不是.lower(),它将始终为false。
  2. 您必须将waitawhile()方法返回print方法,否则会忽略select * from db_cat where cat_id='10' order by cast(order_no as int) asc 方法的返回。