Question to print until python 0.

Asked 2 years ago, Updated 2 years ago, 74 views

list=input().split()

for x in list:
    if int(x) == 0:
        break
    else:
        print(x)

a=input().split()

for x in a:
    if int(x) == 0:
        break
    else:
        print(x) 

I wonder the difference between receiving the list and a and which method would be better. And

a=input().split()

for x in a:
    print(x)
    if int(x) == 0:
        break

And zero shouldn't be printed out I want to know why 0 is output and ends when I run .

coding python

2022-09-21 10:04

1 Answers

list=input().split()

for x in list:
    if int(x) == 0:
        break
    else:
        print(x)

a=input().split()

for x in a:
    if int(x) == 0:
        break
    else:
        print(x)

There is no difference between list and a in this code. They're both the same.

a=input().split()

for x in a:
    print(x)
    if int(x) == 0:
        break

Take a good look at the order in which it is executed.
There is print(x) before the conditional statement, so it is also printed when x = 0.


2022-09-21 10:04

If you have any answers or tips

Popular Tags
python x 4647
android x 1593
java x 1494
javascript x 1427
c x 927
c++ x 878
ruby-on-rails x 696
php x 692
python3 x 685
html x 656

© 2024 OneMinuteCode. All rights reserved.