簡體   English   中英

如何基於單個輸入創建列表

[英]How can i create a list based on a single input

我是編碼新手,從Python開始學習基礎知識。 通過代碼課程,並從我自己的簡單“初級聯賽球隊規划師”開始(針對我的兒子足球隊)。

我確實嘗試研究過我的問題,但是我找不到解決方案(如果由於缺乏專業知識而不知道要查找的內容,事情會變得很困難22)

問題

基於諸如total_matches之類的單個輸入字段,我想創建一個列表,其中包含每個匹配項的列表項

list_matches = ["match 1", "match 2", ....]

不知道從哪里開始。 我想到立即打印它們,然后使用一些重復的打印語句並使用輸入來創建計數,但是由於要擴展功能,因此需要在列表中。 我也希望獲得一些技巧,將列表替換為帶有日期等的實際比賽列表。

所以這就是我提示匹配量的方式

total_matches = 0
total_matches= int(raw_input("Enter the amount of matches your team has to play: "))

現在,我想創建一個列表,期望

matches = ["match 1", "match 2", ....]

對於Python 3+,請嘗試以下操作:

total_matches= int(input("Enter the amount of matches your team has to play: "))
matches = ['match '+str(k+1) for k in range(total_matches)]

對於Python 2+,您需要更改從用戶那里獲取輸入的方式。 休息是一樣的。

total_matches= int(raw_input("Enter the amount of matches your team has to play: "))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM