Home > OS >  I'm not sure why my for loop is not appending the necessary string when I run it?
I'm not sure why my for loop is not appending the necessary string when I run it?

Time:03-03

time = list(range(7,25))
result_time = []

for time in result_time:
    if time < 13:
        time = ("Good morning"   time)
        print(time)
    elif time > 12 and time < 20:
        time = ("Good afternoon"   time)
    elif time > 19:
        time = ("Good evening"   time)

CodePudding user response:

You need to iterate over the range of numbers and in each iteration concatenate the stringified time.

result_time = []
list_of_time = range(7, 25)

for time in list_of_time:
    if time < 13:
        time = ("Good morning "   str(time))
    elif time > 12 and time < 20:
        time = ("Good afternoon "   str(time))
    elif time > 19:
        time = ("Good evening "   str(time))
    result_time.append(time)

print(result_time)

CodePudding user response:

Your issue is iterating over an empty list. Instead, you should be iterating through the range you created.

Try this instead

time = list(range(7,25))

for t in time:
    if t <= 12:
        print('Good morning'   str(t))
    elif t > 12 and t < 20:
        print('Good afternoon '   str(t))
    else:
        print('Good night '   str(t))
  • Related