Click here to Skip to main content
15,881,687 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am using python 3

Python
import sys
while True:
   answer = input('Do you want to continue?:')
   if answer.lower().startswith("y"):
      print("ok, carry on then")
   elif answer.lower().startswith("n"):
       sys.exit()
x = str(input("Enter A or B for a Trick Or a Treat!: "))
if x == 'a':
    print("treat")
else:
    print("trick")


It out puts "do you wish to continue" upon pressing yes it keeps repeating instead of asking press a or b for a trick or a treat

What I have tried:

I tried breaks and randoms but they both dont work
Posted
Updated 26-Oct-21 21:42pm

Python is indentation-sensitive, so from the line x = str(... this part does not belong to the while loop.
Try
Python
import sys
while True:
   answer = input('Do you want to continue?:')
   if answer.lower().startswith("y"):
      print("ok, carry on then")
   elif answer.lower().startswith("n"):
      sys.exit() #warning: indentation here was not correct (4 spaces instead of 3)
   x = str(input("Enter A or B for a Trick Or a Treat!: "))
   if x == 'a':
      print("treat") #idem here
   else:
      print("trick") #idem here

Python Indentation[^]
 
Share this answer
 
v2
Comments
Joseph Diblasi 26-Oct-21 17:06pm    
So now after that it keeps repeating it ie Do you want to continue?:y
ok, carry on then
Enter A or B for a Trick Or a Treat!: a
treat
Do you want to continue?:y
ok, carry on then
Enter A or B for a Trick Or a Treat!: a
treat
Do you want to continue?:y
ok, carry on then

my whole goal is to make it so that it asks if you want to continue once and so that if you press a it can be trick OR treat so its random!
Not real uip on Python but try this

Python
import sys
while True:
   answer = input('Do you want to continue?:')
   if answer.lower().startswith("y"):
      print("ok, carry on then")
      x = str(input("Enter A or B for a Trick Or a Treat!: "))
      if x == 'a':
          print("treat")
      else:
          print("trick"
   elif answer.lower().startswith("n"):
       sys.exit()
)
 
Share this answer
 
Comments
Joseph Diblasi 26-Oct-21 17:15pm    
File "main.py", line 11
elif answer.lower().startswith("n")
^
IndentationError: unindent does not match any outer indentation level
Joseph Diblasi 26-Oct-21 17:16pm    
I wanna make it so it changes trick or treat on a so it can be trick once treat next and it asks if u wanna continue
Mike Hankey 26-Oct-21 17:29pm    
Set a flag and check, if set answers trick or if false answers treat and updarte flag.
Joseph Diblasi 26-Oct-21 17:50pm    
?
Try this slightly simpler option, which does not need sys.exit():
Python
while True:
    x = str(input("Enter A or B for a Trick Or a Treat!: "))
    if x.lower().startswith("a"):
        print("treat")
    else:
        print("trick")
    answer = input('Do you want to continue?:')
    if answer.lower().startswith("y"):
        print("ok, carry on then")
    else:
        break # terminate the while loop
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900