简体   繁体   English

单一输入中的数学运算

[英]Mathematical operation in one single input

I'm a python beginner and i'm trying to make a calculator that instead of asking the user enter first number than an operator and than antoher number to return the result, the user can be able to input like for example 3 * 3 in one single input so the computer returns 9.我是 python 初学者,我正在尝试制作一个计算器,而不是要求用户输入第一个数字而不是运算符和另一个数字来返回结果,用户可以输入例如 3 * 3 in一个单一的输入,因此计算机返回 9。

num1 = float(input("Enter a number: "))
op = input("Enter an operator: ")
num2 = float(input("Enter another number: "))

if op == "+":
 print(num1 + num2)
elif op == "-":
 print(num1 - num2)
elif op == "*":
 print(num1 * num2)
elif op == "/":
 print(num1 / num2)

else:
 print(" Enter a valid operator...")

python eval function may help: python 评估 function 可能会有所帮助:

inputstr = '3 * 3'
print(eval(inputstr))

If you are sure the input will be completely formatted, use eval()如果您确定输入将完全格式化,请使用eval()

s = input("Enter an expression: ")
try:
    print(eval(s))
except:
    print("Enter a valid expression...")

As others mentioned, the unsafe way is to use eval() , but a safe way could be正如其他人提到的,不安全的方法是使用eval() ,但一种安全的方法可能是

vals = input("Enter an operation: ").split()
num1 = float(vals[0])
op = vals[1]
num2 = float(vals[2])

# The rest of your code as you've written it

IIUC, you are looking to get the complete input from the user in one go, so that your calculator works without asking for inputs 3 times. IIUC,您希望在一个 go 中从用户那里获得完整的输入,这样您的计算器就可以在不要求输入 3 次的情况下工作。

NOTE: Using eval is highly unsafe and is very much discouraged.注意:使用eval非常不安全,非常不鼓励使用。 Your approach is absolutely fine and doesn't need eval() .您的方法绝对没问题,不需要eval() The question you are trying to solve is not related to eval() to begin with.您试图解决的问题与eval()无关。 Read more here . 在这里阅读更多。

Try this -尝试这个 -

num1, op, num2 = input('Enter equation with spaces in-between: ').split(' ')
num1, num2 = float(num1), float(num2)

if op == "+":
    print(num1 + num2)
elif op == "-":
    print(num1 - num2)
elif op == "*":
    print(num1 * num2)
elif op == "/":
    print(num1 / num2)

else:
    print(" Enter a valid operator...")
enter equation: 3 * 3
9.0

The str.split(' ') will break the string input into 3 parts based on the spaces. str.split(' ')将根据空格将字符串输入分成 3 部分。 You can then store these in variables, convert the num1 and num2 into float and use your function.然后您可以将这些存储在变量中,将num1num2转换为float并使用您的 function。

Use .split() .使用.split()

This works assuming there's a space separating each value and operator, and there are only two values involved.假设每个值和运算符之间有一个空格分隔,并且只涉及两个值,则此方法有效。

def equation():
    string = input("Enter an equation: ")
    #this splits the input string into its parts and puts them into a list
    string_as_list = string.split(' ')
    print(string_as_list)
    
    num1 = float(string_as_list[0])
    op = string_as_list[1]
    num2 = float(string_as_list[2])
    if op == "+":
        result = num1 + num2
    elif op == "-":
        result = num1 - num2
    elif op == "*":
        result = num1 * num2
    elif op == "/":
        result = num1 / num2
    
    return result

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM