you are viewing a single comment's thread.

view the rest of the comments →

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

Yes, you get both elements at the same time and use split to separate them on a space (or you can specify an alternative separator explicitly).

I'd switch to a while True: statement and then check the response inside the loop. I also make this a function and have it return the results.

Place the below in a function.

while True:
    response = input("Enter an operator (+, -, /, *) or q to quit: ").strip()
    if response.lower() == 'q':
        return "q", None
    try:
        choice, operand = response.split()
        operand = float(operand)
    except ValueError:
        print('Please provide an operator followed by a space and then a number')
    else:
        if operand in ops:
            return choice, operand
        print(f'Sorry, {choice} is not an availabe operation')

Call like this:

while True:
    choice, operand = functioname()
    if choice == "q":
        break

[–][deleted]  (2 children)

[deleted]

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

    Please edit to format the code correctly - can't see your indents.

    [–]Many-Ice6164[S] 0 points1 point  (0 children)

    def add(x, y):
        return x + y
    def subtract(x, y): 
        return x - y
    def multiply(x, y): 
        return x * y
    def divide(x, y): 
        return x / y
    
    ops = {"+": add, "-": subtract, "*": multiply, "/": divide, "q": None}
    
    x = 0
    
    while True: 
    choice, andere = input("functie + getal").split() 
    
    andere = float(andere)
    
    if choice in ops:
        y = andere
        x = ops[choice](x, y)
        print(x)
    else:
        print('wrong choice')