this post was submitted on 01 Mar 2025
20 points (88.5% liked)

Programming

18472 readers
118 users here now

Welcome to the main community in programming.dev! Feel free to post anything relating to programming here!

Cross posting is strongly encouraged in the instance. If you feel your post or another person's post makes sense in another community cross post into it.

Hope you enjoy the instance!

Rules

Rules

  • Follow the programming.dev instance rules
  • Keep content related to programming in some way
  • If you're posting long videos try to add in some form of tldr for those who don't want to watch videos

Wormhole

Follow the wormhole through a path of communities [email protected]



founded 2 years ago
MODERATORS
 
 # Ask user to enter an expression and display output
def main():
    expression = input("Expression: ")

    print(calculate(splitter(expression)))


# Split expression into components and assign to variables as float values
def splitter(expression):
    x, y, z = expression.split()

    return x, y, z

# Calculate expression result
def calculate(x, y, z):
    x, z = float(x), float(z)

    if y == "+":
        return str(round((x + z), 1))
    elif y == "-":
        return str(round((x - z), 1))
    elif y == "*":
        return str(round((x * z), 1))
    else:
        return str(round((x / z), 1))



main()

I am getting traceback errors for any expression (1 + 1) I enter.

you are viewing a single comment's thread
view the rest of the comments
[–] [email protected] 3 points 2 days ago (1 children)

Yeah others have pointer out the error, but I want to really recommend using VSCode with the Python extension and static types. It will make finding these errors super easy because it adds a red underline exactly where the problem is.

Static types means:

def splitter(expression: str) -> tuple[str, str, str]:
...
def calculate(x: str, y: str, z: str) -> str:
[–] [email protected] 4 points 2 days ago (1 children)

PyCharm is the way to go to write Python.

[–] [email protected] 1 points 2 days ago* (last edited 2 days ago) (1 children)

Yeah that's a great option too. Not free though (although the pricing is very reasonable IMO). I think if you're this much of a beginner it doesn't make sense to pay for Pycharm.

[–] [email protected] 4 points 2 days ago (1 children)

The community version is free right

[–] [email protected] 1 points 2 days ago

Ah yeah I don't think that existed last time I used it.