掌握初级Python知识并尝试找出其中的逻辑。我的经验是SQL,所以很难摆脱这种思路。你知道吗
我想让用户从列表中选择一架飞机,并有程序采取相应的代码拉每分钟的价格。你知道吗
我试过不同的变体。你可以看到我如何设置我的列表的区别,试图看看问题是否存在。你知道吗
aircraft = {'1':'Fighter Jet','2':'Airliner','3':'Bi-Plane','4':'Aerobatic Plane'}
ppm = {'Fighter Jet':'2.25','2':1.75,'3':'1.00','4':1.50}
def flightcost(time, aircraft):
y = time
x = ppm.get(aircraft.code)
return x * y
time = input('\nHow many minutes would you like to fly? Please enter 5 - 60 numbers only! \n')
print('\nHere are the aircraft simulators we have: \n')
for code,airplane in aircraft.items():
print('{} {}'.format(code, airplane))
aircraft = input('\nType in the numeric code for the plane you prefer? \n')
print('\nHere is your flight cost: ', flightcost)```
Expected results if a user wants to fly for 10 minutes in the Fighter Jet Simulator:
'time = 10 minutes'
'aircraft = 1:Fighter Jet'
'ppm = 1:2.25'
'flightcost = time (10 minutes) * ppm (2.25)'
'flightcost = 22.50'
Results right now: '<function flightcost at 0x00845150>'
(The code on the end is always different depending on what I've entered)
Something is happening. Just not what I am expecting!!!
flightcost
是一个函数,您正在尝试打印它。 您实际想要打印的是这个函数的返回值,因此您应该调用它并将适当的参数传递给它:print('\nHere is your flight cost: ', flightcost(time, aircraft))
。你知道吗不过,您的代码还有一些问题,我修复了其中几个问题:
在我修复的问题中:
ppm
dict的键中混合了飞机名称和idppm
dict中混合了值的类型,它们都应该是float,而不是string。你知道吗aircraft.code
不存在,应将飞机代码作为参数传递给flightcost
函数。你知道吗print
,以防止将输出显示为元组。你知道吗相关问题 更多 >
编程相关推荐