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

Multiply input by order and sum multiplied result then print

Hey guys I’m new to python and right now I’m stuck in this:
Get user number input like "98751", then multiply them by order and sum the multiplied result like this:

"9^1" + "8^2" + "7^3" + "5^4" + "1^5"

So far I got here without input:

num = 34532
x = [int(a) for a in str(num)]
print(x)
a=1
multilist = [number * a for number in x]
print(multilist)

then print the result.
Thanks in advance

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

Edit:
final result for whoever needs it, thanks to Jhanzaib Humayun

num = input("Please enter desired number: ")
sumn = 0
for i in range(len(num)):
    sumn+=int(num[i])**(i+1)

s = [int(n)*(i+1) for i,n in enumerate(num)]
print("Multiply result by order:")
print(s)
print("Final result:")
print(sumn)

>Solution :

You can use something like this:

num = input("Input a number: ")
sum = 0
for i in range(len(num)):
    sum+=int(num[i])**(i+1)
print(sum)

Edit: You can use something like this if you want to use list comprehension

num = "34532"
s = sum([int(n)*(i+1) for i,n in enumerate(num)])
print(s)

By using enumerate, you get the index i and the element n. Which you can use according to your needs, and lastly sum can be used to sum upp all the elements in the array.

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