![](/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.