我是Python新手,只是玩玩,用这段代码学习不同的功能。(请阅读注释)
less = ["science", "maths"]
for i in range(0,len(less)):
a = ("You have got; " + (less[i]))
b = (a)
#By putting in print here I figured out that it added science first then maths overrides it.
print(b)
print (b)
当你打印字符串时,它说:
“你已经掌握了;数学”
但我试图让它说:
“你已经掌握了;科学数学”
基本上,我试图找到一种方法来添加到变量中,而不是一个方法覆盖另一个变量,所以当我最后打印它时,它将同时包含科学和数学。
我将对其进行扩展,这就是为什么我需要列表的长度、循环等。
最佳答案
您可以简单地迭代列表中的对象,并使 print
语句不会终止该行。
less = ["science", "maths"]
print ("You have got; ", end="")
for i in less:
print (i, end=" ")
关于Python 3,循环中的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39117991/