[英]Can I ask the user to input any basic arithmetic equation without having to ask for every individual number to be computed?
I am a complete beginner in coding.我是编码的完整初学者。 I want to make a simple math calculator using Python.
我想用 Python 做一个简单的数学计算器。 Instead of the traditional method of asking the user to input 2 separate integers (in this example addition)
而不是要求用户输入 2 个单独的整数的传统方法(在此示例中添加)
num1 = int(input("Enter a number: "))
num2 = int(input("Enter a number: "))
total = num1 + num2
print(total)
I would like the user to immediately input我希望用户立即输入
1+1
or any other simple math that may involve one or a combination of the following: addition, subtraction, multiplication, division.或任何其他可能涉及以下一项或多项的简单数学:加法、减法、乘法、除法。 Is it possible to do so?
有可能这样做吗?
you could also separate the input into a list.您还可以将输入分成一个列表。 For example, when you input "1 + 1" there are functions in Python to separate all the characters in the list.
例如,当您输入“1 + 1”时,Python 中有函数可以分隔列表中的所有字符。 It can make it ["1", "+", "1"].
它可以使它成为[“1”,“+”,“1”]。 With this, you can read each individual character and use them like input[2] to find the operation.
有了这个,您可以读取每个单独的字符并像 input[2] 一样使用它们来查找操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.