It's a Python question

Asked 2 weeks ago, Updated 2 weeks ago, 1 views

I am a student studying Python.

My code for is

total = 0
while True:
    n = input()
    n_reverse = " "
    n1 = int(n)
    total += n1
    if n1 <= 0:
        break
    for i in n:
        n_reverse = i + n_reverse
    print(n_reverse)
print("Total :", total)

Yes. If you write the code and print it out, you can print it out one by one.

Is there a way to print it out at once like the example?

python

2022-09-20 14:39

1 Answers

If you look at the while statement, it's processing it right away and printing it out.

You can separate the input/output and rotate the loop twice.

So, we can only take the input part forward and save the input, and change the rest of the action to work in the for loop after the input is done.

I think I could have done it myself. Think about this a little more and solve it. That's the fun of programming.

# Input and save
inputs = []
while True:
    n = input()
    # # n_reverse = " "
    n1 = int(n)
    # # total += n1
    if n1 <= 0:
        break
    inputs.append(n)
    # # for i in n:
    #    #    n_reverse = i + n_reverse
    # # print(n_reverse)

# Apply logic to stored inputs

total = 0

for n in inputs:
    n_reverse = " "
    n1 = int(n)
    total += n1
    for i in n:
        n_reverse = i + n_reverse
    print(n_reverse)

print("Total :", total)


2022-09-20 14:39

If you have any answers or tips


© 2022 pinfo. All rights reserved.