Python - The "input()" function doesn't get executed [duplicate] - python

This question already has answers here:
How to read multiple lines of raw input?
(16 answers)
Closed last year.
I wrote this code in Python3:
for _ in range(2):
inputs = input()
print(inputs)
It works fine when I provide the input values one by one.
It also works when the input values are copied and pasted as in Image 1 and the Image 2 is printed in the terminal.
However, when the input is copied and pasted as in Image 3, it just doesn't print the last line and expects the user to press enter (or to add more characters and then press enter) to finish the code: Image 4
What is going on?
Why is this happening and how can I solve it when some other program or a user gives the input that way?

There is no new line after the second input please provide new line (by hitting enter) .
After insert 1 10 there is no new line and input method is waiting for user to provide a new line so it will stop reading and return string as output.

Related

Print function does not run [duplicate]

This question already has answers here:
Python, writing multi line code in IDLE
(7 answers)
Copy-paste into Python interactive interpreter and indentation
(9 answers)
Copying and pasting code into the Python interpreter
(11 answers)
Closed 2 years ago.
Just started learning.
Put the code:
username = input("Enter username:")
print("Username is: " + username)
into IDLE and after it asks for input, I expect it to print something. It doesn't. It just asks for input and that's it. What am I missing?
Just to clarify to everyone- I had entered the code on separate lines and it says
Enter Username:
I enter 'John'
Nothing happened after that.
Edit: I think the answer is that you cannot do multi-line code in the shell. I had no idea what a 'shell' is as opposed to a file where you can do multi-line.
Python is a top-down language, meaning that it will only continue to the next line of code until the current line of code has finished running
The input() function requires that you provide input into the console, to continue to the next line of code. So, you have to input something and only then will it actually print anything.

How to change time in available line in python [duplicate]

This question already has answers here:
How to rewrite output in terminal
(4 answers)
Closed 2 years ago.
Let's assume there is a code which is using with anaconda prompt. While the program flowing, every second is printing to screen. However, here every second is printed the same or next line.
I want to print every second in the same place.
For example, print 30 and after 1 second later, delete 30 and print 29 to the same place.
How can I do that with python?
You can use \r to return to the start of line instead of moving the cursor to the next line (\n). Whether or not this works with your shell is an open question; e.g. IDLE occasionally has problems with this.
import time
for x in range(10):
print(x, end='\r')
time.sleep(1)
print() # to add a newline, finally
print('The end!')
After every second when you have to clear the screen you can use this :
import os
os.system('cls')

Why input never ends [duplicate]

This question already has answers here:
Can't send input to running program in Sublime Text
(5 answers)
Closed 2 years ago.
I have python 3.7 installed and I have this code:
print("Enter your name:")
x = input()
print("Hello, " + x)
I was writing the name and press enter but the input is not over, it is still running and waiting for more inputs
Edit: the problem is that input is never ending, doesn't matter how many enters I press
Sublime text doesn't support inputting data. Read this : Sublime text input not working
That said, apparently SublimeREPL provides this sort of functionality, although I don’t use it myself so I don’t know much about it.

How to take multiple inputs from a user in Python [duplicate]

This question already has answers here:
How to read user input until EOF?
(4 answers)
How do I read from stdin?
(25 answers)
Closed 17 days ago.
I'm trying to create a program where python will take multiple inputs until the user enters "END". Each input must be on a different line. I'm still new and learning python/programming in general so is there any way to do this? and how would I turn the inputs into a list?
What a demo might look like -
What are your hobbies?
painting
hiking
drawing
END
You can read input with the input() function. You want to read till 'END' is entered by the user. So, read the input and check whether it is 'END'. If not append it to the list else exit the loop.
In codes you can do like this.
inputs = [] # list to store the inputs
while True: # looping forever
data = input('Enter the data : ') # read the data from user to variable data
if data == 'END': # if END is read then exit the loop
break
else:
inputs.append(data) # otherwise, append the input to the list
print(inputs) # display the list.
Hope this helps.!!

Multi-line user input [duplicate]

This question already has answers here:
How to read multiple lines of raw input?
(16 answers)
Closed 7 years ago.
I want to be able to accept multi-line user input. The problem is that with my program the user can't just use Shift Enter to do it.
I want them to be able to paste in several paragraphs for translation and when it detects a new line it automatically takes it as an enter and cuts off the input. I am using raw_input, not input.
Does anyone know a way to overcome this, so it can accept pasted text with multiple lines without assuming it is the end of the input?
You can repeatedly ask for raw_input() in a loop and concatenate the lines until you hit some input that signalizes the end. A very common one would be an empty line:
allLines = []
print('Insert the text:')
while True:
line = raw_input('')
if line == '':
break
allLines.append(line)
fullInput = '\n'.join(allLines)
print('You entered this:')
print(fullInput)

Categories