def product(x): total = 1 for i in range(len(x)): total = total * x[i] return total number_list = [1, 2, 3, 1, 10 ] print product(number_list) choose = str(raw_input("What are your numbers: "))
Indenting is crucial in Python.
Any lines of code that you want to have included in your product function must be indented. In the following image, because you stopped indenting,
total = 1 is the last line of your function.
The above code seems needlessly overcomplicated
here's my two cents:
def product(numbers): total = 1 for i in numbers: total *= i return total
or an even shorter version:
def product(numbers): total = 1 for i in numbers: total*=i return(total)
Hey, how can i get my raw input to take more than 1 number: It returns an error if i try entering more than 1 number....Thanks!
def product(numbers): total = 1 for n in numbers: total *= n return total numbers = str(raw_input("What are your numbers: ")) print product(numbers)
Thanks, that was really helpful!
I've tried it by making the
total = 0, it was a challenge lol, managed to get it working though,
this is what I came up with:
def product(uInput): total = 0 length = len(uInput) if length <= 1: total = uInput elif length <= 2: total = uInput * uInput else: total = uInput * uInput uInput.pop(0) uInput.pop(0) for j in uInput: total = total * j return total print product([4,5,5])
thanks marmaa...good code here..
If you want to do that here is my solution but it is in python 3x should still work in python 2x with a small bit of fiddling
def product(numbers): total = 1 for i in numbers: total*=i print("Product of",numbers,) print("Is :",total,) def getinput(): userInput = input("Enter String of numbers \ seperated by spaces\n :> ") print("\n") chngNum = map(int, userInput.split()) numbers=  for i in chngNum: numbers.append(i) return numbers product(getinput())
Here's how I did it
result = 1
sequence = list(sequence)
for i in sequence:
if i == int(i):
result *= i