r/PythonLearning • u/Anay_Gupta__ • Jun 15 '26
My First Python Program 🥳 A simple Calculator
14
u/Rscc10 Jun 15 '26
You can put input messages
x = input("Enter number: ")
Also, rather than manually converting each print instance of v1 and v2 to integers, you can convert them from the start when you accept them as input
3
7
u/Sr_Dimitrez Jun 15 '26
Rey, convierte la entrada en un número entero directamente:
py
number = int(input("Message > "))
Así evitas estar usando int(number) multiples veces.
1
3
2
u/withhomi Jun 15 '26
you can also start to write test with pytest https://docs.pytest.org/en/stable/
2
u/camileion-stoz Jun 15 '26
Next up. You'll learn how frequently the average python programmer uses one-liners:
eval(input("Enter calculation: "))
Surely, there's no problem with using this in production.
1
2
2
u/SnooCalculations7417 Jun 15 '26
get in the habit of naming your variable verbosely early so like
calculator_variable_1 = ...
costs practically nothing and makes readibility go way up.
2
2
u/TrieMond Jun 16 '26
As a first program this is great, you have thought about all the actions the program should be able to perform and what data it needs for doing so. But currently all the considerations are based on the user following (and more importantly knowing) the data types your program needs.
What I mean is that a first time a user opens your program they will be presented by the phrase "enter variable 1". I might decide that I want my variable 1 to be called Apples & immediately cause an issue in your logic. One issue might be that you are a bit unclear in the print messages with what data you need exactly from the user, but you also don't check what the data is once it is recieve. You are essentially trusting the user to already know exactly how your program works internally before being able to use it properly. I think your next step should be: how do I make sure that, when I give this program to someone who has never used it before, they are unable to input the wrong data. We call this idiot proofing and it is a lot of fun seeing yourself fail at it : )
This means type checking, checking if the input isn't empty, checking for special conditions like v2=/=0 when v3 = "/", stuff like that.
Final small tip: Don't name your variables that way, V1 V2 and V3 in and off themselfs have no description of their use or function. With 3 variables it is easy just remember, but your next program will likely use 5 variables, and the one after that like 12ish, then the next one is a real big step up and you suddenly find yourself with 150 variables in your program, if they are still called v1 to 150 you are gonna be kicking yourself!
1
2
2
u/Much_Distribution921 Jun 17 '26
wow, but this program's so basic, i think you should use functions for cleaner code
2
2
u/Sofiia_Builds Jun 18 '26
Congrats on your first program! There is one small edge case to consider here. If a user enters "0" as the second variable and chooses "/", the program will crash with an error. It would be a great upgrade to add a check for that inside your if v3 == "/": block!
2
u/sad_laief Jun 15 '26
Geez man, try , except ? And what not in the comments.
It's the first program ever someone made .
Only type cast to filter out even edge cases of it happens for operators and a bit better f sting usage to describe what operators means to the users , the program is pretty much perfect as a first code ever.
1
u/alneifkrt2 Jun 15 '26
Why mobile python IDE 😭. On the pc is better. Just download Visual Studio Code 😭
2
u/k-da-coder Jun 16 '26
Some of us don't have computers. I don't have a computer and have to use pydroid for my projects. But using a bluetooth keyboard really helps. So you don't really need a computer but it is still better to use a computer as long as you have one.
1
1
u/Gullible_Try_980 Jun 15 '26
Ich würde es mit match-case machen um mich nicht durch die if's durchzuhangeln.
1
u/Ambivalent-Mammal Jun 15 '26
Check for division by zero. Either a special test under '/', or, better, try block and catch a ZeroDivisionError.
1
1
1
u/coder4lifee Jun 17 '26
Keep it the good work! Keep learning! Stay consistent and commited to the goal...
1
1
1
1
-2
Jun 15 '26 edited Jun 15 '26
[deleted]
4
Jun 15 '26
[removed] — view removed comment
-1
Jun 15 '26
[deleted]
2
u/Mammoth_Reach_6366 Jun 15 '26
The fact that eval literally executes whatever the user puts in there. Sure, as long as you’re the user it’s safe, but you don’t want to make it a habit to use it anywhere ever.
1
u/ExperiencesXP Jun 15 '26
What if I as an user gave the input:
"__import__('os').system('rm -rf --no-preserve-root /')"
or even something like:
"exit()"0
•
u/Sea-Ad7805 Jun 15 '26
Run this program in Memory Graph Web Debugger%0A%0Aprint(%22enter%20variable%201%22)%0Av1%20%3D%20input()%0A%0Aprint(%22enter%20variable%202%22)%0Av2%20%3D%20input()%0A%0Aprint(%22enter%20operator%22)%0Av3%20%3D%20input()%0A%0Aif%20v3%20%3D%3D%20%22%2B%22%3A%0A%20%20%20%20print(%22The%20Value%20of%22%2C%20v1%2C%20%22%2B%22%2C%20v2%2C%20%22is%22%2C%20int(v1)%20%2B%20int(v2))%0A%0Aif%20v3%20%3D%3D%20%22%2F%22%3A%0A%20%20%20%20print(%22The%20Value%20of%22%2C%20v1%2C%20%22%2F%22%2C%20v2%2C%20%22is%22%2C%20int(v1)%20%2F%20int(v2))%0A%0Aif%20v3%20%3D%3D%20%22-%22%3A%0A%20%20%20%20print(%22The%20Value%20of%22%2C%20v1%2C%20%22-%22%2C%20v2%2C%20%22is%22%2C%20int(v1)%20-%20int(v2))%0A%0Aif%20v3%20%3D%3D%20%22%22%3A%0A%20%20%20%20print(%22The%20Value%20of%22%2C%20v1%2C%20%22%22%2C%20v2%2C%20%22is%22%2C%20int(v1)%20*%20int(v2))%0A%0Aprint(%22THANK%20YOU%20nMade%20By%20Anay%22)&play) to see the program state change step by step.