Python coding question?

Given a series of numbers as input, add them up until the input is 10 and print the total. I used the code below to answer this question, but it is not returning output for me.

Do not add the final 10.

num_str = input("Input an int: ")

num_int = int(num_str)

x = 0

while num_int != 10:

x += num_int - 10

if num_int == 10:

print(x)

4 Answers

Relevance
  • Anonymous
    9 months ago

    total = 0

    while True:

    . user_input = input("Input an int: ")

    . if ("10" == user_input): break

    . total += int(user_input)

    print(total)

    • Login to reply the answers
  • VP
    Lv 7
    9 months ago

    Your assignment doesn't seem right to me. Is the assignment to:

    a. Keep adding randomly entered integers until the user [magicially] enters a '10' then print the sum?

    b. Ask the user to enter 10 random integers, then print the sum.

    c. Whatever you're doing?

    In the small chance that you paraphrased the assignment, maybe you could tell us the original text of the assignment. Also, if you have some input data you're supposed to use -- show that to us, too.

    • Login to reply the answers
  • John
    Lv 7
    9 months ago

    total = 0

    while True:

    . user_input = input("Input an int: ")

    . if ("10" == user_input): break

    . total += int(user_input)

    print(total)

    • Login to reply the answers
  • 9 months ago

    Hi, Kevin and Ginny.

    You need your input to be inside your while loop.

    total = 0

    num = 0

    while (num != 10):

    ... num = int (input ("Enter an integer: "))

    ... if (num == 10): break

    ... total += num

    print ("The total is", total)

    • Login to reply the answers
Still have questions? Get your answers by asking now.