2015-11-08 90 views
-1
duration=input("Enter day duration here:") 
amount=input("Enter the amount here:") 

def make_decision(duration,amount): 

    if (duration<=0 or amount <=0): 
     print "Invalid value" 
    elif (duration<30): 
     if (amount>=50): 
      corresponding_value = "Medium" 

     elif (amount<50): 
      corresponding_value = "Low" 

    elif (duration>=30): 
     if (amount>=50): 
      corresponding_value = "High" 
     elif (amount<50): 
      corresponding_value = "High" 

    return corresponding_value 

make_decision(duration,amount) 

我不明白为什么请帮助!当我运行这个,它只是在获得输入后结束。 我修了不算帐间隔,但还是一样的!为什么我的函数没有返回值?

+3

也许你想'打印make_decision(持续时间,量)'? – k4yaman

+0

'else'里面的'if'在两种情况下都有相同的结果,因此会显得多余。 (它的'elif'条件必然是真的,所以会比'else'更好。) – tripleee

回答

1

您的函数正在返回值,但您错过的是当您从函数返回值时,您必须在某个变量中收集该值并相应地使用它,或者您可以使用print语句将其打印出来函数调用之前。

更新后的代码如下:

duration=input("Enter day duration here:") 
amount=input("Enter the amount here:") 

def make_decision(duration,amount): 

    if (duration<=0 or amount <=0): 
     print "Invalid value" 
    elif (duration<30): 
     if (amount>=50): 
      corresponding_value = "Medium" 

     elif (amount<50): 
      corresponding_value = "Low" 

    elif (duration>=30): 
     if (amount>=50): 
      corresponding_value = "High" 
     elif (amount<50): 
      corresponding_value = "High" 

    return corresponding_value 

# this is one option 
print make_decision(duration,amount) 
# another option could be 
# result = make_decision(duration, amount) 
# print result 
1

由于塔伦描述,你的函数返回一个值,但不能打印。使用

print corresponding_value 

而不是返回。因此,当您使用

make_decision(duration,amount) 

调用该功能时,您将获得打印在控制台上的输出。

做的另一种方式是

print make_decision(duration,amount) 

而且还有其他办法也。

而且,在你的函数

if (amount>=50): 
     corresponding_value = "High" 
elif (amount<50): 
     corresponding_value = "High" 

大概的第二部分,有一些错字错误。因为这两种情况都打印相同的文本。如果您需要在持续时间> = 30且金额> 0时打印相同的文字'高',则不需要elif条件。写的更好的方法是:

def make_decision(duration,amount): 

if (duration<=0 or amount <=0): 
    print "Invalid value" 
elif (duration<30): 
    if (amount>=50): 
     corresponding_value = "Medium" 

    elif (amount<50): 
     corresponding_value = "Low" 
else: 
     corresponding_value = "High" 
print corresponding_value  

make_decision(持续时间,量)

相关问题