Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

I am expecting the product value should be assigned with value of 1 not 10, is there a way to assign the value of 10 and keep incrementing the same till while loop condition has been met.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Your expected output is not clear,

if you want the product=1 to be printed before all the multiplication:

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

you can add the print statement before the while loop

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))

if you want the following 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

print before multiplying

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 work only with higher versions of Python3

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading