Hello everyone-
I’m new here and to programming in general. Right now my preferred language is python, I’m almost done with the ‘Learn python the hard way’ series. Trying these problems has been a very humbling experience!!
I have been trying a LOT on the ‘life, universe and everything’ problem and failing bad. Here is my latest submission:
n = raw_input("")
while n:
if int(raw_input()) != 42:
print n
continue
else:
break
any tips would be great. I feel pretty bad about my knowledge so far right now :\
It seems that you are not reading the input properly…
What is n = raw_input("") doing there?
Your idea is good, you read a number, test it to see if it equals 42 and keep reading from the input until you reach a 42, when you break…
But, why complicate what can be so simple?
Using your code, I just changed some small things and got accepted:
n = int(raw_input())
while n != 42:
print n
n = int(raw_input())
I just implement your approach in a more direct manner, completely avoiding superfulous variables and simply reading a 1st number to “trigger” the while loop and do the checking afterwards!
Oh!!!
I didn’t think to put the != 42 within the line! I thought I had to use if/else within the while loop for it to work! Thanks! You’re my hero now. Thumbs up to you
I personally love the free e-book: “How to Think like a computer scientist: Learning with C++” from Allen B. Downey, for a deep understanding on how to think like a true cs And the famous: “Introduction to Algorithms” from CLRS…