''' This program allows users to add citites and distances
as well as calculating the cost of their flight.'''
City = []
Distance = []
Canada_city = ''
flight = False
while Canada_city != 'exit':
print('Type exit to quit\n')
Canada_city = input('Enter a Canadian city\n')
City.append(Canada_city)
if Canada_city == 'exit':
quit
else:
km = int(input('How far is the city from Ottawa in km?\n'))
Distance.append(km)
done = input('Would you like to save these cities & distances? y/n \n')
if done == 'y':
print ('Your final cities list is ',City)
print ('Your final distance list is ',Distance)
choice = input('What city would you like to travel to from your list\n')
所以現在一個城市被添加到列表中,距離也是如此。但我不知道如何做到這一點,所以選擇一個城市時,與其相關的距離也可以在方程中使用,而無需手動挑選它。起步費用爲100美元,每公里爲0.5美元。如何將一個列表中的值分配給另一個列表中的一個字符串
while flight == False:
if choice in City:
print ('You have chose to fly to: ',choice)
print('Please wait while we determine your flight path')
下面的語句只會輸入最近輸入的距離並計算輸入城市的相關距離。
print ("Your cost to travel is",0.5*km+100)
flight == True
break
Cost(choice)
else:
print ('City not found in your list')
choice = input('What city would you like to travel to from your list\n')
else:
continue
例如,我可能進入多倫多和400公里,它會正確計算成本300美元。但是,如果我再添加一個距離列表600公里的另一個城市,並選擇再次計算多倫多,那麼它將使用600的距離,而不是與它一起輸入的距離爲400。那麼基本上怎麼能這樣做,當一個城市被召喚時,它會計算輸入的距離而不是最近輸入的距離?
爲什麼不在兩個單獨的列表中保存城市和距離? – Vaishali
@VaishaliGarg可能是因爲他*不知道*? – mkrieger1
dictionary:'data = {}'put info'data ['Toronto'] = 400'; 'data ['Another'] = 600' and later get info'print(100 + data ['Toronto'] * 0.5)' – furas