JavaScript
x
17
17
1
season_num = [1, 2, 3, 4]
2
3
if not season_num:
4
input('Please enter a valid num: ')
5
else:
6
7
for i in season_num:
8
input('give a num: ')
9
if i == 1:
10
print('Spring')
11
elif i == 2:
12
print('Summer')
13
elif i == 3:
14
print('Fall')
15
else:
16
print('Winter')
17
Advertisement
Answer
What you’re probably trying to do is this:
JavaScript
1
12
12
1
seasonNum = input('give a num: ')
2
if seasonNum == "1":
3
print('Spring')
4
elif seasonNum == "2":
5
print('Summer')
6
elif seasonNum == "3":
7
print('Fall')
8
elif seasonNum == "4":
9
print('Winter')
10
else:
11
print('invalid selection')
12
However, a better way to do this is to use a dictionary:
JavaScript
1
10
10
1
seasons = {
2
"1": "Spring",
3
"2": "Summer",
4
"3": "Fall",
5
"4": "Winter"
6
}
7
seasonNum = input('give a num: ')
8
selectedSeason = seasons.get(seasonNum)
9
print(selectedSeason or "invalid selection")
10