Life, universe and python

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 :\

1 Like

Hello and welcome to Codechef!!

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? :wink:

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!

1 Like

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 :slight_smile:

Loving the community so far…

1 Like

Hello again dculver1986,

It’s nice to be appreciated and trust me when I say that this is by far one of the best programming communities out there, if not the best of all!! :smiley:

Everyone is always willing to help and you can learn A LOT if you work on the problems yourself! Good luck,

Bruno

1 Like

Thanks Bruno,
Do you have any recommended reading for newbs like me to progress further ?

There are several good books :smiley:

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 :wink: And the famous: “Introduction to Algorithms” from CLRS…

But, most importantly, practice a lot :DD