-1

Whenever I try to run my program it works but None is also being displayed after. Here is the code and the results I got.

def main():
    time = input("What time is it: ")
    converted_time = convert(time)
    print(converted_time)


def convert(time):
    hours,minutes = time.split(":")
    new_hours = float(hours)
    new_minutes = float(minutes)

    if new_hours >= 7 and new_hours <=8:
        print("Breakfast Time")
    elif new_hours >=12 and new_hours <=13:
        print("Lunch Time")
    elif new_hours >=18 and new_hours <=19:
        print("Dinner Time")
    else:
        print("")


if __name__ == "__main__":
    main()
What time is it? 7:00
answer: 
Breakfast Time
None


tried playing around with some of the print functions.
  • 3
    Does this answer your question? [What is the purpose of the return statement? How is it different from printing?](https://stackoverflow.com/questions/7129285/what-is-the-purpose-of-the-return-statement-how-is-it-different-from-printing). `convert(time)` doesn't return anything – Pranav Hosangadi Dec 27 '22 at 14:31
  • The function `convert` doesn't return any value, so when you capture the output of it using `converted_time = convert(time)`, you store the `None` that the function returns (by default). So when you issue `print(converted_time)`, you print `None`. – Hampus Larsson Dec 27 '22 at 14:31

2 Answers2

0

Your convert function has no return statement and therefore implicitly returns None. Therefore, None is the value assigned to converted_time, which you then print.

ndc85430
  • 1,395
  • 3
  • 11
  • 17
  • Basic questions like this have usually been asked and answered. Please don't answer duplicate questions. Instead, find the duplicate and flag/vote to close. See [answer] – Pranav Hosangadi Dec 27 '22 at 14:32
0

The reason is you're both printing in the convert function, as well as taking it's return value and printing it. Since you don't return anything from within the convert function, "None" is implicitly returned, and that is printed. You can fix this code by removing the print(converted_time) call

alielbashir
  • 315
  • 2
  • 9
  • Basic questions like this have usually been asked and answered. Please don't answer duplicate questions. Instead, find the duplicate and flag/vote to close. See [answer] – Pranav Hosangadi Dec 27 '22 at 14:33