Does python takes enter as an input? - python

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.

Related

Asking for user input in print function - Python 2.7

Sorry if this seems like a bit of a dumb question but I’m a bit confused about a Python 2 exercise I am trying to complete.
The exercise asks to write a code that will find the factorial of a number.
I have written a function that does this but I am confused about the pre set print function that has asks for user input as I’ve only seen this done as a separate thing. Could anyone help me figure out how to make this work please as the exercise asks for the print function to not be deleted.
Thank you in advance!
def FirstFactorial(num):
# code goes here
fact = 1
while num > 0:
fact *= num
num -= 1
return fact
x = raw_input("enter a number: ")
result = FirstFactorial(x)
# keep this function call here
print FirstFactorial(raw_input())
You have to print result not print FirstFactorial(raw_input())
The way it is set up now the program will work for the first input, but it will never print it out so you never see result.
Then it will look for a second input from your second call to raw_input(), but the user will not be notified that the program wants a second input. The second output of FirstFactorial() will be correctly printed.

Pyautogui will work until it has to input something into the interpreter, which at that point will stop until I click enter manually

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

How do you input something to python

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.

How to have an updating prompt while waiting for input in python?

So the question is how to have an input function or stdin.readline function waiting for an input, while having an updating prompt i.e. The prompt contains the time in format HH:MM:SS and is refreshing every second like:
while 1:
sys.stdout.write('\r' + time.strftime(TIME_FORMAT) + ' :>')
time.sleep(1.0)
But as soon as you add an input there, of course the program waits until you write some input. The version of python I am using is 3.5.
I know I should use curses, but I am planing to write a cross-platform program and the only module I have found is clint, but it didn't have anything in the documentation on the updating prompt.
I have found something that got pretty close but has different problems:
def input_thread(L):
x = input()
L.append(x)
L = []
thread = threading.Thread(target=input_thread, args=(L,))
thread.start()
while 1:
sys.stdout.write('\r' + time.strftime(TIME_FORMAT) + '>:')
time.sleep(1.0)
sys.stdout.flush()
Now the problem remains is that when you type the input but do not press ENTER, the input on the next iteration remains but when you write something, the previous input gets replaced by the current one. Of course the previous inputs are still there in the argument L, so there is no lost input. I hope I didn't describe this to confusing.
If there is no easy way of doing this as it could be done with curses, I'm open to similar cross open tools. Thanks for your time and answers!
So, I figured out what I wanted, a few months ago but didn't answered my own quesiton.
To have a prompt with updating time, you need a separate threads that:
Updates the prompt with additional input
Catches your key strokes
The second thread handles every key-stroke and you can program it how to handle the pressed keys.
When pressing a key, the thread that updates the prompt adds the pressed keys. Also you have to configure some shortcuts for Return, Backspace, etc... to work as expected
I've got it to work and unfortunately... I don't like it. If anyone would like to see the code I will happily provide.

Running a function in Python every time any key is pressed?

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

Categories

Resources