我怎么会收到这个错误?
我的代码:
def cat_n_times(s, n):
while s != 0:
print(n)
s = s - 1
text = input("What would you like the computer to repeat back to you: ")
num = input("How many times: ")
cat_n_times(num, text)
错误:
TypeError: unsupported operand type(s) for -: 'str' and 'int'
原文由 user285896 发布,翻译遵循 CC BY-SA 4.0 许可协议
失败的原因是因为 (Python 3)
input
返回一个字符串。要将其转换为整数,请使用int(some_string)
。您通常不会在 Python 中手动跟踪索引。实现这种功能的更好方法是
n
应该是 _次数_,s
应该是 _字符串_。