1 / 23
Jun 2004

while 1 :
        a = int(raw_input())
        if a == 42:
                break;
        print a
  • created

    Jun '04
  • last reply

    Dec '21
  • 22

    replies

  • 5.8k

    views

  • 19

    users

  • 3

    links

2 years later

k=raw_input()
while int(k)!=42:
	print k
	k=raw_input()

or, better yet,

k=input()
while k!=42:
	print k
	k=input()

(input() automatically evaluates input; this is handy for getting a single line of integer input)

4 months later

[quote="nneonneo"]

k=raw_input()
while int(k)!=42:
	print k
	k=raw_input()

raw_input is slower than file generator significantly. Are there any other ways to do input/output?

7 months later

why the code is wrong:

num = 1
while( num ) :
	num = int( raw_input())
	if ( num == 42 ):
		break
	print  num

but this works:

while( 1 ) :
	num = int( raw_input())
	if ( num == 42 ):
		break
	print  num
2 years later

	from itertools import takewhile
	from sys import stdin, stdout
	map(stdout.write, takewhile('42\n'.__ne__, stdin.readlines()))
5 months later

for line in iter(raw_input, '42'):
    print line

Haven't seen that one here.

2 years later

If I read correctly, it asks to stop processing input after reading in the number 42. I have done so here:

unsolved = 1
while 1:
	a = int(raw_input())
	if a != 42 and unsolved:
		print a
	else:
		unsolved = 0

Yet, this is incorrect?

As in the example:

Input:
1
2
88
42
99 <--- After 42, still accepting input.

Output:
1
2
88
<--- but not outputting.

Why is this?

Hi mur_quark,

how do you expect your code to finish (terminate)? Your while loop will keep reading input forever, that is, until it runs into an EOFError: EOF when reading a line since input obviously has to be finite wink

Cheers daft

1 year later

What a shame , below program is wrong. The machine can not recognize py file?

!/usr/bin/env python

-- coding: utf-8 --

def main():
while True:
x = raw_input("Enter a number(within 2 digit):")

    try:
        y = int(x)
    except ValueError:
        continue

    if len(x) != 1 and len(x) != 2:
        continue
    elif y == 42:
        break
    else:
        print y

if name =="main":
main()

6 months later

Hi ,

I have tried this code

number = []
a = 0
while a <= 99:
        a = int(raw_input())
        number.append(a)
for i in number:
        if i == 42:
                exit()
        print i

Works well in my system, but SPOJ shows runtime error

All of the numbers will be less than or equal to 99 as given in the problem statement.

42

For the above given input your code will read 42, append it to a, then, since a was <= 99 will try to execute raw_input again which returns an EOF error.

Also, there is no given constraint on the amount of numbers in the input. There could be millions. You're told to read and print and stop when the number is 42. You run the risk of getting an out of memory exception by filling up number.

1 year later

It would be more helpful to post your code (using the correct format tags) as text, not as a screenshot.

The code given above is much too complicated. E.g you do not need to check the boundaries (-100 .. 100) at SPOJ, the input will be within the range given in the problem statement.

The while loop at the end of your program will process input after input without printing anything...

Sorry, I'm pretty new to this whole thing. Could you teach me how to write my code as text here please?

I see.. I had a much simpler solution to the problem that didn't include checking for the boundaries, but I got it wrong, and reading the question again I saw that "All numbers at input are integers of one or two digits", and so I thought that was where I went wrong. Regarding the while loop, as can be seen in the "Example" section of the question, "99" is inputed (after 42) but not printed as output, as my programme would do.

Could you please assist me with seeing the errors in the paragraph above this? Or if you have any time constraints, directly messaging me the solution would help a lot too!

Thank you!

Regarding the issue with code pasting: just copy your code. The "difficult" part is the formatting. I suggest to read other postings here, read e.g. this (although I just figured out that triple backticks do not preserve indentation)

Your solution could be as simple as this pseudocode:

while true:
    read input into variable
    if input == '42':
        break
    print variable
4 months later
19 days later

i submitted the following ans:
num = 42
running = True

while running:
x = int(input())
if x == 42:
break
else:
print()

but the SPOJ says its wrong.

6 months later

every time you loop the while loop num changes so the while loop wil not run forever because num changes