我有一个函数来检查一个数字是否是平方根:
def primeness(n):
for x in range(2, (n**0.5) + 1):
if n % x == 0:
return False
return True
def main():
n = input('Type a digit \n')
if primeness(n):
print(n, 'is a prime number')
else:
print(n, 'is not a prime number')
main()
但是,每当我运行它时,我都会收到此错误:
Traceback (most recent call last):
File "C:/Users/Matt/Desktop/Python Stuff/test.py", line 12, in <module>
main()
File "C:/Users/Matt/Desktop/Python Stuff/test.py", line 8, in main
if primeness(n):
File "C:/Users/Matt/Desktop/Python Stuff/test.py", line 2, in primeness
for x in range(2, (n**0.5) + 1):
TypeError: unsupported operand type(s) for ** or pow(): 'str' and 'float'
我究竟做错了什么?