0
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)
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • 1
    You are iterating over an empty list, so the loop will never run. – Paul M. Mar 03 '22 at 00:08
  • You're attempting to add a string and an integer together. Use `str(time)` rather than `time` on the RHS of the assignments? – BrokenBenchmark Mar 03 '22 at 00:08
  • Does this answer your question? [python adding number to string](https://stackoverflow.com/questions/11999228/python-adding-number-to-string) – duckboycool Mar 03 '22 at 00:09
  • @PaulM. - Ultimately, this is the question I'm trying to solve: Given the list time, with elements from 7 to 24, use a for loop to iterate over it and create a program that appends the string "Good morning" to result_time if the element in the list time is less than or equal to 12, appends "Good afternoon" if the element is grater than 12 and less than 20, and appends "Good night" otherwise. If my list is empty, would that mean, I should be using the list of time directly in the loop? – Jose Santos Mar 03 '22 at 00:19
  • "Given the list time, with elements from 7 to 24" - The 'time' variable you create is this list, which you are not iterating over. You are iterating over 'result_time', which is empty. There's nothing to iterate over so it doesn't. – Layne Bernardo Mar 03 '22 at 00:21

2 Answers2

1

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)
Hambagui
  • 23
  • 5
  • Thank you Hambagui! How would you declare the list of time elements to time before hand and then call it in the for loop? time = list(range(7,25)) result_time = [] for time in range(7, 25): – Jose Santos Mar 03 '22 at 00:26
1

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))
Theta
  • 53
  • 7