-1

I am trying to create a python program that uses user input in an equation. When I run the program, it gives this error code, "answer = ((((A*10A)**2)(B*C))*D**E) TypeError: unsupported operand type(s) for ** or pow(): 'int' and 'str'". My code is:

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)`
John smith
  • 55
  • 1
  • 7

5 Answers5

1

The input() function returns a string value: you need to convert to a number using Decimal:

from decimal import Decimal

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

But your user might enter something that isn't a decimal number, so you might want to do some checking:

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)
Richard Inglis
  • 5,888
  • 2
  • 33
  • 37
0

The compiler thinks your inputs are of string type. You can wrap each of A, B, C, D, E with float() to cast the input into float type, provided you're actually inputting numbers at the terminal. This way, you're taking powers of float numbers instead of strings, which python doesn't know how to handle.

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: "))
Wesley
  • 1,412
  • 1
  • 15
  • 23
0

input() returns a string, you have to convert your inputs to integers (or floats, or decimals...) before you can use them in math equations. I'd suggest creating a separate function to wrap your inputs, e.g.:

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: ")
zwer
  • 24,943
  • 3
  • 48
  • 66
0

That code would run fine for python 2.7 I think you are using python 3.5+ so you have to cast the variable so this would become like this

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)

I tested it

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
Usama Jamil
  • 68
  • 10
0

there are three ways to fix it, either

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: "))

which limits you to integers (whole numbers)

or:

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 limits you to float numbers (which have numbers on both sides of the decimal point, which can act a bit weird)

the third way is not as recommended as the other two, as I am not sure if it works in python 3.x but it is

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: ")
3NiGMa
  • 545
  • 1
  • 9
  • 24