当我尝试打印列表中的项目时,它永远不会运行第一个if语句。这是我的代码。 number_of_numbers_multiplication
等于number_of_numbers_multiplication
所以它不应该转到第二个声明吗?
numbers = [3,2,1]
how_many_multiplication = 3
number_of_numbers_multiplication = int(how_many_multiplication)
first = True
for multiplication_printer in range (1,number_of_numbers_multiplication):
if multiplication_printer == number_of_numbers_multiplication:
print(numbers[multiplication_printer])
elif multiplication_printer > 1 and multiplication_printer != number_of_numbers_multiplication:
print(numbers[multiplication_printer],'multiplied by,')
elif first == True:
print(numbers[0],'multiplied by,')
print(numbers[1],'multiplied by,')
first = False;
请帮助
答案 0 :(得分:1)
这很正常。 range function的停止参数number_of_numbers_multiplication
是独占的。如果您想要覆盖它,请使用range(1,number_of_numbers_multiplication+1)
。
编辑:现在,由于您更改了number_of_numbers_multiplication的值并且它等于3.解决方案是使用range(1,number_of_numbers_multiplication)
但使用if multiplication_printer==number_of_numbers_multiplication-1
编辑2 要记住的重要事项是:
1. multiplication_printer
用作索引,因此必须在list-1的长度(3-1 = 2)处停止。
2.范围的stop参数是独占的,因此如果multiplication_printer
必须在2处停止,则参数必须等于3.