简体   繁体   中英

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. If input 8 was given, the output would be: 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:

n = int(input('Enter your number:'))
print('\n'.join(map(str,range(1,2*n,2))))

output for 8 as input:

1
3
5
7
9
11
13
15

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM