Print function does not run [duplicate] - python

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.

Related

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

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.

Is there an easy way to have a multiline input in Python 3? [duplicate]

This question already has answers here:
How to read multiple lines of raw input?
(16 answers)
How to get multiline input from the user [duplicate]
(5 answers)
Closed 2 years ago.
If I have a large multiline input and I run this line:
story = input()
it reads it as two inputs and my program doesn't process it correctly. I found a fix on stackoverflow that I modified to look like this:
story = []
while True:
try:
line = input()
except EOFError:
break
story.append(line)
story = ' '.join(story)
But I would need to use ctrl+D to induce an EOFError to stop the input. Is there a more intuitive way on Python to copy paste a large multiline input?

if statement being ignored in Python [duplicate]

This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 2 years ago.
So I am not great with Python if it is not obvious, but I can't seem to figure out why it is straight up ignoring my if statement. Could anyone please shed some light on this, I have double checked my variable names.
I have more code above this that imports random and does some password checking successfully, but this section of code isn't working as expected.
correct=0
q1a=random.randint(0,10)
q1b=random.randint(0,10)
answer1=q1a+q1b
print(q1a, "+", q1b, "=")
a1=input("Answer: " )
if answer1==a1:
print("Correct!")
correct+=1
print(answer1,a1,correct)```
Typecast input to integer, by default it takes it as a string.
a1 = int(input("Answer:"))

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.

Why can't I print outside loop [duplicate]

This question already has answers here:
Python indentation mystery
(3 answers)
Closed 6 years ago.
I am a code beginner.Can anyone tell me what happend if I get a syntaxerror when putting the "print" outside the loop
Seems like you are using an interactive interpreter shell. You should hit enter after the last line of the loop before you try to print.
In future questions please write your code in the question body instead of attaching a screenshot.

Categories