It's a simple calculator with few operators, please take a look and give me feedback on what I can do to shorten it (and run the same way it does with this coding) or ways to improve it (extra coding to make it efficient?)
My recommendations would both fall under the category of ERROR TRAPPING: Although you do address the invalid operator, I think you should do it when the user enters it, not after getting all the inputs. The second one is when you ask the user y or n, you should only accept y or n. Just my opinions to make the program a bit more bullet proof.
EDIT: Do not just verbatim copy the first suggestion, because I made a mistake. The code I wrote doesn't work quite right with yours, so it's not just a copy-paste replacement. However, I'd still recommend taking the time to understand how it works and how to incorporate it. It'll just take a bit more effort to make them work with what you already have. At the very least, it'll be somewhat of an exercise in debugging.
Here are a few improvements I would make:
Wherever you're inputting a number, check for a
ValueError exception. Based on the code you already have, it'd look something like this:
# replace line 6 with this code, and edit this for line 16 as well def num1_prompt(): try: global num1 num1 = float(input("Please enter a number: ")) except ValueError: choice = input("Invalid number, try again? y/n: ") if choice.casefold() == "y": num1_prompt() else: #You could put something like print("Stopping program!") here, if you want to. quit() num1_prompt()
Secondly, on line 45, instead of having
finish = input("Thank you, please enter any character to exit: "), have something like the following:
print("Thank you for using my calculator!") quit()
This way, rather than having to enter characters to stop the program, it'll stop on its own.
Finally, at every point where you have to input a (y)es or (n)o answer, use
if choice.casefold() == "y": instead of
if choice == "y":. Using
.casefold() will change your input to all lowercase, meaning that regardless of whether "Y" or "y" is input, it'll still be read as "y", which is what you want. Case insensitivity!
(By the way, small nitpick, but it'd be a little nicer if your program checked for an invalid operator before it asked for the second number.)
@eighty Thank you so much, this is actually very helpful.
Could you please answer these questions tho since I'm fairly new to coding
1)Could you please explain what global num1 does
2)why did you def num_prompt(), can I do it without defining it?
3) What does try: and, except do?
Lastly, the reason I put an input at the end to close the program is because when I put the program into my desktop and opened it without the editor, (Like I opened the program to straight up RUN it)
the program would run fine but at the end it closes instantly after printing the last code.
For example if I put print("Thank you") it would immediately close the program right after it prints the "thank you" and it wouldn't let the user see it, so I did it the input way so the user gets to see everything he did before he can close it on will.
Oh and also thank you for the .casefold, It's really helpful learning new things.
(Also if you got the extra time after responding, could you tell me how you edited your comment :p (Bold,italic,etc.)
@Komil sorry about the wait, I've been busy.
1) I'm gonna be honest, I'm not entirely sure. Sometimes, you can get an error similar to the following:
Line <#>: undefined: Error: local variable '<variable>' referenced before assignment. If this happens, stick a
global <variable> on the line before the error, and that should hopefully fix it. I just tend to use
global whenever the code I'm writing has a lot of functions and whatnot, even if it might be unnecessary. Consider it like an error vaccine; you might not ever get it, but you're protected regardless.
In all honesty, though, that's pretty bad advice, so I'd definitely recommend reading this GeeksForGeeks article on the topic.
2) Like I said in my edit, that was an oversight on my part. You can just ignore that. Instead, I'd recommend putting most or all of your code inside a function. That way, when using an error handler - as I'll explain shortly - you can quickly and easily restart the program without any user input.
3) This is a little hard to explain, but I'll try my best. Whatever code you put in the
try: statement will be run normally, and if it doesn't throw an error, your code will skip the
except <error>: and move on. However, if you do get an error, Python will look at the error statement(s) you have after your
try:. If the error type it threw matched any of your
excepts, it'll run whatever code follows the
except <error>:. If that didn't make much sense, see if this code example helps:
# Receive input, convert it to an integer, store it as a variable. # This is run inside a try statement. try: num1 = int(input()) # If the input can't be converted to an integer, it'll throw a ValueError. # We don't want this, though, we want the program to do something in case this happens. except ValueError: # To make this easier to understand, it could be imagined as an if statement: "If a ValueError is thrown, run the following code." # Code to run if error is thrown print("A ValueError has appeared! Oh no!") # If no error is thrown, the code will continue here print(num1 * 2) # In the case that the thrown error doesn't match any of your except statements, your program will throw a standard error message and stop the program.
4) Oh, that makes sense, I hadn't thought of that! I guess I'm just too used to Repl.it, then.
5) You're welcome for that,
.casefold() is definitely a life saver.
As for text formatting in comments, Repl Talk uses something called Markdown. I'd definitely recommend giving it a read!
@eighty Hey, no worries on the late reply!
I understood what global means, thanks :)
and I also understood what try: and except mean.
Honestly it means a lot because these things that I have learned from you will actually help me make more small fun programs, if anything I'll hit you up on Repl.it, if possible could you give me your email so I can straight up email you codes or the python file or if you want privacy, I could give you my email so you can send me a message telling me its you.
If you want your email to be kept private it's all good too, I'll try to reach you from repl.it.
Anyways thanks a lot!
@Komil you're welcome to contact me whenever and wherever! I've been working on a personal website to teach myself HTML and whatnot, and you can find some of my other Repl.it projects as well as my contact info there. If you're suspicious of questionable links - I don't blame you - you can find the Github repository for it here, as well as a slightly out-of-date version right here on Repl.it.
Warning: self-deprecation inbound.
@Komil I bought my domain from Porkbun because they're fairly cheaper than other domain registrars. As for actually hosting the website, I use Github Pages, mainly due to the fact that it's free. It actually has some really nice features, such as Jekyll, but I have yet to actually use them since that'd ruin the "learning HTML" part. If you're not interested in HTML and just want a nice-looking site, Jekyll is a really good workaround. Using a site builder like Wix will always be easier (and probably look better), but those are expensive, so if you're willing to put in the effort, it's definitely worth it to avoid those.
And, as always, Google is your best friend when it comes to technical stuff like DNS.
@Komil So, let's say you've bought the domain komil.work. You now own the domain name, but you still have to tell your domain where to get content from. (Until you do this, it'll show a basic page showing that the website has been bought, but this page is otherwise useless.) This can be done by pointing a DNS record to a web server. Web hosting companies, such as HostGator or Hostinger, will store the files and data for your website on their servers and handle all the delivery for you. However, you have to pay in order to actually use their servers, kind of like renting an apartment. Github isn't a web hosting company, and therefore they make their money in other ways. This allows them to offer basic, free web hosting for their users. Github won't be as feature-rich as proper hosting companies, but for a basic, personal website, it'll do. Once you own your domain and have successfully set up your Github Pages site, you can use a DNS record to point your domain name towards the Github server where your website is hosted, and the data will be fetched and sent to whoever is trying to see your website.
To simplify, think of your domain name as your house address. You own the property, but until you have a house built, visitors will only find an empty lot.
To get started with Github Pages, go to this link and follow the interactive guide. Select "User or organization site" for the first option it gives you, and "I don't know" for the second. This will show you how to install the Github Desktop client as well as how to create a personal site, rather than a project one.
Alternatively, you could also host your website on Repl.it, but I wouldn't recommend that. Github is far more reliable.
You might consider using
eval after confirming the provided operator is valid. This would eliminate the if-then-elif loop, but using eval might be considered bad practise.