【问题标题】:Asking user to enter one value from a list and using that value to pull from another list要求用户从列表中输入一个值并使用该值从另一个列表中提取
【发布时间】:2019-12-25 06:38:19
【问题描述】:

具备 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 in print('\nHere is your flight cost: ', flightcost):你需要把它改成flightcost()

标签: python list multipleselection


【解决方案1】:
#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))

【讨论】:

    【解决方案2】:

    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)))
    

    在我修复的问题中:

    • 您在ppm dict 的键中混合了飞机名称和ID。
    • 您在ppm dict 中混合了值的类型,它们应该都是浮点数,而不是字符串。
    • aircraft.code不存在,您应该将飞机代码作为参数传递给flightcost函数。
    • 调整了对最终 print 的调用,以防止将输出显示为元组。

    【讨论】:

    • 感谢您花时间修复我的代码!我试图展示我在设置列表方面尝试过的所有不同的事情。看到您的更改,我更清楚地知道将来应该如何设置。感谢您对此的帮助!
    猜你喜欢
    • 1970-01-01
    • 2022-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多