2017-04-19 54 views
-2

所以我有这样的代码:如何使用户定义函数变量?

def hotel_cost(nights): 
    return 140 * nights 

def plane_ride_cost(city): 
    if city == "Charlotte": 
     return 183 
    elif city == "Tampa": 
     return 220 
    elif city == "Pittsburgh": 
     return 222 
    elif city == "Los Angeles": 
     return 475 

def rental_car_cost(days): 
    cost = 40 * days 
    if days >= 7: 
     cost -= 50 
    elif days >= 3: 
     cost -= 20 
    return cost 

def trip_cost(city, days, spending_money): 
    return plane_ride_cost(city) + rental_car_cost(days) + hotel_cost(days) + spending_money 

print (trip_cost("Tampa", 2, 200)) 

我知道我需要使用输入(),使所有4个变量(夜,城市,天,spending_money)由用户定义。但究竟如何?

+1

看看了'input'功能一开始:-) –

+0

我做到了已经。我在每个函数定义之前都放了4个'输入',但是我仍然不知道在代码结尾输入什么...... – icelandico

回答

2

尝试此启动:

a = input("Town:\n") 
b = int(input("Days:\n")) 
c = int(input("Money:\n")) 

print (trip_cost(a, b, c)) 
+0

这就是要点。最后一行解决了我的问题。非常感谢 ! – icelandico