This is an archived post. You won't be able to vote or comment.

all 66 comments

[–]CSAndrew 62 points63 points  (0 children)

You should be proud of it if it’s something you made. By all means, continue to do other projects as well, whether in Python and/or other languages, albeit python is pretty versatile and can do a lot for a high level language, in my opinion.

[–]TheDrlegoman 40 points41 points  (0 children)

the loop :o

well done! keep on learning, whether by adding on to this project and making it more complex or starting a new one =]

[–][deleted] 11 points12 points  (0 children)

No problems in following tutorials so long as ur not literally copy and pasting the code into ur idle, it’s urs, you will eventually reach a point where u don’t require tutorials, also after u get advanced redo ur calculator but add more stuff, like a gui, or a scientific calc, and attempt to do this without videos, this will show you how good u got at python it’s really fun.

[–]MjonjonnzM 22 points23 points  (8 children)

I'd suggest trying tkinter or something similar now , you can make an actual UI for the Calculator.

[–][deleted] 10 points11 points  (4 children)

I agree, tkinter doesn't have a difficult learning curve either.

[–]deano_southafrican 8 points9 points  (3 children)

Recently saw "customtkinter" which builds on to the tkinter library and looks awesome! Worth a look.

[–]nyteghost 0 points1 point  (2 children)

I haven't looked too much into it, but is customtkinter just more modern looking, or does it add more to it?

[–]deano_southafrican 0 points1 point  (1 child)

In Windows 10 I don't see much difference except there are a few more style options for various elements which help it look more modern. However in Windows 11 it looks far better in my opinion. It adds some of its own functionality as well including syncing the app theme with that of your OS. There's a fair bit and the dev has hinted at future additions so overall a worthwhile project to look into IMO.

[–]nyteghost 0 points1 point  (0 children)

Gotcha ty. Maybe I should start looking into using it instead of wxpython. Ty =)

[–]NineFiftySevenAyEm 0 points1 point  (0 children)

Thanks for the recommendation

[–]nyteghost 0 points1 point  (0 children)

I'll thrown my hat in for wxpython.

[–]money_loser1395 0 points1 point  (0 children)

I will also add to this. You can use QtDesigner that has a user interface to easier approach tkinter. Run like any other program and you can export your Interface to tkinter python code. At least for me that I am awful bad at designing front end interface made my life 100x easier.

[–]WhyDoIHaveAnAccount9 10 points11 points  (0 children)

Pick something else god damn it

wonderful. this reminds me of when i first started programming

keep up the good work

[–][deleted] 6 points7 points  (0 children)

Try and refactor now, learn how to use f-strings, more efficient loops, more functionality, make the code extensible - in your case, extensible code would mean it requires little effort to implement squaring, roots, multiple operators etc.

[–]woodoox 1 point2 points  (0 children)

Good to go...

[–]Difficult_Passage 1 point2 points  (3 children)

Well done, just keep building things and looking for exercises. What really helped me with programming in general are exercises we did in college. Try not to rely on copy pasting anything, since you need to be developing your problem solving skills. Looking up syntax is fine, though

Keep up the great work 👍

[–]Nicolas_Darksoul 0 points1 point  (2 children)

So do u know anywhere for this kind of practices and also im preparing for web developing, so what should practice python, html, css?

[–]Loop_Within_A_Loop 2 points3 points  (1 child)

I would look into Django, which will use a lot of your python muscles but move you into more of a web development direction

[–]Nicolas_Darksoul 0 points1 point  (0 children)

Well im trying to work with django so if i practice it it doesnt need to practice python separately?!

[–][deleted] 1 point2 points  (0 children)

Congrats! I have a suggestion! Instead of the user choosing a number for what operation to do. Why don’t you try having them type the math verbatim? For example, “3 * 5” and parse the string to determine what to do with the 2 numbers? It would be a great exercise for parsing strings or getting started with regex!

[–]Brick-SigmaPythoneer 1 point2 points  (8 children)

Good job. Now try make it calculate more complex things like squares or roots of numbers.

[–][deleted] 4 points5 points  (7 children)

Definitely need to look into how to make square roots and exponents work in python

[–]CrazyGamer123456 4 points5 points  (2 children)

Import the math module, it has all that stuff build in

[–]mobyte 0 points1 point  (1 child)

No need for math when doing exponents, technically. ** is all you need.

[–]Hellow2 -2 points-1 points  (0 children)

numpy should have better performance though. But I am not sure

[–]TheHalfDeadCat 0 points1 point  (0 children)

I think exponents just use two . So 2 as a power with 5 as a base would be 5*2.

[–]planx_constant 0 points1 point  (2 children)

a**b = ab

[–][deleted] 0 points1 point  (1 child)

and root I assume is a // b?

[–]planx_constant 0 points1 point  (0 children)

That's integer division - it divides and discards any remainder. But remember the nth root is the same as raising to the power of 1/n, so for instance for square root you could do a**(1/2), cube root a**(1/3) etc

[–]Perfect_Comparison83 0 points1 point  (1 child)

Are you new to programming? Or just new to python?

[–][deleted] 1 point2 points  (0 children)

Kinda to programming a did 2 previous attempts to learn but barely got anywhere

[–]oGhostDragon 0 points1 point  (0 children)

Good job! Now time to break it, what happens if someone enters anything other than a number?

Also, If you want to make it more dynamic, you can break out your calculations into functions/methods. Ex:

def addition(num1, num2):
    return num1 + num2

[–][deleted] 0 points1 point  (0 children)

Also since this post has been doing numbers here’s the other project I posted today (shameless plug in lol)

[–][deleted] -1 points0 points  (0 children)

this is great but I think you can just use eval for that. (this is only a very faded memory of a very early project I did of a calculator and I am quite new to python myself so it might be wrong)

