### Archive

Posts Tagged ‘langu’

## Python Project

Python: Sum n numbers

Bites of Python exercises

I found this Python Exercises on the at PyBites codechalleng and i love the idea of solving problems and this will let me learn more, so thanks guys @pybites.

So this time the challenge is to Write a function that can sum up N numbers. The function should receive a list of N numbers, If no argument is provided, return sum of numbers 1..100.

I hope i did this right, here is my function; the result seems to be fine i am still learning so … sorry if i misunderstood.

def sum_up(my_nums=range(1,100)):

total=0

print”The List of number/s “,my_nums

for x in range (len(my_nums)):

total=total+int(my_nums[x])

print”Total is:”,total

print(“This function will sum up a given list of numbers.\nWhen you finish typping the numbers just press “”Enter””\nIf you did not enter any numbers i will assume the list is [1,100]”)

#Get the list from the user
n=input(“Type the numbers:”)

#In case the user enter some spaces in his number, we should remove it
the_n=n.replace(” “, “”)

#Here we are calling the functin
sum_up() if the_n ==”” else sum_up(the_n)