How to fix the menu selection for quiting - python

So i want this to run when number 7 is entered it should quit the current program. I have tried with the rest of my menu selection but I just want to fix the quit at the moment.
if menu_selection == "7":
quit()

I am assuming you are guessing quit() is the way to exit.
You should try sys.exit():
import sys # at the head of your program
...
your program
...
if menu_selection == "7":
sys.exit()

If you want to exit your program you can use exit() from sys
import sys
[...]
if menu_selection == "7":
sys.exit()

Related

python not taking input after subprocess is called with subprocess.popen

Hello everyone im fairly new to python and this is my first language im learning so far. Im making a menu using a while true loop that takes input to select whichever flashing method a person needs to use and after calling whatever file using subprocess.popen or subprocess.call and it finishes it goes back into the menu loop but does not respond to any input at all.
import sys
import time
import subprocess
#!/bin/bash
sys.path.append
menu = True
def mainMenu():
while menu:
print("""
##################
# Utilities Menu #
##################
1. Tray Flasher
2. ESP Burner
3. Quit
""")
choice = (input('Select a utility: '))
if choice == '1':
trayFlasher()
elif choice == '2':
espBurner()
elif choice == '3':
print('Goodbye!')
def trayFlasher():
print('Have Fun!')
time.sleep(1)
subprocess.call(["<path to file>"])
def espBurner():
print('Burn Baby Burn...')
time.sleep(1)
subprocess.Popen(['<path to file>'])
sorry if my code is not so good or efficient, currently still learning. TIA!

is their a way to close the IDLE shell if they input something

is there a way on IDLE to close the users shell if they were to enter something that would be supposed to "quit"?
You can use exit(0) to terminate the program, as follows:
while True:
i = input()
if i == "quit":
exit(0)
else:
# do something
pass
For more information about what 0 for exit() means:
Difference between exit(0) and exit(1) in Python

python execute function when console is closed

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")

Repeating Menu but only repeats once

Whenever I select option 1 to play the game, the menu resets itself and starts from the beginning. It only does this once however and on the second time, choosing option 1 starts the game.
When I select any other option, it runs on the first attempt and does not repeat. Any help to stop this?
#Jake Eaton
#Menu
import time
from typing import type, type2
from main_game import Main_Game #Imports time, system and name Modules
from view_leaderboard import View_LB #Imports defined code with the names type, type 2, Main_Game and View_LB
from os import system, name
def clear():
if name =='nt': #Defines the clear command for the Terminal
_ = system('cls')
type("Welcome\n")
time.sleep(2)
type("Welcome to the only Text Adventure you will ever need to play in order to learn!\n")
time.sleep(2)
type("This Text Adventure covers the AQA A-level Computer Science Topic 4.6\n")
time.sleep(2)
type("Please remember that this is still in Beta and may never have a full 1.0 release\n")
time.sleep(2)
type("All releases and the source code can be found on Github using the link: https://github.com/Spud-Lord/Mock-NEA\n")
time.sleep(2)
type("To view the releases, just click on the releases tab on the Github Page\n")
time.sleep(2)
type("So let us begin!")
time.sleep(2)
type("Now then...")
time.sleep(2)
type("What do you want to do? Just type in the number of the option you want!")
time.sleep(2)
def Main_Menu(): #Defines all indented code as Main_Menu
menu = input("""
[1] - Start Game!
[2] - View Leaderboard
[3] - Exit Game
""") #The triple speech marks allow the Menu to go over multiple lines with only one code
if menu == "1":
clear() #Calls Clear Definition to clear Terminal Screen
Main_Game()
elif menu == "2":
print("")
View_LB() #Calls View_LB Definition to view leaderboard
time.sleep(2)
print("")
Main_Menu()
elif menu == "3":
exit() #Exits the game
else:
type("Please type in one of the numbers above...\n")
Main_Menu() #Loops back round if the user doesn't enter one of the numbers
Main_Menu() #Allows the Menu to loop
I did not reproduce your issue with the following code.
It seems you at least need to replace type(...) by print though!
The code needs to be cleaned up so that it becomes more readable. Put all your functions first then add the main part of the code (which starts at print("Welcome\n"))
#Jake Eaton
#Menu
import time
#from main_game import Main_Game #Imports time, system and name Modules
#from view_leaderboard import View_LB #Imports defined code with the names print, print 2, Main_Game and View_LB
from os import system, name
def clear():
if name =='nt': #Defines the clear command for the Terminal
_ = system('cls')
print("Welcome\n")
time.sleep(2)
print("Welcome to the only Text Adventure you will ever need to play in order to learn!\n")
time.sleep(2)
print("This Text Adventure covers the AQA A-level Computer Science Topic 4.6\n")
time.sleep(2)
print("Please remember that this is still in Beta and may never have a full 1.0 release\n")
time.sleep(2)
print("All releases and the source code can be found on Github using the link: https://github.com/Spud-Lord/Mock-NEA\n")
time.sleep(2)
print("To view the releases, just click on the releases tab on the Github Page\n")
time.sleep(2)
print("So let us begin!")
time.sleep(2)
print("Now then...")
time.sleep(2)
print("What do you want to do? Just print in the number of the option you want!")
time.sleep(2)
def Main_Game():
print ("Main game")
def View_LB():
print ("LB")
def Main_Menu(): #Defines all indented code as Main_Menu
menu = input("""
[1] - Start Game!
[2] - View Leaderboard
[3] - Exit Game
""") #The triple speech marks allow the Menu to go over multiple lines with only one code
if menu == "1":
clear() #Calls Clear Definition to clear Terminal Screen
Main_Game()
elif menu == "2":
print("")
View_LB() #Calls View_LB Definition to view leaderboard
time.sleep(2)
print("")
Main_Menu()
elif menu == "3":
exit() #Exits the game
else:
print("Please print in one of the numbers above...\n")
Main_Menu() #Loops back round if the user doesn't enter one of the numbers
Main_Menu()

Python | IF statment stop script after user input has been made

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")

Categories

Resources