我在 python3.9.1 中遇到了这个错误。我用 thonny 编码。
numbers = [12, 23, 37, 42, 59, 67]
#print(numbers)
#index = 0
#for i in numbers:
#print(numbers[index] + 19)
#ndex = index + 1
#for x in numbers:
#print(x)
#print(x * x)
variable = 34
print("The number of the variable is: " + str(variable))
print("The list numbers are: " + str(numbers))
new_number = input("Input a new number, but smaller than the numbers listed above: ")
for var in variable:
print(var * new_number)
请帮忙。 :)
答案 0 :(得分:0)
出现错误是正常的,您不能使用 int
数据类型的 for 循环,因此请使用范围函数:
numbers = [12, 23, 37, 42, 59, 67]
variable = 34
print("The number of the variable is: " + str(variable))
print("The list numbers are: " + str(numbers))
new_number = input("Input a new number, but smaller than the numbers listed above: ")
for var in range(variable):
print(var * new_number)
注意
如果将其转换为列表,您可以更好地理解 range 函数的工作原理:
variable = 34
print(list(range(variable)))
# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33]