[英]How do I convert my output to list format?
我正在嘗試將我的 output 轉換為:
[5130, 5415, 5700, 5985, 6270, 6555, 6840, 7125, 7410, 7695, 7980]
但我只得到:
5130
5415
5700
5985
6270
6555
6840
7125
7410
7695
7980
我不知道如何轉換它。 我正在輸入數字 15。
我的代碼:
x = int(input("Enter your number here: "))
def inrange(x):
for i in range(5000,8000):
if i % x ==0 and i % (x +4)==0:
print(i)
inrange(x)
這是問題:創建一個程序,其中包含一個 function,它接受一個 integer 參數。 function 將打印可被 (1) integer 參數和 (2) 參數 + 4 整除的 5000 到 8000 之間所有值的列表。
如果您希望 function 返回包含您的項目的列表而不是打印它們,請嘗試:
x = int(input("Enter your number here: "))
def inrange(x):
results = []
for i in range(5000,8000):
if i % x ==0 and i % (x +4)==0:
results.append(i)
return results
inrange(x)
編輯:
此嘗試正在修改您的代碼,因此它可以工作。 如前所述,最簡單的方法是:
return [i for i in range(5000,8000) if i % x == 0 and i % (x + 4) == 0]
你可以使用 `print(i, end='') 讓你的項目水平打印。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.