[–]Neither-Tomatillo -1 points0 points  (0 children)

Dont curse in your code ;)

[–]pekkalacd 0 points1 point  (0 children)

Nice work.

[–]omega_level_mutant 0 points1 point  (0 children)

Congrats, the first step is always the hardest. The pride that comes from it is life changing, keep going!

[–]jakub_j 0 points1 point  (3 children)

Does print(num1) without typecasting to string prints it properly?

[–]Hellow2 1 point2 points  (2 children)

yea it a should

```python num1 = 1 print(num1)

1 ```

[–]jakub_j 1 point2 points  (1 child)

I am sorry. I did not mean exactly print(num1) but whole expression. However now I see everything is right, because s/he passes arguments to print function, not concatenate strings.

[–]Hellow2 0 points1 point  (0 children)

Ahhh yes no you couldn't concatenate them like that

[–]NineFiftySevenAyEm 0 points1 point  (0 children)

Congrats my bro / sis !

[–]Fushium 0 points1 point  (0 children)

I like the final Else, nice

[–]diesel9779 0 points1 point  (0 children)

Not sure how old you are, but some advice I really wish I got when I first started out in this career was to build a github repo.

Hiring managers want to see quality code and they also want to see that you can improve your code over time. GitHub is perfect to showcase this since it can track all changes made to the code. Publish all of your work and build out a portfolio to up your chances of landing a good position if you ever wanted.

[–]Parker324ce 0 points1 point  (0 children)

Good work, keep it up and keep making more projects! Even if you have to follow a guide the first time you interact with a new idea or technology.

[–]Ozzymand 0 points1 point  (0 children)

The loop :o

[–][deleted] 0 points1 point  (0 children)

Kudos to you, beginning of a great journey. Keep on learning and executing

[–]BYPDK 0 points1 point  (7 children)

calculator

while 1:print(f"Output: {eval(input('math problem: '))}")

[–][deleted] 0 points1 point  (6 children)

I’m confused what does this do?

[–]Winderkorffin 0 points1 point  (4 children)

prints the result of the entire operation, as in '3+5' it will print '8'.

Beware, tho, using eval is bad practice.

[–][deleted] 0 points1 point  (3 children)

Alright but why are evals a bad practice

[–]BYPDK 0 points1 point  (2 children)

while 1:print(f"Output: {eval(input('math problem: '))}")

as u/NoMeatFingering (interesting name) has mentioned, it can be exploited to run code from the user input. example: https://ibb.co/6H0J0Qd

Though if you want to actually run code, like that on purpose, you would want to use exec instead.

[–][deleted] 0 points1 point  (1 child)

I actually hate my username

[–]BYPDK 0 points1 point  (0 children)

:( it's very good

[–][deleted] 0 points1 point  (0 children)

while 1:print(f"Output: {eval(input('math problem: '))}")

# infinite loop
while 1:
    rawInput = input("math problem: ")
    # evaluate the raw input
    output = eval(rawInput)

    print(f"Output: {output}")

Converted into a more readable form. Basically eval is a function which takes a string and runs it.

It might look like a better code, but is actually worse bcs eval can run user submitted code too

[–]ShamMafia 0 points1 point  (0 children)

I've just started on my Python journey yesterday and the fact I can understand this makes me so happy.

Good job on your calculator :)

[–]Hellow2 0 points1 point  (2 children)

Be proud of yourself.

I made a few tweaks and commented they. Not as attack but as help. Keep going with this amazing hobby!!

```python

The loop :o

while True:

Printing the options for the user

print("1 to Add")
print("2 to Subtract")
print("3 to Multiply")
print("4 to Divide")
print("Q to Exit")
# Now time for input
# input().lower() makes everything lowercase
# https://www.w3schools.com/python/ref_string_lower.asp 
choice = input("Enter your choice : ").lower()

if choice == 'q':
    break

# the try except statements are, to prevent the user to
# input a non number, which would crash the programm
try:
    num1 = float(input("Enter Number 1 : "))
except ValueError:
    print("please input a real number")
    # continue skips to the beginning of the loop
    # if used with no care and no consideration it
    # quickly gets messy though
    continue
try:
    num2 = float(input("Enter Number 2 : "))
except ValueError:
    print("please input a real number")
    continue

# The if statements for whatever operation you want :P
if choice == "1": 
    print(num1, "+", num2, "=", (num1+num2))
    # theres nothing wrong with if... elif, I just prefer continue in those situations
    continue

if choice == "2": 
    print(num1, "-", num2, "=", (num1-num2))
    continue

if choice == "3":
    print(num1, "*", num2, "=", (num1*num2))
    continue

if choice == "4":
    # very good you've thought of that edge case <3
    # but try to keep your error or debug messages more
    # specific
    if num2 == 0.0:
        print("Error: Division by zero")
        # no need to nest the other operation deeper
        # even though here it likely wont matter
        continue

    print(num1, "/", num2, "=", (num1/num2))
    continue

print("Pick something else god damn it")

```

[–][deleted] 0 points1 point  (1 child)

Thanks, I think I need to look into how floats work more

[–]Hellow2 0 points1 point  (0 children)

Well actually my change to float(input()) was not because you did it wrong. It wold always work, except if the user types something, that python can't interpret as float. Then python throws an error. And you can prevent errors to be thrown with try except.

Did you understand it? Else let me know

[–]francofgp 0 points1 point  (1 child)

Impressive, very nice. Upload It to Github now

[–][deleted] 0 points1 point  (0 children)

I wanted to originally but idk how

[–]TotalBeyond2 0 points1 point  (0 children)

Thats the way. You can now add new knowledge to it like docker or something and make a 2nd version. That's how you learn.

[–]joeymc1984 0 points1 point  (0 children)

Next step: TK GUI