How do you input something to python - 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.

Related

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

All lines of code not executing in python

why is only part of my code running in python?
I tried the same code in IDLE and SUBLIMETEXT and it only runs the first two lines and that is it.
as shown below
print("hello")
first = input("what is your firstname? ")
print("thanks")
surname = input("what is your surname? ")
Your code works fine. You may be confused with 2 things:
The input() function pauses the execution until you will finish entering something (or nothing) and pressing the Enter key.
After your last command, when you entered required info (your surname) and pressed the Enter key, there is no other command to print something.
So your program will finish silently, not using the last entered info (your surname).
It's waiting for the raw_input to run- raw_input is a function that waits for user input.
Try typing something in and pressing enter when the program asks What is your firstname?- the rest of the program will run.
I tried the code in CodeSculptor and it worked OK:
https://py3.codeskulptor.org/#user305_1ZWADnYLXt_0.py
You probably wanted a little modified version of your program:
print("hello")
first = input("what is your firstname? ")
surname = input("what is your surname? ")
print("thanks", first, surname)
The difference is in swapping the last 2 lines and adding the variables first and surname to the print() function (to display the entered info, i.e. the first name and the surname).
The output from doing it in the Python IDLE (after File→New, copy/paste the code into the new empty window, pressing F5, entering the name myprogram and pressing Enter; then — of course — entering names (John and Doe):
====== RESTART: C:/Program Files/Python36/Lib/idlelib/myprogram.py ======
hello
what is your firstname? John
what is your surname? Doe
thanks John Doe
>>>
In Sublime Text you need to install the SublimeREPL plugin.
Without it your program isn't able to accept user's input, so after your second command
first = input("what is your firstname? ")
it displays
hello
what is your firstname?
and waits for user input, but is unable to continue with the next one — there is no point in writing anything (your first name) to the prompt. (It will be displayed but your program will not accept it.)
After installing the SublimeREPL plugin, select
Tools → SublimeREPL → Python → Python-RUN current file.
Note: The “REPL” abbreviation means “Read-Evaluate-Print Loop”.
The code seems to be fine. Shouldn't be a problem in execution. It will take your first name as input, once you type that in and hit Enter, it will ask you for the surname.
TRY ADDING SEMICOLON AT THE END OF EACH LINE.
or maybe just type all of it in one line. For example:
print("Hello"); first = input("your first name"); print("HELLO 2"); second = input("your last name")

Input function need to press enter several times to get value after wake up Python uiautomator

I'm trying to get something on Android phone using uiautomator, and if it cannot get it, I'll let user to manual input the value. However, the input() function is strange after uiautomator is called, user needs to press ENTER key once before input values. If two uiautomator are called, user needs to press ENTER key twice before input values.
Any idea how to fix it?
Thanks!
import uiautomator
d = uiautomator.Device('serial number', 5555)
print(d.info)
in = input('Type something:')
print(in)
I expect to get user input values after press ENTER key once. However, user needs to press ENTER key before type something then press ENTER key again to pass input values to parameter in. The more uiautomator is called, the more ENTER key needs to be pressed before type something.
It is uiautomator bug and is no longer updated. uiautomator2 has fixed this.

Does python takes enter as an input?

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.

Python: Execute a line by user input?

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.

Categories

Resources