Skip to content
Advertisement

Hi: the following code that should prints a correspondng season based on the value presented on the variable season_num: the code not working properly [closed]

season_num = [1, 2, 3, 4]

if not season_num:
    input('Please enter a valid num: ')
else:

    for i in season_num:
        input('give a num: ')
        if i == 1:
            print('Spring')
        elif i == 2:
            print('Summer')
        elif i == 3:
            print('Fall')
        else:
            print('Winter')

Advertisement

Answer

What you’re probably trying to do is this:

seasonNum = input('give a num: ')
if seasonNum == "1":
    print('Spring')
elif seasonNum == "2":
    print('Summer')
elif seasonNum == "3":
    print('Fall')
elif seasonNum == "4":
    print('Winter')
else:
    print('invalid selection')

However, a better way to do this is to use a dictionary:

seasons = {
    "1": "Spring",
    "2": "Summer",
    "3": "Fall",
    "4": "Winter"
}
seasonNum = input('give a num: ')
selectedSeason = seasons.get(seasonNum)
print(selectedSeason or "invalid selection")
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement