pythonedx

How do i turn split input into int?


Here's what I have so far. What I'm trying to do is ask the user for an expression, they input something like 1 + 1 and it's split into x, y, and z. Such that x would be 1, y would be + and z would be the second 1. I'm sure there may be several issues but the main one I'm having right now is converting their input into an int.

x, y, z = input("Expression: ").split()

if y == "+":
    output = x + z
    print(output)

elif y == "-":
    output = x - z
    print(output)

elif y == "*":
    output = x * z
    print(output)

elif y == "/":
    output = x / z
    print(output)

Solution

  • You can try for this:

    x, y, z = input("Expression: ").split()
    
    x = int(x)
    z = int(z)
    
    if y == "+":
        output = x + z
        print (output)
    
    elif y == "-":
        output = x - z
        print(output)
    
    elif y == "*":
        output = x * z
        print(output)
    
    elif y == "/":
        output = x / z
        print(output)