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
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.
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.
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!
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:
You were using input when you should have been using print.
You were using the wrong quotes.
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.
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.