Why can't I print outside loop [duplicate] - python

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.

Related

I am making a computer vision code with python and i got stuck [duplicate]

This question already has answers here:
I'm getting an IndentationError. How do I fix it?
(6 answers)
Closed 3 months ago.
everytime i try to run the code i get the following -->
cap = cv2.VideoCapture(0)
^
IndentationError: expected an indented block
I tried moving the "def hands():" and the error just keeps occurring
You need to indent one time cap variable and all code that you what to be executed when calling hands.

How would I return to the start of a while statement instead of just straight breaking [duplicate]

This question already has answers here:
Example use of "continue" statement in Python?
(10 answers)
Closed 8 months ago.
When I try to use a break statement to go back to the start of the infinite loop, it breaks right back to the first while loop asking for pie_type. Shown Here.
break is useful to exit loop (as if in haste) due to some external condition being met.
continue is used to transfer control back to the beginning of loop

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.

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

end="" and time.sleep() relation [duplicate]

This question already has answers here:
Command prompt can't write letter by letter? [duplicate]
(3 answers)
Closed 6 years ago.
import random
import time
myStr="1234567890qwertyuiopasdfghjklzxcvbnm.,*/-+>£#$½{[]}\|!'^+%&/()=?_é><`;:"
def generator():
while True:
randomLetter=random.choice(myStr)
print(randomLetter,end="")
time.sleep(0.1)
generator()
I want to slow down my while loop . If i write time.sleep it doesnt output anything .If i delete end="" part it works but i still want to write everything in one line and I also tried with "sys.stdout.write" but it didnt work again.
do sys.stdout.flush() after print

Categories