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).
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")
sorry for such a noob question but how exactly do you input something to python. I am not talking about the function but rather what is the physical button on the keyboard do you press to finish your input? For example, the code (random found):
value = input("Please enter a string:\n")
print(f'You entered {value} and its type is {type(value)}'
After you type in the string, what button do you press? I am using the OSX IDLE key-binding and the Return key just end the program.
The code you have will print : Please enter a string:.
Then you just type in whatever, 'hello' for example and hit enter.
And it will output: You entered hello and its type is <class 'str'>
Then, like #Silvio said, it will immediately close, but that is easily fixed with another input() statement at the end of you code.
I executed a program to input n space-separated integers as input, create a tuple, and the use hash() on them.
print(hash(tuple(map(int, input().split(" ")))))
I am using Pycharm IDLE.
While execution after entering the numbers and pressing I get the answer in one line in the following code:
print(input()==0 or hash(tuple(map(int,input().split()))))
But After I press enter in the following code, the pointer moves to the new line and then I have to press enter again.
My question is :
1) Why do I have to press enter twice in the second one.
2) Does Python takes enter as an input or as a null input.
A dry run would be clarify a lot of my doubts.
As others have already said, the reason you're having to press enter twice when running the code
print(input()==0 or hash(tuple(map(int,input().split()))))
is simply because you're calling the input function twice. This behavior can be reproduced with a simple example
>>> input(), input()
pressed enter once
pressed enter twice
('pressed enter once', 'pressed enter twice')
>>>
Since you only want to ask the user for input once, you need to save the result of the first call to input in a variable:
>>> var = input(); print(var == 0 or hash(tuple(map(int, var.split()))))
3527539
>>>
Also, as others have already said, I'm not really sure why you tried to write your code all on one line. Yes writing concise code is not a bad goal, so long as that code remains readable. Often times it's simply better (and in your case, necessary) to divide you code among multiple lines.
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.
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