while 1 :
a = int(raw_input())
if a == 42:
break;
print a
created
last reply
- 22
replies
- 5.8k
views
- 19
users
- 3
links
while 1 :
a = int(raw_input())
if a == 42:
break;
print a
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?
What a shame , below program is wrong. The machine can not recognize py file?
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()
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.
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