Posts: 10
Threads: 3
Joined: Nov 2019
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:
print(c)
main()
Posts: 479
Threads: 86
Joined: Feb 2018
We need some kind of context as to what the problem with your code is. I have no idea what this code is supposed to do, and it looks as if it will work perfectly.
Posts: 10
Threads: 3
Joined: Nov 2019
it should look like this I am confused on how to add and remove spaces as well as left and right adjust individual parts
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 F MN 56649
Frostbite Falls MN 56650
Posts: 479
Threads: 86
Joined: Feb 2018
Doing that will be a bit more complicated than the code that you currently have. I'll write and explain the code for you.
def main():
mycities = ['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'
]
formattedCities = []
comparisonCities = []
for city in mycities:
formattedCities.append(city.split(',')) #Each city is put in a list, and in that list, everything that was seperated by a comma, got split into different strings
comparisonCities.append(city.split(',')[0]) #The same thing here, but instead of keeping the list, the code just grabs the first string of the list, or the name part.
longestString = len(max(mycities)) #This is a number representing how many character long, the longest name is
# Print the table
for c in formattedCities:
print(c[0] + ' '*(longestString - len(c[0]) + 1) + '%s %s' %(c[1], c[2])) #The "c[0]" prints the first elements of the script, the name, then the spaces are calculated using the longest character length compared to the length of the name currently being printed. The plus one is for to make sure a space is printed when printing the longest name. Finally, the last two elements of the list, the state abbreviation and zip code, are printed separated by space
main() I hope you understand everything.
Posts: 10
Threads: 3
Joined: Nov 2019
Nov-19-2019, 03:01 AM
(This post was last modified: Nov-19-2019, 03:27 AM by Mike2607.)
I was wrapping my brain trying to figure out how to add and remove spaces. I didn't realize you can create list with in a single list and then combine the data later. that make way more sense that what I was trying to do. thanks for all the help.
if I wanted to do this with out adding commas or removing the spaces is this possible?
Posts: 12,036
Threads: 486
Joined: Sep 2016
or use:
def display_list(listname):
for element in listname:
items = element.split(',')
print(f"{items[0]:20} {items[1]:2} {items[2]:5}")
def main():
mycities = ['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'
]
display_list(mycities)
if __name__ == '__main__':
main() results:
Output: 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
Posts: 1,950
Threads: 8
Joined: Jun 2018
Nov-19-2019, 06:14 AM
(This post was last modified: Nov-19-2019, 06:15 AM by perfringo.)
(Nov-19-2019, 03:01 AM)Mike2607 Wrote: if I wanted to do this with out adding commas or removing the spaces is this possible?
I think that this assignment more about cleaning string than printing table. As there are strings which don't have spaces between state abbreviation and zip-code one should normalise them (split to chars and construct new string without spaces) and use index (all states have two-letter abbreviation):
for row in mycities:
city, state_and_zip = row.split(',')
location = ''.join(char for char in state_and_zip.split())
print(f'{city:20}{location[:2]:3}{location[2:]:10}') Output: 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
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
|