簡體   English   中英

列表打印字母而不是字符串?

[英]List prints letters and not strings?

我很難正確打印以下內容:

core = 1, 2, 3, 4, 5
glutes = 6, 7, 8, 9, 10
upper = 11, 12, 13, 14, 15
lower = 16, 17, 18, 19, 20
conditioning = 21, 22, 23, 24, 25

core_ability = int(input("Core: "))
glute_ability = int(input("Glutes: "))
if core_ability > 4:
        upper_ability = int(input("Upper body: "))
else:
        ""
lower_ability = int(input("Lower body: "))

conditioning_ability = int(input("\nConditioning ability level:"))

newcore = core[0:core_ability]
newglutes = glutes[0:glute_ability]
if core_ability > 4:
        newupper = upper[0:upper_ability]
newlower = lower[0:lower_ability]
newconditioning = conditioning[0:conditioning_ability]

if core_ability > 4:
        movement_bank = str(newcore) + str(newglutes) + str(newupper) + str(newlower) + str(conditioning_ability)
else:
       movement_bank = str(newcore) + str(newglutes) + str(newlower) + str(conditioning_ability)

sections = int(input("\nNumber of GPP sections in the session: "))

print("\nSPECIFY THE NUMBER OF MOVEMENTS PER SECTION")

if sections == 1:
        section1_num = int(input("Section 1:"))
        print(random.sample(movement_bank[0:], k=section1_num))

我得到一個輸出,看起來像:

' ', ' ', 'r'

當我想得到類似的東西時:

'1', '16', '8'

我在“movement_bank”列表中的每個列表中添加了“str()”,因為沒有它我得到一個錯誤:TypeError: can only concatenate list (not "int") to list。

非常感謝所有幫助。

看來,您有不同的列表,並希望將它們全部合並為一個列表。 使用extend

core = 1, 2, 3, 4, 5
glutes = 6, 7, 8, 9, 10
upper = 11, 12, 13, 14, 15
lower = 16, 17, 18, 19, 20
conditioning = 21, 22, 23, 24, 25

movement_bank = []
core_ability = int(input("Core: "))
movement_bank.extend(core[:core_ability])
glute_ability = int(input("Glutes: "))
movement_bank.extend(glutes[:glute_ability])
if core_ability > 4:
    upper_ability = int(input("Upper body: "))
    movement_bank.extend(upper[:upper_ability])
lower_ability = int(input("Lower body: "))
movement_bank.extend(lower[:lower_ability])
conditioning_ability = int(input("\nConditioning ability level:"))
movement_bank.extend(conditioning[:conditioning_ability])

sections = int(input("\nNumber of GPP sections in the session: "))
print("\nSPECIFY THE NUMBER OF MOVEMENTS PER SECTION")
if sections == 1:
    section1_num = int(input("Section 1:"))
    print(random.sample(movement_bank, k=section1_num))

暫無
暫無

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

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