Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 获取函数的用户输入_Python - Fatal编程技术网

Python 获取函数的用户输入

Python 获取函数的用户输入,python,Python,我正在尝试使用codeacdemy学习python。这是他们的运动之一。基本上,他们让我创建4个不同的函数来计算总成本。但是没有要求用户手动输入值的选项。所以,这就是我想说的。该代码直接指向归还租车成本部分。这只是我遇到麻烦的最底层 print "this code calculates the total price of a trip, using 4 functions" def hotel_cost(nights): return 140*nights def plane_r

我正在尝试使用codeacdemy学习python。这是他们的运动之一。基本上,他们让我创建4个不同的函数来计算总成本。但是没有要求用户手动输入值的选项。所以,这就是我想说的。该代码直接指向
归还租车成本部分。这只是我遇到麻烦的最底层

print "this code calculates the total price of a trip, using 4 functions"

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=days*40

    if (days>=7):
        cost -= 50

    elif(days>=3):
        cost -=20

    return cost

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

city= raw_input("enter city name")
days= raw_input("enter number of days staying")
spending_money= raw_input("enter spendig money")
print trip_cost(city,days, spending_money)
这是原始代码,它的符文非常好。我只想让用户在运行代码时输入值

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=days*40

    if (days>=7):
        cost -= 50

    elif(days>=3):
        cost -=20

      return cost

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

print trip_cost("Los Angeles",5,600)
平等问题:

提议 仅将其视为改进此代码的一部分。我想是的,它没有回答你的问题

我不知道Code Academy对该练习的建议是什么,但下面给出了一些更简单、更清晰的方法:

print "this code calculates the total price of a trip, using 4 functions"

def hotel_cost(nights):
    return 140 * nights

def plane_ride_cost(city):
    #So you can create dict and put for each city
    #Key - name of city
    #value - cost
    CITY_COST = {
        "Charlotte": 183,
        "Pittsburgh" : 222,
        "Los Angeles" : 475,
        "Tampa": "220"
    }
    #Method from dict 
    #if city doesn't exists it'll return False
    #The second param is default return if doesn't exist key into dict
    #you can change if do you want
    return CITY_COST.get(city, False)



def rental_car_cost(days):

    cost = days * 40

    if (days >= 7):
        cost -= 50

    elif(days >=3 ):
        cost -=20

    return cost

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

city= raw_input("enter city name")
days= raw_input("enter number of days staying")
spending_money= raw_input("enter spendig money")
print trip_cost(city,days, spending_money)
关于Dict的文档

尝试使用
int(原始输入(输入停留天数))
输入(“输入停留天数”)
而不是
原始输入(“输入停留天数”)
发生了什么?看到了什么不同吗?这是因为
原始输入()
将输入数据转换为字符串,但与
input()
不同。了解
input()
和raw\u input()之间的区别,以及随着python的发展它是如何变化的。我对代码做了一些更改,如下所示。它运行完美,没有错误。如果有帮助,请告诉我

print "this code calculates the total price of a trip, using 4 functions"

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=days*40

    if (days>=7):
        cost -= 50

    elif(days>=3):
        cost -=20

    return cost

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

city= raw_input("enter city name")
days= int(raw_input("enter number of days staying"))  ##### notice here
spending_money= int(raw_input("enter spendig money")) ### and here too
print trip_cost(city,days, spending_money)
您也可以使用下面的代码来代替上面的代码

################## without type casting#############
    city= raw_input("enter city name")
    days= input("enter number of days staying")  ##### notice something here
    spending_money= input("enter spendig money") ### and here 
    print trip_cost(city,days, spending_money)

顺便说一句,我知道我可以说打印旅行费用(“洛杉矶”,5100)这将给我答案。但我想在控制台中询问用户,而不必每次更改代码。感谢您提供的帮助,您需要修复缩进。我认为我的缩进适用于原始代码。我的代码运行无错误,并且我知道100%正确,直至退还租车费(天)+酒店费(天)+…正如这里显示的那样,它不是。我已经修复了它。您是否尝试按原样运行代码?按原样,它甚至不会编译。问题是代码没有运行或给出错误的结果?我相信您需要将原始输入调用的输出转换为float,以获得天数和支出输入。此代码具有与我编写的代码完全相同的问题:(.我无法找出我做错了什么,我现在理解得太多了。代码运行得很好,也很好。谢谢!!@user4081147欢迎!