My input commands are not activating inside of if elif and else statements

I intend for the input commands to activate once the if, elif, or else statements are true but the code just ends without asking for the input. Here is my code.

def main():
	a = input("Hello brave adventurer! Are you ready to starts your quest? (y/n)")
	pw = 1
	while(pw == 1):
		if(a == "y" or pw < 2):
			pw = 2
		elif(a == "n"):
			input("Well thats unfortunate. try again!")
			pw = 1
			input("okay bye")
		else:		
			input("That isn't an option.")
			pw=1

Hi,

welcome to the forum!

Can you please copy and paste your code between two lines each having three wavy apostrophes in series. This key is located just underneath the esc key. You will have to press the Shift key in tandem.

paste_code_here

This will cause your code to appear as it would appear in an IDE editor. Also please double check
your code so that it has all of the correct indentations where appropriate. This way community members can interpret your code as intended and help you more easily.

yes, sorry!

def main():
a = input(“Hello brave adventurer! Are you ready to starts your quest? (y/n)”)
pw = 1
while(pw == 1):
if(a == “y” or pw < 2):
pw = 2
elif(a == “n”):
input(“Well thats unfortunate. try again!”)
pw = 1
input(“okay bye”)
else:
input(“That isn’t an option.”)
pw=1

Im not sure why the indentation did not appear, but i can say that im def using the right indentations.

Your code is missing indentations as none are present.

For example:

def something(x, y):
return x * y

if x > 10:
print(‘x’)

Should look like this:

def something(x, y):
    return x * y

if x > 10:
    print('x')

Notice the indentation? It is best if you copy it from your IDE editor. If you use IDLE, it should generally be set up such that indentations are set to four spaces. This makes the code much more readable.

I use a type of IDLE, but its given to me by my school! In the actual IDLE the indentations are correct, but the inputs after the first one just dont pop up!

okay, and after I have that and the indentations are fixed, how come the inputs still dont work?

Can’t speak for others, but I’m not even going to try reading such unformatted/unindented code. You’d better fix that.

okay lol, one sec sergeant

def main():
     a = input(“Hello brave adventurer! Are you ready to starts your quest? (y/n)”)
     pw = 1
     while(pw == 1):
          if(a == “y” or pw < 2):
               pw = 2
          elif(a == “n”):
               input(“Well thats unfortunate. try again!”)
               pw = 1
               input(“okay bye”)
          else:
               input(“That isn’t an option.”)
               pw=1
main()

That code doesn’t run, has syntax errors.

it runs alright in my schools IDLE thing

That isn’t true

it just doesnt work properly, but it fully runs with no errors

Your original code had plain quotes (good), but this code has ‘smart’ quotes (bad).

Also, your original code indented using tabs, whereas the recommendation is 4 spaces.

Now to work through the code.

Suppose you answer “y”.

    pw = 1

OK so far.

    while(pw == 1):

The condition is true, so you enter the loop.

        if(a == "y" or pw < 2):

The condition is true.

            pw = 2

You reach the end of the loop, so you go back to its start.

    while(pw == 1):

The condition is false, so you skip over the loop and arrive at the end of the function.

My problem is that when you type n as your option, the inputs do not come up. it just ends, even though pw is still equal to 1!

and I use tab to indent simply because thats how i was instructed to in my online schools programming foundations course.

Is this what you meant:

def main():
     
     while(True):

          a = input('\nHello brave adventurer! \nAre you ready to starts your quest? (y/n)')
         
          if(a == 'y'):
               print('Great!  Glad that you have decided to begin your adventures with us.')
               print('Buckle up!')
               break
               
          elif(a == 'n'):
              
               print('Well, thats unfortunate. Try again!')
               print('Okay, bye.')
               break
               
          else:              
               print('\nThat isn’t an option.  Please enter a valid entry.')
main()


A couple of mistakes in your code:

  1. You were using input when you should have been using print.
  2. You were using the wrong quotes.
  3. You had the question outside of the while loop. It should be inside so that it queries you
    again in case the user enters an invalid entry.
  4. No need to use a variable pw when you can just set the while loop to True and use the break
    keyword to exit when a condition is satisfied or met.

Traditionally outside of my schools idle, thats what I would like, BUT, inside of this idle, the print commands dont appear until everything else, including all the inputs, have run. All meaning that it wouldnt work with my schools dumb IDLE.

Well, if the course said to use tabs, then use tabs on the course, but pretty much everyone else who uses Python follows the recommendation of 4 spaces.

Anyway, suppose you answer “n”.

     pw = 1
     while(pw == 1):

The condition is true.

          if(a == "y" or pw < 2):

The condition is true because pw is 1, and 1 is less than 2.

               pw = 2

You reach the end of the loop, so you go back to its start.

    while(pw == 1):

The condition is false, so you skip over the loop and arrive at the end of the function.

1 Like