Jose Santos
Jose Santos

Reputation: 1

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

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)

Upvotes: 0

Views: 141

Answers (2)

Theta
Theta

Reputation: 63

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))

Upvotes: 1

Hambagui
Hambagui

Reputation: 23

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)

Upvotes: 1

Related Questions