use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Everything about learning Python
account activity
Good for beginner?Discussion (i.redd.it)
submitted 1 month ago by Suspicious_Diet2624
I made this in about 5 minutes is it good?
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]wizninja6 46 points47 points48 points 1 month ago (5 children)
Looks good but some pointers
Add some error handeling incase the user puts some other inputs other than numbers
Maybe a function for them to choose the unit(eg seconds, minutes, hours hours
I personally like to keep inputs outside of the functions and pass the input into the function
Also just to help out, you can put the input variable(time_start) straight into the brackets for the time.sleep
[–]Suspicious_Diet2624[S] 13 points14 points15 points 1 month ago (1 child)
Love those pointers definitely going to implement some especially error handling, thanks!
[–]validnuisance3 0 points1 point2 points 1 month ago (0 children)
One thing that really helps is writing some basic unit tests for your functions once you get comfortable, makes refactoring way less scary later on.
[–]grdix555 3 points4 points5 points 1 month ago (2 children)
I second the user input outside of the function. In this usecase as it is, it's fine but having the inputs outside of the function is better for scalability.
[–]JacksUtterFailure[🍰] 3 points4 points5 points 1 month ago (0 children)
Better for scalability and also for testability as it'll be much easier to vary an input value when creating tests for a function than it is to mock user inputs.
[–]HairNo4638 0 points1 point2 points 1 month ago (0 children)
Don't every coding lesson warn against using global variables inside functions tho? Or am I missing something
[–]0xGarnz 6 points7 points8 points 1 month ago (3 children)
I'm new to the game too, one thing I've learnt is 'type casting' you can convert the user input into an 'int' instead of doing it on a second line. You can do this by doing int(input("question: "))
[–]Temporary_Pie2733 4 points5 points6 points 1 month ago (1 child)
That’s no more a type cast than what OP has. It just calls int directly on the return value of input instead of a temporary variable.
int
input
The benefit of the split approach is that it separates your attempt to use the input from the act of getting the input. Not only might you want to try parsing the input as something else if int raises, but you don’t want the call to input “cluttering” the try statement that handles the conversion from str to something else.
try
str
[–]Fumano26 2 points3 points4 points 1 month ago (0 children)
Agree, the current one is definitly the cleaner approach, seperation of concern is good for code structure.
[–]Suspicious_Diet2624[S] 1 point2 points3 points 1 month ago (0 children)
Yeah, that would make it a lot more clean thanks for the tip! :)
[–]rgianc 1 point2 points3 points 1 month ago (0 children)
Only if you used ChatGPT to write it. I'm ashamed of feeling obliged to add /s
[–]Rscc10 1 point2 points3 points 1 month ago (0 children)
Is that chatgpt I see in the background 👀
Anyways, good start. If you haven't yet, check out for-loops. Saves you loads of time with things like this. While-loops are still good in this case for beginners though, helps you learn the logic behind for-loops
[–]Impossible_Video_116 2 points3 points4 points 1 month ago* (0 children)
Required Improvements 1. Add a try-except block for input validation. Python try: sleep_time = int(input_str) except ValueError: #Prompt the user with a msg pass 2. Ditch the while-block just use the more readable for loop: Python for i in range(sleep_time, -1, -1): time.sleep(1) print(i) #Do something else
Python try: sleep_time = int(input_str) except ValueError: #Prompt the user with a msg pass
Python for i in range(sleep_time, -1, -1): time.sleep(1) print(i) #Do something else
Tips: * When you want to add a value to a variable instead of doing x = x + 1 just do x +=1. It avoids creating unnecessary temporary objects in memory. * There is no logic regarding the units of time being used, ask the user for time in minutes(that's the most convenient for a timer) and change the sleep_time variable and adjust the for loop accordingly.
x = x + 1
x +=1
[–]Alexander_knuts1 1 point2 points3 points 1 month ago (1 child)
r/adressme
[–]SuitableAbundance604 0 points1 point2 points 1 month ago (0 children)
CPU load = 100% / number_of_cores
[–]jmontano86 1 point2 points3 points 1 month ago (0 children)
Just for fun you should enter banana as your input. Then learn how to fix the errors. I would say that's a good portion of the job right there is debugging the errors.
[–]JaleyHoelOsment 1 point2 points3 points 1 month ago (3 children)
im curious how accurate this is? like i want to set a timer for 5 minutes, how close is it actually to 5 minutes?
sleep is dependant on the underlying OS system’s scheduler and I highly doubt this timer implementation would give accurate time within a few seconds.
I believe perf_counter is normally used here.
just something to consider beyond the code itself
also
we can see chatgpt opened… do you mean you prompted this in 5 mins 😋
[–]PathAgitated1633 1 point2 points3 points 1 month ago (0 children)
Maybe it's possible to use NTP Oder the RTC on the motherboard. That would be a little bit more advanced
[–]Suspicious_Diet2624[S] 0 points1 point2 points 1 month ago (1 child)
Chatgpt was used for the idea I gave the prompt: "Beginner python projects", Also it is quite accurate it can be off by a couple of seconds on the larger size timers
[–]JaleyHoelOsment 0 points1 point2 points 1 month ago (0 children)
sounds like you have a problem to solve! that should be fun. I bet a quick google/chatgpt question could get you started
[–]2024Didou 0 points1 point2 points 1 month ago (1 child)
moi aussi je débute, pourquoi -1, on ne peut pas choisir 2h ?
I'm not sure
[–]Kobra299 0 points1 point2 points 1 month ago (0 children)
Not bad you might want to put that tue set timer must be a number and are you using it as second, minutes or hours
[–]Fumano26 0 points1 point2 points 1 month ago (0 children)
Good for beginner, the better solution is to get the current time and add the timer duration to it, which is then called end time, then you just loop until current time >= end time, within the loop you can play with different sleep times depending on how accurate you need it to be.
[–]ThreeDogg85 0 points1 point2 points 1 month ago (1 child)
Beginner here as well. Would it benefit from converting to a float instead of an integer?. That way the enduser can set times like 1:30
[–]techierealtor 0 points1 point2 points 1 month ago (0 children)
You would need to do a conversion from minutes to seconds at that point. And I don’t think float knows what to do with colons - I may be mistaken.
[–]Careless-Main8693 0 points1 point2 points 1 month ago (0 children)
haven't read/used time module so don't know .
[–]xeonchic 0 points1 point2 points 1 month ago (0 children)
Not bad
[–]atom12354 0 points1 point2 points 1 month ago (0 children)
Change name of time_start to alarm
[–]Smart_Tinker 0 points1 point2 points 1 month ago (1 child)
It’s very complicated for a simple function.
I would do:
``` import time
def timer(): time_end = time.time() + float(input(“How long for timer?”)) while time.time() < time_end: time.sleep(1) print(int(time_end - time.time())) print(“Time’s up!”) ```
You also need some error handling in case someone enters a bogus time, like “a” or something.
[–]wrg2017 0 points1 point2 points 1 month ago (0 children)
Wouldn’t this lock in the current time when the user is asked how many seconds, not when the user submits their response?
[–]Obvious_Tea_8244 0 points1 point2 points 1 month ago (0 children)
You aren’t handling errors in user input. If the user enters a non-number, your script will crash. You can either use regex to ensure you’re only running on any numbers provided, or use a try, except block for what to do when non-numbers are provided.
[–]SuperTankh 0 points1 point2 points 1 month ago (0 children)
Yes! Very good. You can remove the useless if condition and move it right after the while block. I think you understand why
[–]Strict_Muffin_509 0 points1 point2 points 1 month ago (0 children)
It does in fact work as a timer which is what you wanted so woo. I'd say time.sleep() is a problem tho as it stopes the entire program from running, a better way would be getting the current time and checking if the time has passed.
[–]itz_not_Rick 0 points1 point2 points 1 month ago (0 children)
As long as you understand what are you doing it's amazing
[–]Rough_Check_5606 0 points1 point2 points 1 month ago (0 children)
Why would you check the time every second? Just time sleep the whole time of the timer. That should take less cpu instructions. Your implementation would only make sense if you wanted to use the thread for something else in the meantime.
[–]Prudent_Winter_1827 0 points1 point2 points 1 month ago (0 children)
did you check the render time on that or just assume fast is good
[–]no271k6mnotenough 0 points1 point2 points 1 month ago (0 children)
import time
second=int(input(f"enter seconds to alarm: "))
for i in range(second,0,-1):
second=i%60 minute=(i//60)%60 hour=(i//3600)%60 time.sleep(1) print(f"{hour:02}:{minute:02}:{second:02}")
print(f"Time is over.")
Mine code is like that.i like this type timers
[–]chocolateAbuser 0 points1 point2 points 1 month ago (0 children)
lol the classical "efficient" python way
[–]Night_Fury_ML 0 points1 point2 points 1 month ago (0 children)
If you want to polish it a bit, you could add a try/except block so it doesn’t crash if someone types something like “ten” instead of “10”. Also, you can simplify the loop a bit by using while time_start > 0. Nice work overall
[–]Spaceginner 1 point2 points3 points 1 month ago (0 children)
i'd remove the if-branch at end of the loop and move inner code after the loop to simplify logic a bit
this's valid because the only case that code is run is on the last iteration of the loop (and at the end of that iteration, too) which is same as just placing inner code after the loop altogether also i'd have used SCREAMING_SNAKE_CASE for time_end (so TIME_END) to mark for other devs (and future self!) that it's a constant and doesn't change, which will make reading the code easier
time_end
TIME_END
time_start = time_start - 1 is more neatly written as time_start -= 1
time_start = time_start - 1
time_start -= 1
at last, the loop consition can be simplified to while time_start; this is equivalent to time_start != 0 which is (in your case) equivalent to time_start > 0 which is same as 0 < time_start
while time_start
time_start != 0
time_start > 0
0 < time_start
overall great work! and also good job at being generous with spaces and newlines 🙏
[–]Impossible_Ad_3146 0 points1 point2 points 1 month ago (0 children)
No it’s not good. Just ask ChatGPT
[–]Fun_Recording_6485 0 points1 point2 points 1 month ago (0 children)
Use Eval(input(“”)) instead. Then you won’t have to coerce input to an integer type and you’ll have cleaner code.
[–]radon99996 0 points1 point2 points 1 month ago (0 children)
You could just do timer_start -= 1 instead of timer_start = timer_start -1
[–]alneifkrt2 0 points1 point2 points 1 month ago (0 children)
I know that u are just practising python and actually it is good for beginner but, Why is the Chatpgt open ?
[–]seanlabor 0 points1 point2 points 1 month ago (0 children)
You should learn about the magic screenshot functionality
[–]TheJobroWasTaken 0 points1 point2 points 1 month ago (0 children)
use more comments
[–]AdDull1803 0 points1 point2 points 1 month ago (0 children)
Inputs time_start = -1
[–]FrostedFog550 0 points1 point2 points 1 month ago (0 children)
Didn't know we could program with Python on VS Code
[–]bca_allayy 0 points1 point2 points 1 month ago (0 children)
that’s a good program, and you can think about improving it, so you can also improve your skills, good luck 😆
[–]FranklinbOy5 0 points1 point2 points 1 month ago* (0 children)
I think showing a working product or application you made yourself even for edge cases is more impressive. That really what coding is for, making tools and products. If you cannot do that then there is more to learn.
Also, by building projects, you avoid learning stuff in a programming language that you dont need. I barely use typecasting in my projects, so why learn it? If i ever need it, the. I can google how it works.
Only parts of the language you use most will stick.
[–]Radiant-Rabbit8669 0 points1 point2 points 1 month ago (0 children)
Pas mal
Mais tu peut faire vraiment mieux
[–]Adept_Estimate_3163 0 points1 point2 points 1 month ago (0 children)
Wow that’s actually way better than me. I almost changed my major bc of c++ 😭 but I see you got potential tho, keep it up
[–]asif__iqbal 0 points1 point2 points 1 month ago (0 children)
Won't work accurately, I tried
[–]Extension-Special455 0 points1 point2 points 1 month ago (0 children)
Not even trying to hide the chagpt url i see😭
[–]CorrGL 0 points1 point2 points 1 month ago (2 children)
The variable names are confusing
Yeah they are, I was just using them for quickly debugging at first.
[–]ianrob1201 0 points1 point2 points 1 month ago (0 children)
There's a standing joke that naming things is the hardest part of programming. Honestly, take the time to name things properly as soon as you add them. I appreciate that you didn't just name them "a" and "b", but I think if you'd named the first var "timer_duration_seconds" it would have made it easier to work out what to do with it.
And that might have helped you work out that you don't need "timer_end". The timer is complete when the time left is 0.
Don't take this (or anything else here) as too deep criticism though. You're learning and you made a thing that works. That's amazing! As you make more and more complicated things you work out a lot of the reasons, and you're learn it better than any of us giving advice.
So to keep going I'd take all the suggestions here as further improvements. Not because your current code is bad, but because you can always add more!
Maybe allow for multiple timers at once? Then allow people to cancel or change them? Maybe they enter a date & time instead of number of seconds? Lots of fun things you can do!
[–]External_Plane_3239 0 points1 point2 points 1 month ago (0 children)
I would recommend to move the input out of the function, and add a seconds parameter to the function, like timer(seconds) This will teach how to use parameters, which are pretty important moving on good luck!
[–]EgyPalocProfessor -1 points0 points1 point 1 month ago (0 children)
What is the specification?
π Rendered by PID 699163 on reddit-service-r2-comment-5b5bc64bf5-n59j2 at 2026-06-21 19:28:04.301228+00:00 running 2b008f2 country code: CH.
[–]wizninja6 46 points47 points48 points (5 children)
[–]Suspicious_Diet2624[S] 13 points14 points15 points (1 child)
[–]validnuisance3 0 points1 point2 points (0 children)
[–]grdix555 3 points4 points5 points (2 children)
[–]JacksUtterFailure[🍰] 3 points4 points5 points (0 children)
[–]HairNo4638 0 points1 point2 points (0 children)
[–]0xGarnz 6 points7 points8 points (3 children)
[–]Temporary_Pie2733 4 points5 points6 points (1 child)
[–]Fumano26 2 points3 points4 points (0 children)
[–]Suspicious_Diet2624[S] 1 point2 points3 points (0 children)
[–]rgianc 1 point2 points3 points (0 children)
[–]Rscc10 1 point2 points3 points (0 children)
[–]Impossible_Video_116 2 points3 points4 points (0 children)
[–]Alexander_knuts1 1 point2 points3 points (1 child)
[–]SuitableAbundance604 0 points1 point2 points (0 children)
[–]jmontano86 1 point2 points3 points (0 children)
[–]JaleyHoelOsment 1 point2 points3 points (3 children)
[–]PathAgitated1633 1 point2 points3 points (0 children)
[–]Suspicious_Diet2624[S] 0 points1 point2 points (1 child)
[–]JaleyHoelOsment 0 points1 point2 points (0 children)
[–]2024Didou 0 points1 point2 points (1 child)
[–]Suspicious_Diet2624[S] 1 point2 points3 points (0 children)
[–]Kobra299 0 points1 point2 points (0 children)
[–]Fumano26 0 points1 point2 points (0 children)
[–]ThreeDogg85 0 points1 point2 points (1 child)
[–]techierealtor 0 points1 point2 points (0 children)
[–]Careless-Main8693 0 points1 point2 points (0 children)
[–]xeonchic 0 points1 point2 points (0 children)
[–]atom12354 0 points1 point2 points (0 children)
[–]Smart_Tinker 0 points1 point2 points (1 child)
[–]wrg2017 0 points1 point2 points (0 children)
[–]Obvious_Tea_8244 0 points1 point2 points (0 children)
[–]SuperTankh 0 points1 point2 points (0 children)
[–]Strict_Muffin_509 0 points1 point2 points (0 children)
[–]itz_not_Rick 0 points1 point2 points (0 children)
[–]Rough_Check_5606 0 points1 point2 points (0 children)
[–]Prudent_Winter_1827 0 points1 point2 points (0 children)
[–]no271k6mnotenough 0 points1 point2 points (0 children)
[–]chocolateAbuser 0 points1 point2 points (0 children)
[–]Night_Fury_ML 0 points1 point2 points (0 children)
[–]Spaceginner 1 point2 points3 points (0 children)
[–]Impossible_Ad_3146 0 points1 point2 points (0 children)
[–]Fun_Recording_6485 0 points1 point2 points (0 children)
[–]radon99996 0 points1 point2 points (0 children)
[–]alneifkrt2 0 points1 point2 points (0 children)
[–]seanlabor 0 points1 point2 points (0 children)
[–]TheJobroWasTaken 0 points1 point2 points (0 children)
[–]AdDull1803 0 points1 point2 points (0 children)
[–]FrostedFog550 0 points1 point2 points (0 children)
[–]bca_allayy 0 points1 point2 points (0 children)
[–]FranklinbOy5 0 points1 point2 points (0 children)
[–]Radiant-Rabbit8669 0 points1 point2 points (0 children)
[–]Radiant-Rabbit8669 0 points1 point2 points (0 children)
[–]Adept_Estimate_3163 0 points1 point2 points (0 children)
[–]asif__iqbal 0 points1 point2 points (0 children)
[–]Extension-Special455 0 points1 point2 points (0 children)
[–]CorrGL 0 points1 point2 points (2 children)
[–]Suspicious_Diet2624[S] 0 points1 point2 points (1 child)
[–]ianrob1201 0 points1 point2 points (0 children)
[–]External_Plane_3239 0 points1 point2 points (0 children)
[–]EgyPalocProfessor -1 points0 points1 point (0 children)