ent = input("Press the Enter key to spin the roulette wheel.")
if ent == "":
print("Roulette wheel spinning...")
print("Roulette wheel spinning...")
else:
#I want it to do nothing till only enter is hit What can I write here?
Is there way so when a key other than enter alone is pressed that it wont do anything until only enter is pressed? Using input allows the users to write something and therefore when they hit enter it will always run through. I'd prefer it if I can disable the writing and only respond when the enter key is hit.
You could put a while loop around the input so until the input equals enter the user can't continue. For example:
while True:
ent = input("Press the Enter key to spin the roulette wheel.")
if ent == "":
break
print("Roulette wheel spinning...")
print("Roulette wheel spinning...")
Hope this could help.
Related
That's my code:
while True:
prompt = "Enter code: "
code = input(prompt)
if code == "123":
open_door()
The program automatically opens a door when the user types "123" on a USB keypad that does not have the "Enter" key.
Since my keypad does not have the "Enter" key, I want to force an "Enter" after 5 seconds. 5 seconds after the beginning of the loop, the program hits "Enter" no matter what has been typed. If by any chance code get to hold successfully "123" when "Enter" is hit, the door is open; otherwise, there'll be a second chance.
How can I do that?
Addt’l info: this program will run in a Raspberry Pi 3, but I am using Mac for the tests.
Please note that "buying a keypad that has an "Enter" key" is not possible, because it's not actually a keypad; it's a RFID reader that works exactly like a keypad. I preferred to use "keypad" for understandability.
This worked fine for me:
import pyautogui
import threading
def break_input():
time.sleep(5)
pyautogui.press('enter')
while True:
threading.Thread(target=break_input).start()
prompt = "Enter code: "
code = input(prompt)
if code == '123':
open_door()
You will need the third-party library pyautogui.
guess = raw_input(): if I press enter button here what will be the value inside the guess variable and is it printable?
Executable code in shell IDE:
>>> guess = raw_input()
>>> print guess
If you press enter then guess=''.
You can print guess which will turn out to be blank.
I have tried to find what I am seeking before posting this. But I have a hard time formulating the question and finding an answer.
I wonder if there is any way of having a key, such as "b", that takes the user back to the main menu where ever he is while running my program.
I have a menu and sub-menus and I want the user to be able to go back to the menu wherever he is by just pressing "b". I wonder if there is any easy way of doing this instead of putting
if choice= b:
menu()
whenever I have an input()...
I hope this is not too confusing! Would really appreciate a answer!
Not recommended for making your code easy to read but...
You could make your 'b' input check a function, then run that function at the start of each input check
def b_check(option):
if option == 'b':
main_menu()
else:
return option
def main_menu():
#your main menu function goes here
#your active menu code goes here
#ask the user to make their selection
#Note: for Python 2.x use `raw_input`
option = input('Enter your choice')
b_check(option)
if option == 'a':
#do this
elif option =='c':
#do that
So I have a Python assignment where I need to use a dictionary to hold the items of a menu, then randomly re-key and re-order the menu so that I can change the order of the menu options printed out. Here is an example of the code I have:
ChoiceOne = "Rekey Menu"
ChoiceTwo = "Quit"
menu = {'1':ChoiceOne, '2':ChoiceTwo,}
userChoice = input("Please Enter the Integer for Your Menu Choice: ")
while userChoice not in menu.keys():
print("Invalid Input")
userChoice = input("Please Enter the Integer for Your Menu Choice: ")
if menu[userChoice] == ChoiceOne:
menu = rekey(menu)
elif menu[userChoice] == ChoiceTwo:
quit()
The above code loops while the user chooses not to quit, repeatedly printing out the menu over and over again. The following is my rekey() function
def rekey(menu):
rekey = {}
keylist = random.sample(range(10), 2)
for i, item in zip(keylist, menu ):
rekey[i] = menu[item]
return rekey
My problem seems to present itself in the line where I check whether the user input for the menu option is valid or not. Initially, entering anything other than "1" or "2" will cause the program to enter the While loop until a valid input is entered. After rekeying the menu however, the line "while userChoice not in menu.keys()" is always triggered and no input is ever matched to continue the program.
I've tried to find the problem by printing out the new keys in the dictionary and checking to see if the userChoice matches any one of them, but even when I choose a valid new key, the program seems to think that nothing I enter is a valid key.
I hope I've described the problem well enough to be understood, thanks for the help in advance!!
In your first case the menu keys are strings, after the rekey they are ints. You need to do a conversion of either the keys or the user input so they match.
According to the documentation the input(x) function is equivalent to eval(raw_input(x)). This means that typing "1" at the input prompt is equivalent to typing "1" in the python interpreter, so you get the integer 1.
As SpliFF has already pointed out, you need to ensure that you're either using integer keys in your menu dictionary, or you could use string keys for the dictionary and switch to using the raw_input function to read the user's choice.
I am having trouble running this simple statement in the IDLE prompt.
if True:
print("True") # need to press ENTER twice?
else:
print("False") # need to press ENTER twice?
Any help is appreciated.
IDLE is automatically indenting when you press return. Press delete to remove the automatic indentation.
if True:
print("True") # press return, then backspace
else:
print("False")