【发布时间】:2019-11-26 05:19:14
【问题描述】:
我是 Python 新手,在浏览了一个使用函数将数字提升到特定指数的示例之后,我尝试将其作为数字的用户,而不是仅仅将它们输入到代码中。
以下是我所拥有的:
def raise_to_power(base_num, pow_num):
int(base_num)
int(pow_num)
result = 1
for index in range(pow_num):
result = result * base_num2
return result
base_num = input("What number would you
like to exponentially raise?: ")
pow_num = input("To what power?: ")
print(raise_to_power(base_num, pow_num))`
当我尝试运行它时,我得到了下面列出的错误:
Traceback (most recent call last):
File "C:/Users/PycharmProjects/Exponent
Function.py", line 17, in <module>
print(raise_to_power(base_num, pow_num))
File "C:/Users/PycharmProjects/Exponent
Function.py", line 7, in raise_to_power
for index in range(pow_num):
TypeError: 'str' object cannot be
interpreted as an integer
有人可以解释为什么会发生这种情况吗?
谢谢,
【问题讨论】:
-
将字符串输入转换为整数。
-
提示:在你发布问题之前,谷歌错误信息,这已经被问和回答了 100 次。
-
pow_num = int(pow_num)
标签: python string function integer