Python 列表打印字母而不是字符串?
我很难正确打印以下内容:Python 列表打印字母而不是字符串?,python,Python,我很难正确打印以下内容: 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:
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'
我将“str()”添加到“movement_bank”列表中的每个列表中,因为如果没有它,我会得到一个错误:TypeError:只能将列表(而不是“int”)连接到列表
非常感谢您提供的所有帮助。您似乎有不同的列表,希望将它们合并到一个列表中。 使用
扩展:
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))
您的代码非常混乱,并且有许多不必要的部分,这使得理解代码变得更加困难。对于您遇到的问题,请显示movement\u bank
的值。请注意,random.sample
永远不能返回'16'
,因为字符串中的random.sample
始终返回单字符子字符串。请确保发布一个字符串。你的代码应该是一个例子,也就是说,它不应该是你真正的代码,它包含了所有与问题无关的东西。它应该是一个说明问题的例子,并且只说明问题。它应该是最小的,也就是说,应该没有任何东西不是绝对需要证明问题。它应该是可复制的,也就是说,我应该能够复制、粘贴和运行它,并且我应该立即意识到,结果是错误的,正确的结果是什么。我真诚的道歉,我显然有很多东西要学习。我非常感谢您的反馈,并将尽我最大的努力在将来发布时澄清。例如,我完全不清楚为什么'','r'
是错误的结果,也不清楚为什么'1','16','8'
是正确的结果。此外,您的代码依赖于外部输入,并且完全不清楚哪个输入是正确的结果。或者这是所有输入的正确结果?