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")
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.
This is my first day learning python from a book called "Automate the Boring Stuff with Python".
On the "your first program" section the program is this
# This program says hello and asks for my name.
print('Hello world!')
print('What is your name?') # ask for their name
myName = input("Mizu")
print('It is good to meet you, ' + myName)
print('The length of your name is:')
print(len(myName))
print('What is your age?') # ask for their age
myAge = input('20')
print('You will be ' + str(int(myAge) + 1) + ' in a year.')
but the program only runs
Hello world!
What is your name?
Mizu
but if i replace the input() functions with just values like myName = 'Mizu' , it prints the rest of the lines just fine.
What am i doing wrong here?
i used the default python editor and pycharm and both show no errors or anything.
Note, the book says
myName = input()
so that's what you put in your programme, not
myName = input("Mizu")
and then, likes others have written, when the programme gets there you type in your name.
CAUTION this is correct for python 3. Check your version. If you are using python 2, replace "input" with "raw_input()"
myName = raw_input()
input means that it's waiting for you to write something.
Type something and press"Enter"
It isn't stopped.
you need to type some thing after the third line like
$Mizu"Mizu".
The Mizu shown on the run is just a reminder message to let know which input are you giving value.
The programme is not stopped. It is waiting for your input.
In the input function you have to give some valut at the run time.
myName = input("Mizu"). In this line you have entered your question. You have to give answer at run time
myName = input("what is your name")
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.
Is it possible to have a script like the following in Python?
...
Pause
->
Wait for the user to execute some commands in the terminal (e.g.
to print the value of a variable, to import a library, or whatever).
The script will keep waiting if the user does not input anything.
->
Continue execution of the remaining part of the script
Essentially the script gives the control to the Python command line interpreter temporarily, and resume after the user somehow finishes that part.
What I come up with (inspired by the answer) is something like the following:
x = 1
i_cmd = 1
while True:
s = raw_input('Input [{0:d}] '.format(i_cmd))
i_cmd += 1
n = len(s)
if n > 0 and s.lower() == 'break'[0:n]:
break
exec(s)
print 'x = ', x
print 'I am out of the loop.'
if you are using Python 2.x: raw_input()
Python 3.x: input()
Example:
# Do some stuff in script
variable = raw_input('input something!: ')
# Do stuff with variable
The best way I know to do this is to use the pdb debugger. So put
import pdb
at the top of your program, and then use
pdb.set_trace()
for your "pause".
At the (Pdb) prompt you can enter commands such as
(Pdb) print 'x = ', x
and you can also step through code, though that's not your goal here. When you are done simply type
(Pdb) c
or any subset of the word 'continue', and the code will resume execution.
A nice easy introduction to the debugger as of Nov 2015 is at Debugging in Python, but there are of course many such sources if you google 'python debugger' or 'python pdb'.
Waiting for user input to 'proceed':
The input function will indeed stop execution of the script until a user does something. Here's an example showing how execution may be manually continued after reviewing pre-determined variables of interest:
var1 = "Interesting value to see"
print("My variable of interest is {}".format(var1))
key_pressed = input('Press ENTER to continue: ')
Proceed after waiting pre-defined time:
Another case I find to be helpful is to put in a delay, so that I can read previous outputs and decide to Ctrl + C if I want the script to terminate at a nice point, but continue if I do nothing.
import time.sleep
var2 = "Some value I want to see"
print("My variable of interest is {}".format(var2))
print("Sleeping for 5 seconds")
time.sleep(5) # Delay for 5 seconds
Actual Debugger for executable Command Line:
Please see answers above on using pdb for stepping through code
Reference: Python’s time.sleep() – Pause, Stop, Wait or Sleep your Python Code
I think you are looking for something like this:
import re
# Get user's name
name = raw_input("Please enter name: ")
# While name has incorrect characters
while re.search('[^a-zA-Z\n]',name):
# Print out an error
print("illegal name - Please use only letters")
# Ask for the name again (if it's incorrect, while loop starts again)
name = raw_input("Please enter name: ")
Simply use the input() function as follows:
# Code to be run before pause
input() # Waits for user to type any character and
# press Enter or just press Enter twice
# Code to be run after pause