TypeError: unsupported operand type(s) for -: 'str' and 'int'是什么意思?
我写了一个解题器,代码如下:
import time
while True:
print("欢迎使用三角形数表解题器")
time.sleep(1)
hang = input("请输入行")
lie = input("请输入列")
print((((hang + lie) - 1) * ((hang + lie) - 1)))
可运行了一半后,出现;
欢迎使用三角形数表解题器请输入行12请输入列3Traceback (most recent call last):File "C:\Users\Administrator\Documents\mindplus-py\user\2024-03-31-11-58-03\11111.py", line 10, in <module> print((((hang + lie) - 1) * ((hang + lie) - 1)))TypeError: unsupported operand type(s) for -: 'str' and 'int'
怎么办????????
while True:
print("欢迎使用三角形数表解题器")
hang = input("请输入行")
lie = input("请输入列")
hang = int(hang)
lie = int(lie)
print((((hang + lie) - 1) * ((hang + lie) - 1)))
这样就可以了(输入默认是str类型,需要转换成int)
当然,如果你嫌这个不简洁,可以用这个
while True:
print("欢迎使用三角形数表解题器")
hang = int(input("请输入行"))
lie = int(input("请输入列"))
print((hang + lie - 1)**2)
bug!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!1
页:
[1]