简体   繁体   English

Python 3 TypeError:**或pow()不支持的操作数类型:“ str”和“ int”

[英]Python 3 TypeError: unsupported operand type(s) for ** or pow(): 'str' and 'int'

#Import the module
from math import sqrt

#Using while loop statement to make the program not finish before the user close the program.
while True:

#Print out the introduction message, and get the input value to solve the quadratic equation.
    print("ax^2+bx+c=0의 꼴로 된 방정식을 풀 수 있습니다. a, b, c의 값을 차례대로 입력하세요.")
    a = input("a를 입력하세요 : ")
    b = input("b를 입력하세요 : ")
    c = input("c를 입력하세요 : ")

#Define function that checks whether the input values are natural number or negative number
    def func_num(n):
        if n[0] == '-':
            n = -int(n[1:])
        else:
            n = int(n)

#Execute the function for the input value a, b, c
    func_num(a); func_num(b); func_num(c);

#This if statement chekcs whether the solution of the quadratic equation going to be real number or imaginary number.
    if b ** 2 < 4*a*c:
        solution1 = ((sqrt((b ** 2)-(4*a*c)))-b) / (2*a)
        solution2 = (-(sqrt((b ** 2)-(4*a*c)))-b) / (2*a)
    else:
        square_root = sqrt( -(b**2 - 4*a*c) ) + 1j
        solution1 = ( (square_root)  - b  ) / (2*a)
        solution2 = ( -(square_root)  - b  ) / (2*a)

#Prints out the solution of the quadratic equation.
    print("정답은 바로바로... {}, {} 이거다!".format(solution1, solution2))

but it gives an error (for any input a, b, c that are integer): 但是会给出一个错误(对于整数的任何输入a,b,c):

Python 3 TypeError: unsupported operand type(s) for ** or pow(): 'str' and 'int'

I think the problem is related to func_num() (because that error occurs when I do the calculation with number and strings) 我认为问题与func_num()有关(因为在使用数字和字符串进行计算时会发生错误)

But I still can't find which part is wrong. 但是我仍然找不到哪一部分是错误的。

The reason giving u type error is you did not store and update the variables after calling the function. 输入u类型错误的原因是您在调用函数后没有存储和更新变量。

Try 尝试

 a = func_num(a)
 b = func_num(b)
 c = func_num(c)

And modify your function to return n because you only passing a copy n into the function 并修改您的函数以返回n,因为您仅将副本n传递给了函数

暂无
暂无

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

相关问题 类型错误:&lt;&lt;:&#39;str&#39; 和 &#39;int&#39; 的操作数类型不受支持 - TypeError: unsupported operand type(s) for <<: 'str' and 'int' TypeError:-:“ int”和“ str”不支持的操作数类型 - TypeError: unsupported operand type(s) for -: 'int' and 'str' 类型错误:不支持 / 的操作数类型:&#39;str&#39; 和 &#39;int&#39; (2) - TypeError: unsupported operand type(s) for /: 'str' and 'int' (2) TypeError:-:“ str”和“ int”的不受支持的操作数类型 - TypeError: unsupported operand type(s) for -: 'str' and 'int' TypeError:+ =不支持的操作数类型:“ int”和“ str” - TypeError: unsupported operand type(s) for +=: 'int' and 'str' TypeError:-:“ str”和“ int”的不受支持的操作数类型(Python) - TypeError: unsupported operand type(s) for -: 'str' and 'int' (Python) TypeError的Python问题:+:&#39;int&#39;和&#39;str&#39;的不支持的操作数类型 - Python problem with TypeError: unsupported operand type(s) for +: 'int' and 'str' 类型错误:不支持 - 的操作数类型:&#39;str&#39; 和 &#39;int&#39; [Python] - TypeError: unsupported operand type(s) for -: 'str' and 'int' [Python] Python Rainfall程序TypeError:+不支持的操作数类型:&#39;int&#39;和&#39;str&#39; - Python Rainfall program TypeError: unsupported operand type(s) for +: 'int' and 'str' Python TypeError:-:&#39;str&#39;和&#39;int&#39;不受支持的操作数类型 - Python TypeError: unsupported operand type(s) for -: 'str' and 'int'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM