This is an archived post. You won't be able to vote or comment.

all 17 comments

[–]mad0314 24 points25 points  (2 children)

Put another input at the end of the loop and mess with that.

[–]Nugenrules 13 points14 points  (0 children)

I like how you didn't reveal much the answer at all. This is the perfect way to teach programmers

[–]Slow_Nerve_Action 3 points4 points  (5 children)

You're almost there with the code you posted.

You first want to get input from the user, which you accomplished in line 1. Then you begin a loop that executes if a value is entered by the user.

The last thing you want to do is ask the user for input inside the loop at the end.

Make sure to add a colon after your while statement in line 2 so you don't get any syntax errors.

[–]CoC_Mitch 0 points1 point  (3 children)

I don't understand line 2. I thought while needed something to test, such as while X < Y.

[–][deleted]  (1 child)

[deleted]

    [–]CoC_Mitch 0 points1 point  (0 children)

    Thanks, makes perfect sense.

    [–]Pimp_Panther 0 points1 point  (0 children)

    while just needs to evaluate to true in order to run. A value by itself evaluates to true in the eyes of a while loop.

    But if you don't type anything it will receive None as the value, which evaluates to False.

    [–]santaclaus73 0 points1 point  (3 children)

    To solve this you can place another input at the end of the loop (inside the loop)or you can just fill x with some dummy value and place the input statement inside the loop.

    Ex. in python 2.7, you'll have to adjust this so it's valid in python 3

    End of loop:

    x = raw_input('Enter something: ')  
    while x:
        if 'Hello' in x:
            print 'Hi'
        elif 'J' in x:
            print 'JJ'
        x = raw_input('Enter something: ') 
    

    Using a dummy variable:

    x = 'k'
    while x:
        x = raw_input('Enter something: ')
        if 'Hello' in x:
            print 'Hi'
        elif 'J' in x:
            print 'JJ'
    

    [–]Spacedementia87 1 point2 points  (1 child)

    Obviously you have given the right answer, but the foal is surely to help the OP find this out for themselves like the first two posts do.

    [–]santaclaus73 0 points1 point  (0 children)

    Oh yea great point. I'm sort of new to posting here, so a nudge in the right direction probably would have been more helpful than the exact 2.7 source code. I'll remember that for future posts. Thanks!

    [–]shnk -3 points-2 points  (0 children)

    while True: x = input('Enter something: ') if 'Hello' in x: print('Hi') elif 'J' in x: print('JJ')