[Newbie] My code stop working without errors (Resolved)

Hi I’m a newbie, I am trying my best to have fun while learning and I’am making a choice based game. It’s very basic but at some point, my code stop working and just close normally.

Here (I removed all texts):

choice1 = input(' ')
if choice1.lower() == ' ':
    p1_choice2 = input(' ')
    
    if p1_choice2.lower() == '':
        p1_choice3 = input(' ')
    elif p1_choice2.lower() == ' ':
        p1_choice4 = input(' ')

        if p1_choice4.lower == 'it bug here':
            print(' ')
            print(' ')
        elif p1_choice4.lower == ' and here ':
                p1_choice6 = input(' ')

It’s not finished so many choices aren’t coded but after the “p1_choice4 = input(’ ')”, the if and elif don’t print. In the terminal, I input the answer and then program just close. It don’t print the text (if) or ask the user to input the variable (elif).

Also can you tell me if there is a better way to code this, because if I keep “TAB” then write if’s and elif’s, it will be unreadable with all the space and I will have a hard time to make specific events.

I don’t know if you can understand my message. I need help so bad…

As long as this is the actual code you’re testing, that’s ok. Sometimes
the cause of a problem is in the code you’ve omitted.

Your code starts like this:

 choice1 = input(' ')
 if choice1.lower() == ' ':
     p1_choice2 = input(' ')
     ... etc etc ...

All the following code is indented inside the if choice1.lower() == ' ' if-statement.
So unless choice1 actually _is_ ’ '` then nothing underneath will be executed.

BTW, there’s no need to use .lower() if you’re testing against a string with no letters in it. You could just say:

 if choice1 == ' ':

Also, most people don’t enter a blank line with a single space. An empty line, sure.

I would put some print() calls in your code, eg:

 choice1 = input(' ')
 print("choice1 =", repr(choice1))
 if choice1.lower() == ' ':

That will show you exactly what’s in the variables, in case it looks like the tests are not coming out as you expected.

Hi,

they do print. However, it just so happens that the print statements are blank. Print non-blank messages and not empty strings.

choice1 = input(' ')

if choice1.lower() == ' ':
    print('first if')
    p1_choice2 = input(' ')
    
    if p1_choice2.lower() == '':
        print('2nd if')
        p1_choice3 = input(' ')
        

    elif p1_choice2.lower() == ' ':
        print('first elif')
        p1_choice4 = input(' ')
        
        if p1_choice4.lower == 'it bug here':
            print('3rd if')
            print(' ')
            print(' ')
            
        elif p1_choice4.lower == ' and here ':
            print('2nd elif')
            p1_choice6 = input(' ')
            

print('End of the 1st if statement.')

Also, some of the conditions that you have created require you to either press Enter immediately or enter a single space.

It has:

if p1_choice4.lower == 'it bug here':

and:

elif p1_choice4.lower == ' and here ':

instead of:

if p1_choice4.lower() == 'it bug here':

and:

elif p1_choice4.lower() == ' and here ':

Is that the cause?

3 Likes

Yup, just caught that bug too. Otherwise, it was not the entered string but the str object

<built-in method lower of str object at 0x000001EE5D9CBE70>

Updated snippet with comments.

choice1 = input(' ')  # Press `Space' then 'Enter'

if choice1.lower() == ' ':
    print('first if')
    p1_choice2 = input(' ') # Press 'Enter' for 'if' statement
                            # Press 'Space' + 'Enter' for 'elif' statement

    if p1_choice2.lower() == '': # Press 'Enter' immediately - exits
        print('2nd if')          # Press 'Space' + 'Enter', go to 'elif'
        p1_choice3 = input(' ')


    elif p1_choice2.lower() == ' ':
        print('first elif')
        p1_choice4 = input(' ')
        print('p1_choice4 = ', p1_choice4)
        print('p1_choice4.lower = ', p1_choice4.lower)

        if p1_choice4.lower() == 'it bug here':
            print('3rd if')
            print(' ')
            print(' ')

        elif p1_choice4.lower() == ' and here ':
            print('2nd elif')
            p1_choice6 = input(' ')


print('\nEnd of the 1st if statement.')

No way bro you saved me. I feel so dumb, It happen to me so many times. Thank you for being patient with me!

Like I said I deleted all my text inside the strings because I would make the code difficult to read.