Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Assigning a Variable Help
#1
Hi - I'm new to Python so this is probably a dumb question

Lets say I have variables months of the year that looks like this
month[temperature,days in month, precipitation]

Jan[25,31,3]
Apr[45,30,4]
Jun[85,30,3.3]
Sep[70,30,3.8]
Dec[35,31,2.8]

My code is to input what month you are working with:

month = input("What month is it? ")

Lets say I enter 'Sep'. How do I assign he September values to the variable month so when I type print(month) it returns:

70,30,3.8

Thanks for any help! Much appreciated!
Reply
#2
You should choose appropriate datastructure like dictionary for storing month’s data (not separate variables for each month.
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.
Reply
#3
Thanks that helped point me in the right direction.

temp = {'MonthOfYear' : {"Jan" , "Feb" , "Mar"} , 'Temperature' : {"25" , "35" , "45"} , 'Days' : {"31" , "28" , "31"} , 'Precipitation' : {"3" , "4" , "5"} }

month = input("What month is it? ")


for var in temp:
    for value in temp[str(var)]:
        if str(value) == str(month):
            print(str(value))
Now this what I have. If I type 'Feb' , how does that return "Feb,35,28,4"? I think I'm still missing something.
Reply
#4
A better datastructure is this
temp = {'Jan' : [25,31,3] , 'Apr' : [45,30,4] , 'Jun' : [85,30,3.3] , 'Sep' : [70,30,3.8], 'Dec':[35,31,2.8] }
 
month = input("What month is it? ")
print(temp[month])
Reply
#5
Some further convenience can be added to vmarg code. It makes querying data more readable:

>>> months = {'Jan' : [25,31,3] , 'Apr' : [45,30,4] , 'Jun' : [85,30,3.3] , 'Sep' : [70,30,3.8], 'Dec':[35,31,2.8] }
>>> names = ['temperature', 'days in month', 'precipitation']
>>> for month in months:
...     months[month] = dict(zip(names, months[month]))
...
>>> months
{'Jan': {'temperature': 25, 'days in month': 31, 'precipitation': 3},
 'Apr': {'temperature': 45, 'days in month': 30, 'precipitation': 4},
 'Jun': {'temperature': 85, 'days in month': 30, 'precipitation': 3.3},
 'Sep': {'temperature': 70, 'days in month': 30, 'precipitation': 3.8},
 'Dec': {'temperature': 35, 'days in month': 31, 'precipitation': 2.8}}
>>> monthts['Sep']['temperature']
70
>>> max(months, key=lambda x: months[x]['temperature'])                  # month with maximum temperature
'Jun'
>>> min(months, key=lambda x: months[x]['precipitation'])                # month with minimum precipitation
'Dec'
>>> [month for month in months if 50 <= months[month]['temperature']]    # months where temperature was equal or higher than 50
['Jun', 'Sep']
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.
Reply
#6
Thanks so much!!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Assigning a new value to variable uriel 1 1,571 Dec-04-2021, 02:59 PM
Last Post: Underscore
  assigning a variable :( gr3yali3n 0 1,290 Sep-22-2020, 09:02 PM
Last Post: gr3yali3n
  Assigning variables Godserena 4 2,135 Apr-26-2020, 06:59 AM
Last Post: buran
  Assigning an item from a list xlev 1 1,429 Sep-27-2019, 04:42 PM
Last Post: Larz60+
  assigning index 3Pinter 6 2,949 Jan-18-2019, 10:07 PM
Last Post: nilamo
  Not adding and assigning? 00712411 7 4,087 Oct-10-2018, 07:11 PM
Last Post: volcano63
  Assigning iter_row value to variable ankey 8 8,950 Sep-24-2018, 03:51 PM
Last Post: ankey
  Assigning a new variable in a IF loop pythoneer 5 3,738 Mar-02-2018, 05:21 AM
Last Post: pythoneer
  Assigning to string slice michaeljhuman 1 2,731 Feb-08-2018, 12:57 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020