0

i need to make a program that adds numbers, but asking first the user how many numbers he wants to add, and then adding them. any one knows how to do that

3
Contributors
4
Replies
5
Views
7 Years
Discussion Span
Last Post by snippsat
0

any one knows how to do that

Yes off course many off us now how to that,you should try something yourself.

Here is an explaination.
For first question use int(raw_input('how many questions: ') or python 3 int(input('...').
This will return an integer.
Then you make an empty list my_list = [ ].
So make for loop with range('..'),where you put variable from first question.
Then a new question inside the loop where you bind that question to a variable,that question will loop as many times as the first question variable return because you put it in a range function.
Then you append that question variable to the empty list my_list.append('...')
So outside the for loop you do sum(my_list)

Edited by snippsat: n/a

0

remember to use try to catch TypeError

Yes,we can take that later how to catch errors(Exception handling).
When you new is more important to get the code to run and understand the logic.
Then is a good exercice to try and catch all error a user input question can give.

Edited by snippsat: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.