我想创建一个基本的程序来平方用户输入的数字,我想只通过导入命令来做到这一点,所以我创建了两个python文件= 1) constant.py :在这里,我已经创建了一个平方数字的函数def square( numbers ):2) main.py :当使用导入时,我想连接我的constant.py文件到main.py文件,只需查看代码。
在我的代码中,一切都很好。没有错误或警告发现,但当我运行这个程序时,我面临错误,无法平方的数字。为了停止这个错误,我使用了try和except命令,但不知道如何解决它。我正在使用VScode...
constant.py
def square(number):
return number * numbermain.py
import constant
try:
n = input(">> ")
print(constant.square(n))
except:
print("")
input("Press enter to exit") 发布于 2019-08-14 22:16:24
看起来你得到了一个类似下面这样的错误:
TypeError: can't multiply sequence by non-int of type 'str'因此,当您获得任何输入时,它通常以字符串的形式存储。在运行函数之前,请尝试将n转换为整数。像这样的东西应该是有效的:
import constant
n = int(input(">> "))
print(constant.square(n))https://stackoverflow.com/questions/57496393
复制相似问题