Python For 循环 - 枚举
Python For Loops - Enumerate
你能帮我解决这个问题吗?尝试了以下代码但不起作用。我正在尝试打印出附加的输出。提前致谢。
num_display = int(input('Please enter how many numbers you would like displayed: '))
inc_value = int(input('Please enter the increment value: '))
num_counter = 1
num_sequence = range(1, num_display + 1 , inc_value)
value_counter = range(1, num_counter, inc_value)
for each_num in num_sequence:
print(f'Counter: {num_counter} value: {each_num}')
num_counter = num_counter + 1
value_counter = value_counter + 1
print("Bye.")
输出应该是什么样的
Please enter how many numbers you would like displayed: 12
Please enter the increment value: 3
Counter: 1 value: 1
Counter: 2 value: 4
Counter: 3 value: 7
Counter: 4 value: 10
Counter: 5 value: 13
Counter: 6 value: 16
Counter: 7 value: 19
Counter: 8 value: 22
Counter: 9 value: 25
Counter: 10 value: 28
Counter: 11 value: 31
Counter: 12 value: 34
Bye.
我更改了代码以满足其他条件,但在将 str 转换为 int 后出现此错误:
第 11 行,在
num_sequence = 范围 (1,stop_value,inc_value)
类型错误:'str' 对象不能解释为整数
我的代码:
num_display = input('Please enter how many numbers you would like displayed: ').strip()
inc_value = input('Please enter the increment value: ').strip()
zero_value = 0
if num_display.isdigit() and inc_value.isdigit():
if int(num_display) != zero_value or int(inc_value) != zero_value:
stop_value = int(num_display) * int(inc_value)
num_sequence = range(1,stop_value,inc_value)
for num_counter, each_num in enumerate(num_sequence):
print(f'Counter: {num_counter + 1} value: {each_num}')
print('Bye.')
else:
print("I can't increment in steps of 0.")
else:
print('Positive non zero integers only please.')
我使用下面的方法让它工作,但它没有正确打印另一个,请看截图:
num_display = input('Please enter how many numbers you would like displayed: ').strip()
inc_value = input('Please enter the increment value: ').strip()
zero_value = 0
if num_display.isdigit() and inc_value.isdigit():
if int(num_display) != zero_value and int(inc_value) != zero_value:
stop_value = int(num_display) * int(inc_value)
num_sequence = range(1, int(stop_value), int(inc_value))
for num_counter, each_num in enumerate(num_sequence):
print(f'Counter: {num_counter + 1} value: {each_num}')
print('Bye.')
else:
print("I can't increment in steps of 0.")
else:
print('Positive non zero integers only please.')
working but not printing 5 on the other test
您的代码存在一些问题。 range()
会在每次迭代后自动将计数器增加 inc_value
,因此不要在循环中手动更新该计数器。
另外还有一道数学题。如果你想打印一个数字 num_display
次递增 inc_value
计算最后一个数字的公式(因此 range()
的 end
值)是 start_value + inc_value * num_display
,而不是 num_display + 1
。在你的情况下 start_value
是 1
.
要以 pythonic 方式获取迭代次数,请使用 enumerate()
。
num_display = int(input('Please enter how many numbers you would like displayed: '))
inc_value = int(input('Please enter the increment value: '))
start_value = 1
num_sequence = range(1, start_value + num_display * inc_value, inc_value)
for num_counter, each_num in enumerate(num_sequence):
print(f'Counter: {num_counter + 1} value: {each_num}')
print("Bye.")
预期输出
Please enter how many numbers you would like displayed: 12
Please enter the increment value: 3
Counter: 1 value: 1
Counter: 2 value: 4
Counter: 3 value: 7
Counter: 4 value: 10
Counter: 5 value: 13
Counter: 6 value: 16
Counter: 7 value: 19
Counter: 8 value: 22
Counter: 9 value: 25
Counter: 10 value: 28
Counter: 11 value: 31
Counter: 12 value: 34
Bye.
你能帮我解决这个问题吗?尝试了以下代码但不起作用。我正在尝试打印出附加的输出。提前致谢。
num_display = int(input('Please enter how many numbers you would like displayed: '))
inc_value = int(input('Please enter the increment value: '))
num_counter = 1
num_sequence = range(1, num_display + 1 , inc_value)
value_counter = range(1, num_counter, inc_value)
for each_num in num_sequence:
print(f'Counter: {num_counter} value: {each_num}')
num_counter = num_counter + 1
value_counter = value_counter + 1
print("Bye.")
输出应该是什么样的
Please enter how many numbers you would like displayed: 12
Please enter the increment value: 3
Counter: 1 value: 1
Counter: 2 value: 4
Counter: 3 value: 7
Counter: 4 value: 10
Counter: 5 value: 13
Counter: 6 value: 16
Counter: 7 value: 19
Counter: 8 value: 22
Counter: 9 value: 25
Counter: 10 value: 28
Counter: 11 value: 31
Counter: 12 value: 34
Bye.
我更改了代码以满足其他条件,但在将 str 转换为 int 后出现此错误: 第 11 行,在 num_sequence = 范围 (1,stop_value,inc_value) 类型错误:'str' 对象不能解释为整数
我的代码:
num_display = input('Please enter how many numbers you would like displayed: ').strip()
inc_value = input('Please enter the increment value: ').strip()
zero_value = 0
if num_display.isdigit() and inc_value.isdigit():
if int(num_display) != zero_value or int(inc_value) != zero_value:
stop_value = int(num_display) * int(inc_value)
num_sequence = range(1,stop_value,inc_value)
for num_counter, each_num in enumerate(num_sequence):
print(f'Counter: {num_counter + 1} value: {each_num}')
print('Bye.')
else:
print("I can't increment in steps of 0.")
else:
print('Positive non zero integers only please.')
我使用下面的方法让它工作,但它没有正确打印另一个,请看截图:
num_display = input('Please enter how many numbers you would like displayed: ').strip()
inc_value = input('Please enter the increment value: ').strip()
zero_value = 0
if num_display.isdigit() and inc_value.isdigit():
if int(num_display) != zero_value and int(inc_value) != zero_value:
stop_value = int(num_display) * int(inc_value)
num_sequence = range(1, int(stop_value), int(inc_value))
for num_counter, each_num in enumerate(num_sequence):
print(f'Counter: {num_counter + 1} value: {each_num}')
print('Bye.')
else:
print("I can't increment in steps of 0.")
else:
print('Positive non zero integers only please.')
working but not printing 5 on the other test
您的代码存在一些问题。 range()
会在每次迭代后自动将计数器增加 inc_value
,因此不要在循环中手动更新该计数器。
另外还有一道数学题。如果你想打印一个数字 num_display
次递增 inc_value
计算最后一个数字的公式(因此 range()
的 end
值)是 start_value + inc_value * num_display
,而不是 num_display + 1
。在你的情况下 start_value
是 1
.
要以 pythonic 方式获取迭代次数,请使用 enumerate()
。
num_display = int(input('Please enter how many numbers you would like displayed: '))
inc_value = int(input('Please enter the increment value: '))
start_value = 1
num_sequence = range(1, start_value + num_display * inc_value, inc_value)
for num_counter, each_num in enumerate(num_sequence):
print(f'Counter: {num_counter + 1} value: {each_num}')
print("Bye.")
预期输出
Please enter how many numbers you would like displayed: 12
Please enter the increment value: 3
Counter: 1 value: 1
Counter: 2 value: 4
Counter: 3 value: 7
Counter: 4 value: 10
Counter: 5 value: 13
Counter: 6 value: 16
Counter: 7 value: 19
Counter: 8 value: 22
Counter: 9 value: 25
Counter: 10 value: 28
Counter: 11 value: 31
Counter: 12 value: 34
Bye.