I am currently working on a game and I was wondering if there was any way to execute commands like a text file by user input?
I would like to make it where the text doesn't pop up all at once, but where you could do something like "Press any key to continue" and when they do that, the next wall of text appears. Any help is appreciated. Thanks.
The easy way is:
input('Press Enter to continue')
(in Python 3; raw_input instead in Python 2) but that will indeed wait for an Enter, AKA return, before continuing.
If you're really adamant about the any-key part you'll have to get "down and dirty". In Microsoft Windows only,
import msvcrt
def wait():
msvcrt.getch()
print('Press any key to continue')
wait()
will work -- but it will fail on Linux or MacOSX; you'll need other approaches for those. So please let us know which platform(s) you need to support and we'll figure something out!-)
Try putting input("Press Enter to continue") between printing each wall of text.
Related
I am currently trying to create a program that lets me enter a name, and then by using pyautogui, searches up that name on my computer's message application, then asks the person who's name I typed in if they want to do the pythagorean theorem. The program copies their messages, and it is supposed to have a back and forth with the person who is being messaged and the interpreter directly. The problem I am having is that when python copies the message of my friend, it goes to paste it into an input setup in the interpreter, but will just freeze. It won't continue running until I click enter manually to bypass the input that I set up, which at that point will paste the message into the interpreter, even though the input has already been used. I tried to insert a filler input before the first input, which asks the user if they want to solve for a leg length or hypotenuse. I'm sorry if the explanation is unclear, I hope it is understood better with code. Even with a filler it doesn't work. By the way, (373, 823) is a point within the interpreter just so pyautogui clicks on the interpreter to use it.
pyautogui.click(373, 823)
fillerinput = input("filler: ")
pyautogui.press("a")
pyautogui.press("enter")
userchoice = input("leg or hyp: ")
time.sleep(2)
pyautogui.click(373, 823)
pyautogui.keyDown("command")
pyautogui.press("v")
pyautogui.keyUp("command")
pyautogui.press("enter")
Say you want to ask the user something from the terminal at the end of your program.
However, during the program run, the user pressed the enter key.
import sys
import time
print("Hit enter now to see this 'problem'")
time.sleep(1)
# Hit enter now while the program sleeps!
a=input("Do you want to delete something that is really bad to delete? [Y|n]")
if a.lower()!="n":
print("\nNO! YOU DELETED IT!")
Of course, it's stupid to delete stuff with default response, and I don't do that. However, It's annoying that I, the user, sometimes hit enter and the default is what goes.
I'm actually using click to read the input. So I'd want a preventative command before click executes;
import sys
import click
import time
print("Hit enter now to see this 'problem'")
time.sleep(1)
# Hit enter now while the program sleeps!
# Clear stdin here somehow.
sys.stdin.flush() # <- doesn't work though
a=input("Do you want to delete something that is really bad to delete? [Y|n]")
if a.lower()!="n":
print("\nNO! YOU DELETED IT!")
I'm on Linux (Ubuntu 16.04 and Mac OS).
Any ideas?
Turns out I needed termios.tcflush() and termios.TCIFLUSH which does exactly what is asked:
import sys
from termios import tcflush, TCIFLUSH
import click
import time
print("Hit enter now to see this 'problem'")
time.sleep(1)# Hit enter while it sleeps!
tcflush(sys.stdin, TCIFLUSH)
# Discards queued data on file descriptor 'stdin'.
# TCIFLUSH specifies that it's only the input queue.
# Use TCIOFLUSH if you also want to discard output queue.
a=input("Do you want to delete something that is really bad to delete? [Y|n]")
if a.lower()!="n":
print("\nNO! YOU DELETED IT!")
else:
print("Phew. It's not deleted!")
I'm new to programming. I''ve successfully wrote everybody's first program , however , it executes right before closing itself. How can I add a pause to the program?
Modify the code to be like this, you will learn about the below function soon enough:
print('Hello world!')
input()
Now at the end of the program it won't exit until you press Return.
Or better still, run the code in a shell like IDLE so you don't have to worry about all that.
You need to put a call to input at the bottom of your script:
print("Hello World!")
input("Press 'Enter' to exit.") # Use raw_input in Python 2.x
Doing so will keep the output window open until you press Enter.
If you literally want to add a fixed pause, use the sleep function in the time module. The is discussed in detail in the answer to How can I make a time delay in Python? . Your program then becomes:
import time
print("Hello world")
time.sleep(1)
The first line tells python that time means the module time, while the last line says to wait for 1 second. (If you wanted to wait for 2.5 seconds, then change the last line to time.sleep(2.5), etc...)
In C++, i could use cin.ignore() to get a keystroke from user before program continues.
What is the equivalence of cin.ignore() in python?
You could simply do:
raw_input("Press enter to exit...") # use input() if using Python 3.x
This only works with pressing Enter, however (the user could type in a bunch of letters but only pressing Enter at the end).
First off, forgive me, because I'm incredibly new at this Python thing (I'm really an HTML/CSS kind of guy, but I'm trying to branch out). This is probably an elementary sort of question, but we all have to start somewhere, right?
I'm putting together a very basic Python program that will select a random letter from a string of letters and print it out every time someone hits the any key. The whole thing is pretty simple, and currently returns a random letter, but doesn't wait for a keypress to do so, and stops after completing the function runs once.
import random
letterlist = 'abcd'
def random_letter(letters):
print ('Press enter for a random letter...')
print random.choice(letters)
random_letter(letterlist)
Output should look like this:
Press enter for a random letter.
'b'
Press enter for a random letter.
(and so on...)
It's clear that whatever I need to do ought to fall inside of random_letter someplace. I've been googling around and have found lots of references to raw_input and mvscrt, but I'm not entirely sure what I need. It's entirely possible that I'm just asking the question wrong.
I'm assuming assuming that I need some sort of loop going on to keep this running indefinitely.
Thanks in advance!
For starters, you need a loop somewhere to continue prompting the user. You also need some sort of exit condition for the loop. This loop can be inside the function like so:
def random_letter(letters):
while True:
x = raw_input('Press enter for a random letter...')
if x == 'done':
break
print random.choice(letters)
random_letter('abcdef')
Notice that inside the loop we use raw_input to prompt the user to type something, anything, then press enter. When the user types done and hits enter, we break out of the loop using break.
An alternative would be to wrap your existing function in a loop and take care of the prompting outside the function.
def random_letter(letters):
print random.choice(letters)
while True:
x = raw_input('Press enter for a random letter...')
if x == 'done':
break
random_letter('abcdef')
If you don't have to use a loop, and thinking of capturing keyboard events, then there are no cross-platform ways of doing it: Is there a cross-platform python low-level API to capture or generate keyboard events?
For Windows, there is pyHook: http://pyhook.wiki.sourceforge.net/
You can look into the code of pyKeyLogger: http://pykeylogger.wiki.sourceforge.net/
Or a dirty way, capture interrupts: Capture keyboardinterrupt in Python without try-except