- The issue is, user_dayis a string when entered as an input.
- Because it's a string, it doesn't meet any of the conditions, and therefore, day_timeremains undefined.
- Wrap the input in a try-exceptblock to check that the input is a proper data type. In this case,strtype that can be converted to anintorfloat.
 
- It must be converted to a intorfloatdepending on if you're going to accept only hours or fractional hours.
 
- user_dayshould be in a- while Trueloop to continue asking for a time until a valid numeric input is received.
- Finally, the code can be written more efficiently, by properly ordering the conditions from smallest to largest, for this case.
- user_daywill trickle down to the correct condition.
 
while True:
    try:
        user_day = int(input("What's the hour on a 24-hour scale? "))
    except ValueError:  # checks for the correct data type; maybe someone will spell the hour
        print('Please enter the hour as a numeric value')
    
    if (user_day >= 24) or (user_day < 0):  # 24 and over or less than 0 are invalid times
        print('Please enter a valid time')
    else:
        break  # break when there's valid input
if user_day < 12:
    day_time = 'Morning'
elif user_day < 17:
    day_time = 'Noon'
elif user_day < 20:
    day_time = 'Evening'
else:
    day_time = 'Night'
    
print(day_time)
- Return the indices of the bins to which each input value belongs.
- Use the value returned by np.digitizeto index the correct value fromday_time
import numpy as np
while True:
    try:
        user_day = int(input("What's the hour on a 24-hour scale? "))
    except ValueError:  # checks for the correct data type; maybe someone will spell the hour
        print('Please enter the hour as a numeric value')
    
    if (user_day >= 24) or (user_day < 0):  # 24 and over or less than 0 are invalid times
        print('Please enter a valid time')
    else:
        break  # break when there's valid input
day_time = ['Morning', 'Evening', 'Noon', 'Night']
idx = np.digitize(user_day, bins=[12, 17, 20])
    
print(day_time[idx])