[英]Using python to sum n terms of a sequence using only for and while loops
[英]Printing a sequence with the amount of terms based on input using for-loops in Python
那么我將如何使用 for 循環創建一個像這樣的簡單程序呢? 它會要求用戶輸入,然后打印以下模式以獲得適當數量的術語,並且它不能使用 if-else 語句
例子:
你想要幾個條款?
輸入:9
Output:1 1 3 3 5 5 7 7 9
我希望你能自己弄清楚如何輸入一個數字。 以下 for 循環的使用創建了您想要的序列:
users_input = 9
print("Output: ",end="")
for i in range(users_input):
print(1+2*(i//2),end=" ")
print()
Output:1 1 3 3 5 5 7 7 9
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.