0

I want to make easy program with loop but can't find out how. I am absolute beginner. So basicly program should add all numbers person writes to it. For example:

Person writes: 5

Program shows: 5

Person writes: 6

Program shows: 11

Person writes: 3

Program shows: 14 

and so on. This is what I did and it's wrong I don't know how to write it right.

while True:
    var1 = int(input("Write a number"))
    var2 = 0 + var1
    print(var2)
1
  • 1
    Hints: What do you think var2 = 0 + var1 does, and why did you write that? When and why do you think it might be a good idea to set var2 to 0? When and why do you think it might be a good idea to add something to var2? Commented Jul 18, 2015 at 12:41

4 Answers 4

3

This is the code you are looking for, when the user insert a non digit input, i'm printing bad input and the user can try again.

good luck!

total = 0
while True:
    try:
        user_input = int(input("enter number"))
    except ValueError:
        print ("bad input, try again")      # in case of the input is not a number
        continue
    total += int(user_input)
    print (total)
Sign up to request clarification or add additional context in comments.

5 Comments

As per my guess OP is using python 3 so no raw _input and print is function
@VigneshKalai tnx, i fixed it.
Use a try/except, -5 is a number
@PadraicCunningham , True that.. fixed.
it looks really hard to me as it's my first day I am learning to programm but thank you :)
1
userInput = None # this variable will handle input from user
numberSum = 0 # this is sum of all your numbers
print("Type 'exit' to quit")
while True:
    userInput = input("Enter your number") # here you are taking numbers from user
    if userInput == 'exit': # if user enters 'exit' instead of number, program will call break
        break # break ends loop
    numberSum += int(userInput) # this will add int(userInput) to your numberSum. int() function
                # makes integer type from your String. You have to use it, because
                # while getting input() from user, it's considered as string
print("Your sum is: ")
print(numberSum) # printing your sum
print("byebye...")

Note, if you type anything else than 'exit' or number, the program will exit with ValueError

Comments

0

You should initialize your sum holder (var1) outside the loop and in order to update the sum holder, you should add the newest input to it.

>>> var1 = 0
>>> while True:
...    var1 += int( input('Write a number: ') )
...    print(var1)
...

Write a number: 1
1
Write a number: 2
3
Write a number: 4
7
Write a number: -6
1

Comments

0

In your program each and every time you are updating var2 value with var1 value so the user input is printed

What you have to do is add the value var1 with var2 value .Initially var2 value is zero so it is equated to var1 and so on

var2=0
while True:

    var1 = int(input("Write a number"))

    var2 += var1

    print(var2)

3 Comments

now I got it. One more thing I get this error, how to fix it ?
File "t2.py", line 3 var1 = int(input("Write a number") ^ IndentationError: expected an indented block
@Pexlu python is indent based language you have to provide proper indentation and you have use tab or space not both compined

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.