Stian Breilid
Stian Breilid

Reputation: 61

One line, three variables

I am here trying to just make a simple calculator in python, and I wonder if its possible to make the 3 first lines in to one line when command run. What I mean with that is; I don't have to press enter to type the next number/operator but press space instead (in the input section).

while True:
    import operator

    num1 = int(input("Whats the first number:"))
    oper = input("Which operator would you like to use: (+,-,/,*,**,^)  :")
    num2 = int(input("Whats the second number:"))


    if oper == "+":
        x = operator.add

    elif oper == "-":
        x = operator.sub

    elif oper == "*":
        x = operator.mul

    elif oper == "/":
        x = operator.__truediv__
    elif oper == "**":
        x = operator.pow
    elif oper == "^":
        x = operator.xor

    else:
        print("invalid input")

    print(num1,oper,num2,"=",x(num1,num2))

Upvotes: 6

Views: 1109

Answers (4)

Malo
Malo

Reputation: 1313

An improved working calculator which handles some errors based on other answers:

import operator

x = {
        "+": operator.add,
        "-": operator.sub,
        "*": operator.mul,
        "/": operator.__truediv__,
        "**": operator.pow,
        "^": operator.xor
    }

while True:
    print("Enter a number, a space, an operator, a space, and another number.")
    try:
        num1str, oper, num2str = input().split()
        num1, num2 = int(num1str), int(num2str)
        print(num1,oper,num2,"=",x[oper](num1,num2))
    except:
       print("invalid input")
        

Upvotes: 0

MB-44
MB-44

Reputation: 1

calc=input("type here:- ").split()
# type belove with space between
num1,operatoe,num2=calc[:]
# if length of calc doesn't equal to three you can continue with while loop 
print(num1,operator,num2)

Upvotes: 0

Pedro Lobito
Pedro Lobito

Reputation: 98921

Rory's answer and comments pointed on the right direction, but here's a practical example:

operators = ["+","-","/","*","**","^"]
msg = f"Example query: 8 * 4\nAllowed operators: {', '.join(operators)}\nType your query and press enter:\n"

x = input(msg)
cmd_parts = [y.strip() for y in x.split()] # handles multiple spaces between commands

while len(cmd_parts) != 3: # check if lenght of cmd_parts is 3
    x = input(msg)
    cmd_parts = [y.strip() for y in x.split()]

# verification of command parts
while not cmd_parts[0].isdigit() or not cmd_parts[2].isdigit() or cmd_parts[1] not in operators :
    x = input(msg)
    cmd_parts = [y.strip() for y in x.split()]

num1 = cmd_parts[0]
oper = cmd_parts[1]
num2 = cmd_parts[2]

res = eval(f"{num1} {oper} {num2}")
print(num1,oper,num2,"=", res)

Python Example (Enable Interactive mode)

Upvotes: 0

Rory Daulton
Rory Daulton

Reputation: 22544

You can use the split method of Python strings to accomplish this. Note that this code depends on three objects, separated by spaces, being entered. If more or fewer are entered or the spaces are forgotten, or either "number" is not actually an integer, there will be an error.

print("Enter a number, a space, an operator, a space, and another number.")
num1str, oper, num2str = input().split()
num1, num2 = int(num1str), int(num2str)

Upvotes: 8

Related Questions