Question

(PYTHON ONLY plz!) I'm having trouble getting the longest city in my program (International Falls) to...

(PYTHON ONLY plz!) I'm having trouble getting the longest city in my program (International Falls) to be exactly on space away from the state abbreviation in the table:

My Program:

def main():

mycities = ['Cape Girardeau, MO 63780','Columbia, MO 65201',

'Kansas City, MO 64108','Rolla,MO 65402',

'Springfield, MO65897','St Joseph, MO64504',

'St Louis,MO63111', 'Ames, IA 50010 ', 'Enid, OK 73773',

'West Palm Beach, FL 33412',

'International Falls, MN 56649',

'Frostbite Falls,MN 56650 '

]

# Print the table

for c in mycities:

entry = c.split(",")

if (len(entry) == 2):

city = entry[0].strip()

temp = entry[1].strip()

else:

city = c[0:15].strip()

temp = c[1:0].strip()

state = temp[0:2]

zipCode = int(temp[-5::])

print ("%-20s\t%s\t%d"%(city, state, zipCode))

The program result needs to line up like this:

All cities left aligned with the state abbreviation column starting one space after International Falls

Cape Girardeau      MO 63780
Columbia            MO 65201
Kansas City         MO 64108
Rolla               MO 65402
Springfield         MO 65897
St Joseph           MO 64504
St Louis            MO 63111
Ames                IA 50010
Enid                OK 73773
West Palm Beach     FL 33412
International Falls MN 56649
Frostbite Falls     MN 56650

Please help! Thx!

0 0
Add a comment Improve this question Transcribed image text
Answer #1
SOURCE CODE:

*Please follow the comments to better understand the code.

**Please look at the Screenshot below and use this code to copy-paste.

***The code in the below screenshot is neatly indented for better understanding.


def main():
    mycities = ['Cape Girardeau, MO 63780', 'Columbia, MO 65201',
                'Kansas City, MO 64108', 'Rolla,MO 65402',
                'Springfield, MO65897', 'St Joseph, MO64504',
                'St Louis,MO63111', 'Ames, IA 50010 ', 'Enid, OK 73773',
                'West Palm Beach, FL 33412',
                'International Falls, MN 56649',
                'Frostbite Falls,MN 56650 ']

    # add all data to lists
    cities = []
    states=[]
    zipcodes=[]

    # Print the table
    for c in mycities:
        entry = c.split(",")
        if len(entry) == 2:
            city = entry[0].strip()
            temp = entry[1].strip()
        else:
            city = c[0:15].strip()
            temp = c[1:0].strip()
        state = temp[0:2]
        zipCode = int(temp[-5::])

        # add to lists
        cities.append(city)
        states.append(state)
        zipcodes.append(zipCode)
    # find longest length
    longest_length_city=''
    for city in cities:
        # update the longest length
        if len(city) > len(longest_length_city):
            longest_length_city = city
    #print(longest_length_city)
    # print data
    for city, state, zipCode in zip(cities, states, zipcodes):
        # check if the city is longest length
        if len(city) == len(longest_length_city):
            print("%s %s\t%d" % (city, state, zipCode))
        else:
            print("%-20s\t%-s\t%d" % (city, state, zipCode))


main()

========

OUTPUT: We can see that International Falls has the longest length and only one space is printed.

CODE:

Add a comment
Know the answer?
Add Answer to:
(PYTHON ONLY plz!) I'm having trouble getting the longest city in my program (International Falls) to...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Question 2: How do the average credit scores of people living in various cities in the...

    Question 2: How do the average credit scores of people living in various cities in the US differ? The file Credit Score Data of 143 American cities is provided in Canvas. Construct a histogram Create a Five Summary Report Calculate Mean, Variance and Standard Deviation   What conclusion can you reach concerning the average credit scores of people living in different American cities? City State Average Credit Score Detroit Mi 743 New York NY 762 Minneapolis MN 787 Hartford CT 774...

  • CPS 276 DOC02-- Assignment 02 Cost of Living Calculator Your client maintains job search website. As...

    CPS 276 DOC02-- Assignment 02 Cost of Living Calculator Your client maintains job search website. As a service on their website, they need an application that compares costs of living between different metropolitan areas. Client will pay $780 for a complete application. Background Information: A person living in an area with a high cost of living will need higher income to maintain the same standard of living as someone living in an area with a low cost of living. For...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT