Python 3,循环中的变量

标签 python loops python-3.x variables

我是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/

相关文章:

linux - 连接硬编码目录和用户创建的文本文件会在不应该的时候添加根级路径

loops - 遍历 Haskell 中的两个变量

python不要在撇号后大写

python - 将 Lua 输出保存到 Python 字符串

python 基本 for 循环/列表理解

python - "Server Error in '/' Application"由于 Oracle.ManagedDataAccess.Client.Oracle.Exception : ORA-20002: Procedure(CA_Clasic_Search)Error using Selenium

Python:矢量化列表查找

python - 将二进制数据保存到 MySQLdb - Python

java - 如何检测一个元素是否是数组中的最后一个元素?

python - Windows 10 上的最新蚊子未运行