创新互联Python教程:
编写一个 Python 程序,使用 for 循环范围执行列表乘法。在这个 Python 示例中,我们允许用户输入列表项。接下来,我们使用 for 循环范围(for i in range(listNumber))来迭代 multiList 列表项。在循环中,我们将每个列表项相乘并打印结果。
# List Multiplication
multiList = []
listNumber = int(input("Enter the Total List Items = "))
for i in range(1, listNumber + 1):
listValue = int(input("Enter the %d list Item = " %i))
multiList.append(listValue)
print("List Items = ", multiList)
listMultiplication = 1
for i in range(listNumber):
listMultiplication = listMultiplication * multiList[i]
print("The Muliplication of all teh List Items = ", listMultiplication)
# List Multiplication
multiList = []
listNumber = int(input("Enter the Total List Items = "))
for i in range(1, listNumber + 1):
listValue = int(input("Enter the %d List Item = " %i))
multiList.append(listValue)
print("List Items = ", multiList)
listMulti = 1
for num in multiList:
listMulti = listMulti * num
print("The Muliplication of all teh List Items = ", listMulti)
Enter the Total List Items = 5
Enter the 1 List Item = 10
Enter the 2 List Item = 4
Enter the 3 List Item = 9
Enter the 4 List Item = 11
Enter the 5 List Item = 7
List Items = [10, 4, 9, 11, 7]
The Muliplication of all teh List Items = 27720
这个 Python 程序使用 While 循环执行列表乘法。
# List Multiplication
multiList = []
listNumber = int(input("Enter the Total List Items = "))
for i in range(1, listNumber + 1):
listValue = int(input("Enter the %d List Item = " %i))
multiList.append(listValue)
print("List Items = ", multiList)
listMultiplication = 1
i = 0
while (i < listNumber):
listMultiplication = listMultiplication * multiList[i]
i = i + 1
print("The Multiplication of all the List Items = ", listMultiplication)
Enter the Total List Items = 4
Enter the 1 List Item = 9
Enter the 2 List Item = 10
Enter the 3 List Item = 2
Enter the 4 List Item = 4
List Items = [9, 10, 2, 4]
The Multiplication of all the List Items = 720
在这个 Python 列表的例子中,我们创建了一个 list 乘法(multiList)函数,返回结果 go 列表乘法。
# List Multiplication
def listMultiplication(multiList):
listMulti = 1
for num in multiList:
listMulti = listMulti * num
return listMulti
multiList = []
listNumber = int(input("Enter the Total List Items = "))
for i in range(1, listNumber + 1):
listValue = int(input("Enter the %d List Item = " %i))
multiList.append(listValue)
print("List Items = ", multiList)
listMultip = listMultiplication(multiList)
print("The Multiplication of all the List Items = ", listMultip)
Enter the Total List Items = 3
Enter the 1 List Item = 10
Enter the 2 List Item = 20
Enter the 3 List Item = 8
List Items = [10, 20, 8]
The Multiplication of all the List Items = 1600
新闻标题:Python程序:列表乘法
标题URL:http://www.csdahua.cn/qtweb/news9/366959.html
网站建设、网络推广公司-快上网,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 快上网