Home > Mobile >  Multiply input by order and sum multiplied result then print
Multiply input by order and sum multiplied result then print

Time:04-19

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

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)

CodePudding user response:

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.

CodePudding user response:

If the number is presented as a string then:

num = '34532'

sum_ = sum(int(n)**i for i, n in enumerate(num, 1))

print(sum_)

Output:

257
  • Related