要求用户从列表中输入一个值,并使用该值从另一个列表中提取

2024-04-29 09:25:26 发布

您现在位置:Python中文网/ 问答频道 /正文

掌握初级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!!!

Tags: thein列表fortimeiscodeprint
2条回答
#Below code should work, made 3 edits
aircraft = {'1':'Fighter Jet','2':'Airliner','3':'Bi-Plane','4':'Aerobatic Plane'}
#edit-1, corrected the dictionary ppm, should be using 1 as key and value should be a number
ppm = {'1':2.25,'2':1.75,'3':'1.00','4':1.50}

def flightcost(time, aircraft):
    #edit -2 , as value in ppm is float and we cannot multiply int and float, converted minutes to float. Also it is ppm.get(aircraft) but not ppm.get(aircraft.code)
    y = float(time)
    x = ppm.get(aircraft)
    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 precmdferfer? \n')
#edit-3 - calling flightcost directly doesn't makes any sense, you should pass the parameters required
print('\nHere is your flight cost: ', flightcost(time,aircraft))

flightcost是一个函数,您正在尝试打印它。 您实际想要打印的是这个函数的返回值,因此您应该调用它并将适当的参数传递给它:print('\nHere is your flight cost: ', flightcost(time, aircraft))。你知道吗

不过,您的代码还有一些问题,我修复了其中几个问题:

#!/usr/bin/python2                                                                                

aircraft = {1: 'Fighter Jet', 2: 'Airliner', 3: 'Bi-Plane', 4: 'Aerobatic Plane'}
ppm = {1: 2.25, 2: 1.75, 3: 1.00, 4: 1.50}

def flightcost(time, aircraft_code):
    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: ' + str(flightcost(time, aircraft)))

在我修复的问题中:

  • 您在ppmdict的键中混合了飞机名称和id
  • 您在ppmdict中混合了值的类型,它们都应该是float,而不是string。你知道吗
  • aircraft.code不存在,应将飞机代码作为参数传递给flightcost函数。你知道吗
  • 将调用调整为最终的print,以防止将输出显示为元组。你知道吗

相关问题 更多 >