Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
command = ""
while True :
command = input('>').lower()
if command =="start":
print('Your Car have Started ')
elif command == "stop":
print('Your Car have Stopped')
elif command == "help":
print('''
Start- To Start the car.
Stop- To Stop the car.
Quit- To Exit.
''')
elif command == "quit":
break
else:
print("Hey I don't understand your command")
break needs to be inside your loop, as the error says. You might have accidentally wrote it outside. Move the part below while by pressing tab.
Please check out the python formatting guide if it is unclear.
command = ""
while True :
command = input('>').lower()
if command =="start":
print('Your Car have Started ')
elif command == "stop":
print('Your Car have Stopped')
elif command == "help":
print('''
Start- To Start the car.
Stop- To Stop the car.
Quit- To Exit.
''')
elif command == "quit":
break
else:
print("Hey I don't understand your command")
Related
I was wondering if someone here could help better explain For loops than my textbooks.
I'm writing a program in Python where the program askes a user if they're already an existing user. If they are an existing user, the program starts. If they're not an existing user, they're prompted to create a username and password.
I was wondering if someone could help explain how I could use a For Loop to loop the if statement incase a user enters some unexpected input like A or a number.
Thanks
Here's a code snippet of an if statement I have where it goes:
if newuser == "y":
print("Hello New User")
elif newuser == "n":
print("Hello Existing User")
else:
print("Please try again")
Try creating a while loop, and only break out of this if a condition is met.
while True:
newuser = input('enter y/n')
if newuser == "y":
print("Hello New User")
break
elif newuser == "n":
print("Hello Existing User")
break
else:
print("Please try again")
How can i execute function when user close the console window?
I want make something such this:
if close == True:
print("Really you want quit?")
option = input()
if option == "Y":
quit()
else:
pass
Here's how to add statements, and use a better quit() statement.
import sys # add this to your imports
anotherFile = "afterExit.py" #make sure this is in the same directory
def on_close():
print("Really you want quit?")
option = input()
if option == "Y":
os.system("python " + anotherFile) # on Linux/ Mac, use python3
sys.exit()
# i prefer sys.exit as it does not display the kill message box
else:
print("you chose not to quit")
I am working on a Python project and I have an area in the project where I need to have a loop timer as I need the code checked, say every 5 minutes, to see if the data in the database has changed and then alert the user. Now I can do most of it with if statements etc, but the timer part I have no idea on how to do it.
remind = (input('Would you like me to remind you of the status of your flight? yes/no: '))
if remind == 'yes':
# some how start a loop to check the database for update and give open to exit
print('Loop to check database')
elif remind == 'no':
print('Thank you for using FLIGHT CHECK')
quit()
So the basic logic of steps I would need to take are:
if remind = yes
check mysql table status
bunch of if statement
will also give option to stop alert
if remind = no
end
You can use time, and a while loop which watches a variable for true/false.
import time
run = True
while run:
remind = (input('Would you like me to remind you of the status of your flight? yes/no: '))
if remind.lower() == 'yes':
print('database loop')
# Sleep for 300 seconds
time.sleep(300)
elif remind.lower() == 'no':
run = False
This will loop, every 5 minutes until a user answers no at which point it sets run = False and ends the while loop.
Would you like me to remind you of the status of your flight? yes/no: yes
database loop
Would you like me to remind you of the status of your flight? yes/no: no
>>>
I am currently working on a automated troubleshooter using Python. Below is a piece of my script which I need help with.
s1 = input("Is your phone freezing/stuttering? ")
if s1 == "Yes" or s1 == "yes":
print("Try deleting some apps and this might help with your problem.")
if s1 == "No" or s1 == "no":
def foo():
while True:
return False
So what I want to happen is my script to stop when the user types in YES and when the solution to the fix comes up. Is there a possible loop for that or something similar? Also if the user types in NO then I want the script to continue to the next question.
So one thing that you can do is utilize the sys.
So you can modify your program to look like the following:
import sys
s1 = input("Is your phone freezing or stuttering (yes/no)? ")
if s1.lower() == "yes":
print("Deleting some apps and this might help!")
elif s1.lower() == "no":
print("Your phone is working fine! Program is terminating.")
sys.exit(0) # this exits your program with exit code 0
The sys package is great for program control and also interacting with the interpreter. Please read more about it here.
If you don't want your program to exit and you just want to check that the user entered no, you could do something like:
import sys
s1 = input("Is your phone freezing or stuttering (yes/no)? ")
if s1.lower() == "yes":
print("Deleting some apps and this might help!")
elif s1.lower() == "no":
pass
else:
# if the user printed anything else besides yes or no
print("Your phone is working fine! Program is terminating.")
sys.exit(0) # this exits your program with exit code 0
Let me know if I can help in any other way!
EDIT
A comment by crickt_007 suggested that it might be helpful to repeat the input and continually query the user. You could wrap this whole function in a while loop then.
import sys
while True:
s1 = input("Is your phone freezing or stuttering (yes/no)? ")
if s1.lower() == "yes":
print("Deleting some apps and this might help!")
# solve their issue
elif s1.lower() == "no":
# supposedly move on to the rest of the problem
pass
else:
# if the user printed anything else besides yes or no
# maybe we want to just boot out of the program
print("An answer that is not yes or no has been specified. Program is terminating.")
sys.exit(0) # this exits your program with exit code 0
A while loop sounds like what you are looking for?
import sys
s1 = "no"
while s1.lower() != 'yes':
input("Is your phone freezing/stuttering? ")
if s1.lower() == 'yes':
print("Try deleting some apps and this might help with your problem.")
sys.exit(0)
elif s1.lower() == 'no':
print("something")
else:
print("invalid input")
I have recently been doing a gamedev class in my high school and we have been using Mac's terminal to run python coding to make a text based game. I think it's a version of python 2.7 and I have encountered an error
while Menuselect != "play":
MenuSelect = raw_input("Type 'quit' to exit - 'credits' for credits - Type 'play' to start\n")
MenuSelect = MenuSelect.lower()
if MenuSelect == "quit":
SystemExit(0)
elif MenuSelect == "credits":
print("Lmao, only Matt made this")
else:
print("You mistyped. 10/10")
print(": You wake up in a dark room, you don't know where you are or how you got here :")
When I try and run it and enter 'play' to stop the loop, it just continues the loop again. Anything wrong with it?
You are checking the variable name "Menuselect" with lowercase "s", but assigning the raw_input to a variable called "MenuSelect" with capital "S". Change the 'while' statement to
while MenuSelect != "play":
and it should work.