Python 2.7上的假日应用程序
我试着解决这些作业,但我被卡住了: 您需要创建四个函数:Python 2.7上的假日应用程序,python,python-2.7,Python,Python 2.7,我试着解决这些作业,但我被卡住了: 您需要创建四个函数: Hotel cost-此函数将夜数作为参数,并返回总成本(您可以选择每晚的价格) 飞机成本-此函数将以您要飞往的城市作为参数,并返回航班成本(提示:在函数中使用if/else if语句根据所选城市检索价格) 租车-此函数将天数作为参数,并返回总成本 假日成本-此函数将包含三个参数:夜数、城市和天数 使用这三个参数,您可以使用各自的参数调用上述所有三个函数,并最终返回假期的总费用 打印出假期函数的值以查看结果 尝试使用不同组合的应用程序以显
def hotel_cost(nights):
return nights * 875
def plane_cost(city):
ticket = 0
while city != 4:
if city == '1':
ticket = 750
break
elif city == '2':
ticket = 850
break
elif city == '3':
ticket = 600
break
elif city == '4':
print 'You have selected an invalid option'
else:
print 'You have selected an invalid option'
def car_rental(days):
return days * 275
def holiday_cost(nights, city, days):
nights = hotel_cost(nights)
city = plane_cost(city)
days = car_rental(days)
return nights + city + days
hotel_cost(int(raw_input('How many nights will you be staying? ')))
plane_cost(raw_input('\n1. O.R. Tambo International\n2. Capetown International\n3. King Shaka International\nWhere you flying to? '))
car_rental(int(raw_input('How many days will you need a car for?: ')))
total = holiday_cost(nights, city, days)
print total
我得到的错误如下:
Traceback (most recent call last): File "C:\Users\user\Dropbox\Mengezi Dlomo-9897\intro to programming\Task 24\holiday.py", line 37, in <module> total = holiday_cost(nights, city, days) NameError: name 'nights' is not defined
Traceback(最近一次调用):文件“C:\Users\user\Dropbox\Mengezi Dlomo-9897\intro to programming\Task 24\holiday.py”,第37行,总计=holiday\u cost(nights,city,days)name错误:未定义名称“nights”
您已经在holiday\u cost中调用了其他3个函数。你不需要给他们打多次电话
此外,我还做了一些其他有益的更改,如while city>3:
而不是while city!=4:
in-plane\u cost(),我在plane\u cost()的末尾添加了一行return ticket
此外,该行:
elif city==“4”:
打印“您选择了无效选项”
由于city==“4”处于else
状态,因此不需要
以下是最终代码:
def hotel_cost(nights):
return nights * 875
def plane_cost(city):
ticket = 0
while city > 3:
if city == '1':
ticket = 750
break
elif city == '2':
ticket = 850
break
elif city == '3':
ticket = 600
break
else:
print 'You have selected an invalid option'
return ticket
def car_rental(days):
return days * 275
def holiday_cost(nights, city, days):
nights = hotel_cost(nights)
city = plane_cost(city)
days = car_rental(days)
return nights + city + days
nights = int(raw_input('How many nights will you be staying? '))
city = raw_input('\n1. O.R. Tambo International\n2. Capetown International\n3. King Shaka International\nWhere you flying to? ')
days = int(raw_input('How many days will you need a car for?: '))
total = holiday_cost(nights, city, days)
print total
你写道:
total = holiday_cost(nights, city, days)
但是没有定义夜、城、日。您使用了这些名称来定义输入参数,但这并没有在使用这些名称的函数之外定义这些变量
换句话说
def someFunction(inputP):
#here inputP is defined
...
#her inputP is not defined
要返回问题,必须将返回值分配给这些变量:
nights = hotel_cost(int(raw_input('How many nights will you be staying? ')))
city = plane_cost(raw_input('\n1. O.R. Tambo International\n2. Capetown International\n3. King Shaka International\nWhere you flying to? '))
days = car_rental(int(raw_input('How many days will you need a car for?: ')))
nights=hotel_cost(int(raw_input('How many nights will you be staying? ')))
city=plane_cost(raw_input('\n1. O.R. Tambo International\n2. Capetown
International\n3. King Shaka International\nWhere you flying to? '))
days=car_rental(int(raw_input('How many days will you need a car for?: ')))
total = holiday_cost(nights, city, days)
由于在Python中很容易迷失方向,我建议在不同的范围内使用不同的变量名称:不要在hotel_cost
中使用nights
,然后在holiday_cost
中使用两次,然后在全局范围内使用另一次
干杯 您必须保存在变量中请求的值:
nights = hotel_cost(int(raw_input('How many nights will you be staying? ')))
city = plane_cost(raw_input('\n1. O.R. Tambo International\n2. Capetown International\n3. King Shaka International\nWhere you flying to? '))
days = car_rental(int(raw_input('How many days will you need a car for?: ')))
nights=hotel_cost(int(raw_input('How many nights will you be staying? ')))
city=plane_cost(raw_input('\n1. O.R. Tambo International\n2. Capetown
International\n3. King Shaka International\nWhere you flying to? '))
days=car_rental(int(raw_input('How many days will you need a car for?: ')))
total = holiday_cost(nights, city, days)
问候 有什么问题吗?这太宽泛了。holiday_cost给了我一个错误。错误:回溯(上次的最新调用):文件“C:\Users\user\Dropbox\Mengezi Dlomo-9897\intro to programming\Task 24\holiday.py”,第37行,总计=holiday_cost(夜晚、城市、白天)name错误:名称“nights”没有定义,没有定义,你把夜晚的数字直接读入一个函数。如果你想以后再使用它,你应该先保存它。你不是在存储用户输入的结果,例如,
nights=int(原始输入('raw_input('你要住几晚?'))
等等。我现在可以吻你了,兄弟。我讨厌与这些任务作斗争。这让我觉得自己像个白痴。谢谢总有一天我会成为一名优秀的程序员。顺便说一句,代码就像一个bomb@ZeeDhlomo哈哈,谢谢你的评论,你会看到的,我也是新来的。我刚学了几个月python,这是我的第一门语言。您的代码与答案相差不远,只需要做一些调整:)