Calculator
Komil (9)

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?)

You are viewing a single comment. View All
eighty (2)

@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!