[英]How to input a number x and print it x times, increasing by 2 each time
I need to take number x as input and print the first x odd numbers.我需要将数字 x 作为输入并打印前 x 个奇数。 If input 8 was given, the output would be: 1 3 5 7 9 11 13 15.
如果给出输入 8,则 output 将是:1 3 5 7 9 11 13 15。
x = int(input('Enter your number:'))
for i in range(2*x):
if i % 2 == 1:
print(i)
Here is a solution without loop.这是一个没有循环的解决方案。 It uses
range
to get directly the even numbers, converts those integers to string and displays them all at once using newlines as separator:它使用
range
直接获取偶数,将这些整数转换为字符串并使用换行符作为分隔符一次显示它们:
n = int(input('Enter your number:'))
print('\n'.join(map(str,range(1,2*n,2))))
output for 8 as input: output 为 8 作为输入:
1
3
5
7
9
11
13
15
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.