如何用 python 中的输入解方程

How to solve and equation with inputs in python

我正在尝试创建一个 python 程序,该程序在方程式中使用用户输入。当我 运行 程序时,它给出了这个错误代码 "answer = ((((A*10A)**2)(B*C))*D**E) TypeError: unsupported operand type(s) for ** or pow(): 'int' and 'str'"。我的代码是:

import cmath

A = input("Enter a number for A: ")
B = input("Enter a number for B: ")
C = input("Enter a number for C: ")
D = input("Enter a number for D: ")
E = input("Enter a number for E: ")

answer = ((((A*10**A)**2)**(B*C))*D**E)
print(answer)`

编译器认为你的输入是字符串类型。您可以将 A、B、C、D、E 中的每一个都用 float() 包装起来,以将输入转换为 float 类型,前提是您实际上是在终端输入数字。这样,您将获得浮点数而不是字符串的权力,python 不知道如何处理。

A = float(input("Enter a number for A: "))
B = float(input("Enter a number for B: "))
C = float(input("Enter a number for C: "))
D = float(input("Enter a number for D: "))
E = float(input("Enter a number for E: "))

input() 函数 returns 字符串值:您需要使用 Decimal:

转换为数字
from decimal import Decimal

A = Decimal(input("Enter a number for A: "))
# ... etc

但是您的用户输入的可能不是十进制数,因此您可能需要进行一些检查:

from decimal import Decimal, InvalidOperation

def get_decimal_input(variableName):
    x = None
    while x is None:
        try:
            x = Decimal(input('Enter a number for ' + variableName + ': '))
        except InvalidOperation:
            print("That's not a number")
    return x

A = get_decimal_input('A')
B = get_decimal_input('B')
C = get_decimal_input('C')
D = get_decimal_input('D')
E = get_decimal_input('E')

print((((A * 10 ** A) ** 2) ** (B * C)) * D ** E)

input() returns a string, you have to convert your inputs to integers (or floats, or decimals...),然后才能在数学方程式中使用它们。我建议创建一个单独的函数来包装您的输入,例如:

def num_input(msg):
    # you can also do some basic validation before returning the value
    return int(input(msg))  # or float(...), or decimal.Decimal(...) ...

A = num_input("Enter a number for A: ")
B = num_input("Enter a number for B: ")
C = num_input("Enter a number for C: ")
D = num_input("Enter a number for D: ")
E = num_input("Enter a number for E: ")

该代码 运行 适用于 python 2.7 我认为您使用的是 python 3.5+,因此您必须强制转换变量,这样就会变成这样

import cmath

A = int(input("Enter a number for A: "))
B = int(input("Enter a number for B: "))
C = int(input("Enter a number for C: "))
D = int(input("Enter a number for D: "))
E = int(input("Enter a number for E: "))

answer = ((((A*10**A)**2)**(B*C))*D**E)
print(answer)

我测试过

Enter a number for A: 2
Enter a number for B: 2
Enter a number for C: 2
Enter a number for D: 2
Enter a number for E: 2
10240000000000000000

有三种方法可以修复它,

A = int(input("Enter a number for A: "))
B = int(input("Enter a number for B: "))
C = int(input("Enter a number for C: "))
D = int(input("Enter a number for D: "))
E = int(input("Enter a number for E: "))

这将您限制为整数(整数)

或:

A = float(input("Enter a number for A: "))
B = float(input("Enter a number for B: "))
C = float(input("Enter a number for C: "))
D = float(input("Enter a number for D: "))
E = float(input("Enter a number for E: "))

限制你只能使用浮点数(小数点两边都有数字,which can act a bit weird

第三种方法不如其他两种推荐,因为我不确定它是否适用于 python 3.x 但它是

A = num_input("Enter a number for A: ")
B = num_input("Enter a number for B: ")
C = num_input("Enter a number for C: ")
D = num_input("Enter a number for D: ")
E = num_input("Enter a number for E: ")