繁体   English   中英

试图打印索引值的 position

[英]Trying to print position of index value

Code

lst = [10, 20, 30, 40, 50] #We have list of products here


product = 1
index = 0

while index < len(lst):
    print("================================================================================")
    print("The current index position is :", index)
    product *= lst[index]
    print("The product holds the value   :", product, "is been multplied with value of :{}".format(lst[index]))
    index +=1
    print("================================================================================")
    print("\n\nPrinting the next while condtion")

print("\n\nWhile Condition has been completed since length condition {}".format(len(lst)), "has been met")
print("\nProduct is : {}".format(product))

Output

================================================================================
The current index position is : 0
The product holds the value   : 10 is been multplied with value of :10
================================================================================
Printing the next while condtion
================================================================================
The current index position is : 1
The product holds the value   : 200 is been multplied with value of :20
================================================================================
Printing the next while condtion
================================================================================
The current index position is : 2
The product holds the value   : 6000 is been multplied with value of :30
================================================================================
Printing the next while condtion
================================================================================
The current index position is : 3
The product holds the value   : 240000 is been multplied with value of :40
================================================================================
Printing the next while condtion
================================================================================
The current index position is : 4
The product holds the value   : 12000000 is been multplied with value of :50
================================================================================
Printing the next while condtion
While Condition has been completed since length condition 5 has been met
Product is : 12000000

我期望产品值应该分配为 1 而不是 10,有没有办法分配 10 的值并保持递增直到满足 while 循环条件。

您的预期 output 不清楚,

如果您希望在所有乘法之前打印 product=1:

Initial value of product is : 1

================================================================================
The current index position is : 0
The product holds the value   : 10 is been multplied with value of :10
================================================================================


Printing the next while condtion
================================================================================
The current index position is : 1
The product holds the value   : 200 is been multplied with value of :20
================================================================================


Printing the next while condtion
================================================================================
The current index position is : 2
The product holds the value   : 6000 is been multplied with value of :30
================================================================================


Printing the next while condtion
================================================================================
The current index position is : 3
The product holds the value   : 240000 is been multplied with value of :40
================================================================================


Printing the next while condtion
================================================================================
The current index position is : 4
The product holds the value   : 12000000 is been multplied with value of :50
================================================================================


Printing the next while condtion


While Condition has been completed since length condition 5 has been met

Product is : 12000000

您可以在 while 循环之前添加打印语句

lst = [10, 20, 30, 40, 50] #We have list of products here


product = 1
index = 0

print(f'Initial value of product is : {product}\n')

while index < len(lst):
    print("================================================================================")
    print("The current index position is :", index)
    product *= lst[index]
    print("The product holds the value   :", product, "is been multplied with value of :{}".format(lst[index]))
    index +=1
    print("================================================================================")
    print("\n\nPrinting the next while condtion")

print("\n\nWhile Condition has been completed since length condition {}".format(len(lst)), "has been met")
print("\nProduct is : {}".format(product))

如果您想要以下 output

================================================================================
The current index position is : 0
Current product value is: 1
Multiplying the current product with value 10
================================================================================


Printing the next while condtion
================================================================================
The current index position is : 1
Current product value is: 10
Multiplying the current product with value 20
================================================================================


Printing the next while condtion
================================================================================
The current index position is : 2
Current product value is: 200
Multiplying the current product with value 30
================================================================================


Printing the next while condtion
================================================================================
The current index position is : 3
Current product value is: 6000
Multiplying the current product with value 40
================================================================================


Printing the next while condtion
================================================================================
The current index position is : 4
Current product value is: 240000
Multiplying the current product with value 50
================================================================================


Printing the next while condtion


While Condition has been completed since length condition 5 has been met

Product is : 12000000

乘法前打印

lst = [10, 20, 30, 40, 50] #We have list of products here


product = 1
index = 0


while index < len(lst):
    print("================================================================================")
    print("The current index position is :", index)
    print(f'Current product value is: {product}')
    print(f'Multiplying the current product with value {lst[index]}')
    product *= lst[index]
    # print("The product holds the value   :", product, "is been multplied with value of :{}".format(lst[index]))
    index +=1
    print("================================================================================")
    print("\n\nPrinting the next while condtion")

print("\n\nWhile Condition has been completed since length condition {}".format(len(lst)), "has been met")
print("\nProduct is : {}".format(product))

PS。 f-strings 仅适用于更高版本的 Python3

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM