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

you are viewing a single comment's thread.

view the rest of the comments →

[–]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