简体   繁体   中英

How to assign a value from one list to a string in the other

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

So now a city is added to the list and so is the distance. But I don't know how to make it so when a city is chosen, the distance associated with it is also used in the equation without manually picking it. There is a $100 starting cost and $0.5 for every km after.

while flight == False:
                if choice in City:
                    print ('You have chose to fly to: ',choice)
                    print('Please wait while we determine your flight path')

the statement below will only take the most recent distance entered and calculate but not the distance associated with the city entered.

 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

For example I might enter Toronto and 400 km, it will calculate the cost correctly of $300. But then if I add another city with a distance of 600km to the list, and choose to calculate Toronto again then it will use the distance of 600 and not the distance of 400 that was entered with it. So basically how can make it so when a city is called upon, it calculates the distance entered with it and not the most recent distance entered?

My best suggestion would be to use a dictionary. These store values within keys.

keys = {"Montreal":250}

When you call keys["Montreal"] , you receive the value of 250.

To create a new one, you can use keys["Toronto"] = 500

Your end result: keys = {"Toronto":500,"Montreal":250}

You can then use these values in your program whenever you need them, as long as you call them.

Use dictionary

data = {} 

and put info

data['Toronto'] = 400

data['Another'] = 600 

and get info

print(100 + data['Toronto'] * 0.5)

print(100 + data['Another'] * 0.5)

BTW: you can also create 2-dimensional dictionary - with distances [from][to] ,

data = {}

data['Toronto'] = {}
data['Toronto']['Another'] = 400
data['Toronto']['Other'] = 600

data['Another'] = {}
data['Another']['Other'] = 1000

print(100 + data['Toronto']['Another'] * 0.5)

print(100 + data['Another']['Other'] * 0.5) 

The issue with your program is that in your distance calculation you are using the km variable, which was most recently updated in the input() asking for the distance to the city. Instead, you need to find what place the city is in the City list, and use the correct value from the Distance list.

One nice way you can find where an item is in a list is by using the .index() method. Therefore your code for the calculation becomes:

i = City.index(choice)
d = Distance[i]
print ("Your cost to travel is", 0.5*d+100)

Hope this helps!

To store distances associated with cities, it would be convenient to use an associative array . In Python, these are called " dictionaries ".

Instead of

 Distance = [] ... City.append(Canada_city) ... Distance.append(km) 

do

Distance = {}
...
Distance[Canada_city] = km

And then you get the distance associated with the city choice by

Distance[choice]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM