简体   繁体   English

如何输入一个数字x并打印x次,每次增加2

[英]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.

相关问题 有没有办法根据用户在输入中写入的数字来决定 x 打印变量 x 次? [Python 3.8] - Is there a way to print a variable x times depending on what number the user wrote in the input to decide x? [Python 3.8] 如何根据用户输入打印字符串 x 次 - How to print a string x times based on user input 如果数字模式为x次 - If number patter for x times 我如何在 python 中将任何数字 x 次打印为列表? - How would I print out any number x times as a list in python? 显示每个输出在“X”中显示的次数 - Display the number of times each output was displayed in 'X's 读取和打印字符串x次 - Reading and print string x times 如何在Python-3.x中使用嵌套的for循环从文本文件一次打印一行,x个循环数? - How can I use nested for loops in Python-3.x to print from a text file one line at a time, x number of cycles? 您如何为文本文件编号,但在再次计数之前还要重复每个数字 x 次? - How do you number a text file but also repeat each number x amount of times before counting up again? 如何重试 x 次并打印长时间睡眠 - How to retry x times and put a long sleep with a print 如何打印一个字符串,显示字符串中每个字符重复的次数? - how to print a string showing the number of times each character is repeated in a string?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM