Home > Software engineering >  How to have a user pull data from lists and display it cleanly in output?
How to have a user pull data from lists and display it cleanly in output?

Time:03-29

I have a table containing one year of monthly average temperatures given, and I need to develop some python code that allows a user to select a month and have the selected month and corresponding monthly average temperature printed to the screen in an output like this:

The average temperature in New York in March is 10.0 degrees Celsius

(Note: I am omitting the table, but the lists are typed in the correct order) This is what I have so far:

#variable used to set the selected month (should be index value)
month_index = input("Choose the index value of the month you would like to look at:")

#Lists containing data
month = ["January", "Febuary", "March", "April", "May", "June", "July", "August",
         "September", "October", "November", "December"]
avg_temp = [4.6, 6.1, 10.0, 17.0, 23.0, 27.0, 30.0, 29.0, 25.2, 19.0, 12.7, 7.2]

I tried this statement which works, but not with user input:

print_statement = (f"The average temperature in New York in {month[2]} "
                   f"is {avg_temp[2]} degrees Celsius")

I was also thinking that zipping the lists may be needed to connect values in both columns like this:

month_index = sorted(zip(month, avg_temp)

I messed up the code and cant rememer exactly what I wrote, but basically the resulting output was:

The average temperature in New York in (December, 7.2)

Notice I had to leave out the "is x degrees Celsius" at the end and it isn't a nice clean sentence.

CodePudding user response:

You need to convert the input to an integer first, and then use it in your print statement as follow:

#variable used to set the selected month (should be index value)
month_index = int(input("Choose the index value of the month you would like to look at:"))

#Lists containing data
month = ["January", "Febuary", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
avg_temp = [4.6, 6.1, 10.0, 17.0, 23.0, 27.0, 30.0, 29.0, 25.2, 19.0, 12.7, 7.2]

print(f"The average temperature in New York in {month[month_index]} is {avg_temp[month_index]} degrees Celsius")

Alternatively, you could use a dictionnary, and ask for a month name instead of an index:

#variable used to set the selected month (should be index value)
month_input = input("Choose the month you would like to look at: ")

# List containing data
month = ["January", "Febuary", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
avg_temp = [4.6, 6.1, 10.0, 17.0, 23.0, 27.0, 30.0, 29.0, 25.2, 19.0, 12.7, 7.2]

# Converting to a dict
temp_per_month = {month[i]:avg_temp[i] for i in range(len(month))}

print(f"The average temperature in New York in {month_input} is {temp_per_month[month_input]} degrees Celsius")

Finally, you can zip if you want, but here do not sort since that will sort the entries by alphabetical order (so August will be first, September last instead of January first and December last).

#variable used to set the selected month (should be index value)
month_index = int(input("Choose the index value of the month you would like to look at:"))

#Lists containing data
month = ["January", "Febuary", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
avg_temp = [4.6, 6.1, 10.0, 17.0, 23.0, 27.0, 30.0, 29.0, 25.2, 19.0, 12.7, 7.2]

month_temps = zip(month, avg_temp)

print(f"The average temperature in New York in {month_temps[month_index][0]} is {month_temps[month_index][1]} degrees Celsius")

CodePudding user response:

You mentioned that "I tried this statement which works, but not with user input". Did you convert the user-input to integer before using it as a list index?

Basically, just try this:

month_index = int(input("Choose the index value of the month you would like to look at:"))
  • Related