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!
Related
So I got an idea to build something like mp3 player. To build that, I used multiprocessing module provided in Python. Here is my code
import multiprocessing
from playsound import playsound
def cp():
playsound('Music.mp3') # play the music
x = multiprocessing.Process(target = cp, daemon = True)
def main():
x.start()
while True and x.is_alive:
u = input("Input: ")
print(u)
if u == "S":
x.terminate()
print("Terminated process")
break
elif u == 'P':
# questioned code
print("Process paused")
elif u == 'R':
# questioned code
print("Process resumed")
if __name__ == '__main__':
main()
The idea is, when the program is executed, the program will automatically play the Music.mp3 file. To control the program, the user must input specific keyword into it.
S to stop the music and exit the program
P to pause the music
R to resuming playing the music
For now, I only know how to code the S option. For the others, I don't have any idea how to code it. So my question is: Is there any idea how to complete the P and R options? Or maybe, is there any idea about how to build the program using another method besides using multiprocessing module?
Thanks for the help
so, i was trying to make a personal assistant and i wanted to make a reminder type of commnd.
here is the code of the reminder(notification).
and the speak and takecommand fuctions are custom.
elif "remind" in query:
speak("what should i remind you")
messageT = takeCommand()
speak("ok, so when should i remind you.")
timeOfReminder = takeCommand()
while True:
current_time = tm.strftime("%H:%M:%S")
if current_time == timeOfReminder:
print(current_time)
break;
toaster = ToastNotifier()
toaster.show_toast(messageT,
duration=10)
so the problem is not with this code, but the code contains a while loop which prohibits me to proceed with my code(this whole code is also a part of a while loop which contains all the command).
i wanna know how to like run this loop in parellel of the other main while loop(this while loop checks the time while the main while loop checks what command the user is giving.)
is there any way to do this (if you want i can post my full code if it helps).
Use the threading library in Python 3.
import threading
Create a function:
def my_long_function():
while 1:
print("runs in background")
time.sleep(1)
long_thread = threading.Thread(target=my_long_function)
long_thread.start()
while 1:
print("Running in foreground")
time.sleep(3)
Hope this works for you!
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()
I'm looking to write a program that asks for user_input on a cyclical basis, but does not halt further procedures while waiting for the user to give input.
Is there a command that acts as a press of the RETURN key? Does this have to do with writing to stdin? An alternative might be to see if there is no user_input that currently exists.
Imagine operating the code below, where the program is constantly running, even if the user provides no input,
import os
import time
import termios
import sys
x = 0
while x < 60:
time.sleep(0.5)
x += 1
print x
user_input = raw_input(" ")
if user_input == "exit":
os._exit(1)
I have a little Python program that uses keyboard input to run certain commands.
I setup everything in one main program loop but now I'm wondering, do I even need a main loop?
Here is what I mean:
mainProgramLoop = 1
while mainProgramLoop == 1:
print ("\nType in the command. ")
keyBoardInput= input("command:")
if keyBoardInput == "a":
#do this
elif keyBoardInput == "b":
#do this
Do I actually need that while loop?
Thanks!
No, you do not need a main loop if you use the cmd.Cmd class included with Python:
#! /usr/bin/env python3
import cmd
class App(cmd.Cmd):
prompt = 'Type in a command: '
def do_a(self, arg):
print('I am doing whatever action "a" is.')
def do_b(self, arg):
print('I am doing whatever action "b" is.')
if __name__ == '__main__':
App().cmdloop()
The documentation for the cmd module includes an example near the bottom to help get you started.