r/learnpython • • May 22 '21

Need some pointers.

Need some pointers for my "simple code

I know i dont have to use a function to get this to work. But is there a way i can get this code to exit by pressing enter?

def enter_number(a):
    if int(a) < 15:
        return "It's kind of chilly"
    elif int(a) > 20:
        return "It's a hot day"
    elif int(a) == 20:
        return "It's a mild day"
    elif len(a) == 0:
        return 

while True:
    userinput = input("Enter number:")
    print(enter_number(userinput))
1 Upvotes

5 comments sorted by

View all comments

Show parent comment

1

u/spez_edits_thedonald May 22 '21

while True: will produce a loop that runs forever, but it also doesn't make any sense conceptually.

We don't plan on running the loop forever, we plan on running it until the user tells us they're done. So I think it's more precise and more readable to store the state as not done yet, and then run the loop until we're done.

In short, while True: seems lazy to me, and storing a bool flag is cheap.

more discussion here, but it comes down to your opinion/style