the question asks Write a code that prints the summation of 1 to value . You can assume that value will be positive. For example, summation for user input 5 would print 15 (1 + 2 + 3 + 4 + 5). we are supposed to do this using why loops
the second question is the same but multiplying the numbers to a value
this is what i have but it when it runs nothing happens it just goes down a line without running anything
def summation(n):
count = 0
num = 0
while count < n:
count+=1
num+=count
return num
print (summation)
Copyright © 2024 Q2A.ES - All rights reserved.
Answers & Comments
Funny, I get <function summation at 0x02290D20>, but maybe I guessed wrong at the indentation. If you're seeing nothing at all, then maybe that print statement is indented to be part of the summation function.
Besides indentation, the other problem is that the summation function call needs an argument in parentheses. Even if there's no argument, by the way, you still need the parens. Use func() to call a function named "func" or func alone to refer to the function object that implements the function.
Try this for an indented, fixed version. (.... tokens used for indentation)
def summation(n):
.... count = 0
.... num = 0
.... while count < n:
.... .... count+=1
.... .... num+=count
.... return num
n = int(input("Enter a value for n: ")) # get a value to pass
print (summation(n)) # call summation with that value and print result
By the way, summation could be written as return n*(n+1)/2, or sum(range(n)), depending on how much math and/or Python you know.
You need to call the summation function with an argument (which means you also need to write the code to ask the user to input a number).