![](/img/trans.png)
[英]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]
[英]How to input a number x and print it x times, increasing by 2 each time
我需要将数字 x 作为输入并打印前 x 个奇数。 如果给出输入 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)
这是一个没有循环的解决方案。 它使用range
直接获取偶数,将这些整数转换为字符串并使用换行符作为分隔符一次显示它们:
n = int(input('Enter your number:'))
print('\n'.join(map(str,range(1,2*n,2))))
output 为 8 作为输入:
1
3
5
7
9
11
13
15
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.