[英]Python localhost server/client network code to add/subtract/multiply/divide two numbers?
[英]How to make a python calculator with that can add , subtract , multiply , divide , powers and modulo on more more than two numbers
我是 Python 的新手,我想知道如何制作一個可以對兩個以上數字進行加、減、乘、除和其他運算符的計算器。 如果您能給我一個解釋,我將不勝感激。 我之所以質疑,是因為我認為這是一種糟糕且低效的方法,即為更多運算符和更多數字添加更多 elif 標簽
TL:DR(我猜):我想過用 python 制作一個計算器,它有更多運算符和數字的選項(但我不知道如何制作一個更簡單的計算器:即:30 + 30 * 30. 67.874 / 20 . 69 + 69 + 69 + 69 + 69 + 69. 30 ** (我認為這是一個冪運算符) 2. 等等 如果你不明白我想要什么,我可以幫助你,你可以問我
這是我的普通計算器,沒有輸入兩個以上的數字和一個運算符
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
num1 = float(input("Enter a number :"))
op = input("Enter your selected operator :")
num2 = float(input("Enter another number :"))
if op == "+":
print(num1, "+", num2, "=", add(num1, num2))
elif op == "-":
print(num1, "-", num2, "=", subtract(num1, num2))
elif op == "*":
print(num1, "*", num2, "=", multiply(num1, num2))
elif op == "/":
print(num1, "/", num2, "=", divide(num1, num2))
else:
print("Invalid input")
我知道這個評論/答案代碼沒有縮進,但是堆棧不會讓我用縮進發布,而且文件本身確實有縮進,所以 idk
result = None
operand = None
operator = None
wait_for_number = True
while True:
if operator == '=':
print(f"Result: {result}")
break
elif wait_for_number == True:
while True:
try:
operand = float(input("Enter number: "))
except ValueError:
print("Oops! It is not a number. Try again.")
else:
if result == None:
result = operand
else:
if operator == '+':
result = result + operand
elif operator == '-':
result = result - operand
elif operator == '*':
result = result * operand
elif operator == '/':
result = result / operand
break
wait_for_number = False
else:
while True:
operator = input("Enter one of operators +, -, *, /, =: ")
if operator in ('+', '-', '*', '/', '='):
break
else:
print("Oops! It is not a valid operator. Try again.")
wait_for_number = True
你的問題不夠清楚,但如果我做對了,這應該可行。 需要注意的重要一點:使用 eval() function 不是一個好習慣,因為如果輸入不是來自您,它可能非常危險。 以下是它的一些危險: https://nedbatchelder.com/blog/201206/eval_really_is_dangerous.html
編碼:
# the function takes an operator as a string like: “*” and multiple numbers thanks to the * (args) operator.
def calculate(operator, *numbers):
result = numbers[0]
#the for cycle goes through all numbers except the first
for number in range(1, len(numbers)):
#the eval() function makes it possible to python to interpret strings as a code part
result = eval(f"{result} {operator} {str(numbers[i])}")
print(result)
這是沒有 eval() 的代碼。
import operator
# the function takes an operator as a string like: “*” and multiple numbers thanks to the * (args) operator.
def calculate(op, *numbers):
# binding each operator to its string counterpart
operators = {
"+": operator.add,
"-": operator.sub,
"*": operator.mul,
"/": operator.truediv
}
result = numbers[0]
#the for cycle goes through all numbers except the first
for number in range(1, len(numbers)):
# carrying out the operation, using the dictionary above
result = operators[op](result, numbers[number])
print(result)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.