Enter compound statements interactivly - python

I am having trouble running this simple statement in the IDLE prompt.
if True:
print("True") # need to press ENTER twice?
else:
print("False") # need to press ENTER twice?
Any help is appreciated.

IDLE is automatically indenting when you press return. Press delete to remove the automatic indentation.
if True:
print("True") # press return, then backspace
else:
print("False")

Related

Keyboard key's pressed a lot of times

I want to create program that check if a is pressed. But for a lot of times, not only one time. I tried using for loop and function but I failed.
import keyboard
def lagun():
while True:
try:
if keyboard.is_pressed('a'):
print('you pressed a')
break
except:
break
Can anyone could tell me how to modify this code to check is a is pressed a lot of times?

Breaking Loops as soon as their condition breaks in Python

I would like to know how to break a loop as soon as its condition breaks.Its a little difficult to explain I will attach some code that will explain it well.I think I have to use things like break and continue but I am not sure where in my code should I place or maybe I need to do something else.your help would be greatly appreciated.
while 1:
if image1 appears on screen:
# do some task(it includes multiple if statements in it too)
else:
#do task 1
#do task 2
#do task 3
I want as soon as image1 appears on screen else loop should exit even if it's incomplete.like if image1 appears when else loop is doing task 2 it should exit without completing it and move to the if loop.
with my current code if image1 appears on screen the else loop only exits when all 3 tasks are done.
I want to exit the else loop not the if loop and
by exit or break I mean shift from else loop to if loop
You can use a return, like this:
if image1 appears on screen:
#do some task**
return
else:
press arrow key up for 2secs**
press arrow down key for 2sec**
UPD
I suggest this code is a part of some function.
Otherwise you will have to put it inside some function since return can be called from inside a function only.
It is also a good practice to define and use functions.
If image1 appears on screen, the else part will not execute (that's how if-else functionality works).
You can just add "break" in the end of the if (before the else)
if image1 appears on screen:
do some task**
break
else:
press arrow key up for 2secs**
press arrow down key for 2sec**
Use break where you want to stop the loop -
if image1 appears on screen:
#do some task**
break
else:
press arrow key up for 2secs**
press arrow down key for 2sec**
You can also check this
Use a break in if condition
#loop
if image1 appears on screen:
do some task**
break # this will break the loop at running iteration
else:
press arrow key up for 2secs**
press arrow down key for 2sec**
#loop

Make broken loop run again? [duplicate]

This question already has answers here:
Detect if key was pressed once
(3 answers)
Closed 2 years ago.
I made a program which prints 'F pressed!' when i press the button 'F'. I don't want it to spam my console with that so I made the While-Loop break after that. How can I make the function run again and make the program not stop working? Or just make the text only appear once everytime I press the button?
import win32api
while True:
keystate = win32api.GetAsyncKeyState(0x46)
if keystate < 0:
print('F pressed!')
break
else:
pass
Thanks
How can I make the function run again and make the program not stop working?
The break statement terminates the loop containing it. Control of the program flows to the statement immediately after the body of the loop.
Therefore, you just remove break and everything is ok.
Or just make the text only appear once everytime I press the button?
If the least significant bit is set, the key was pressed after the
previous call to GetAsyncKeyState.
Try the code sample below:
import win32api
while True:
keystate = win32api.GetAsyncKeyState(0x46)&0x0001
if keystate > 0:
print('F pressed!')

How does keyboard.is_pressed() work in Python?

I'm trying to figure out when i press 'u' only 1 times, why is it pressing 'w' infinite times. Print functions don't work neither but if i delet the keyboard.press('w') and keyboard.release('w'), the print functions start to work correctly ( it's printing out 4 until i press a button then it prints out the right number and when i release the button it writes out 4 again)
while True:
if keyboard.is_pressed('u'):
keyboard.press('w')
keyboard.release('w')
print(0)
elif keyboard.is_pressed('j'):
#keyboard.press_and_release('s')
print(1)
elif keyboard.is_pressed('k'):
#keyboard.press_and_release('d')
print(2)
elif keyboard.is_pressed('h'):
#keyboard.press_and_release('a')
print(3)
else:
print(4)
keyboard.release('w')
I know that it was a year ago, but I have found this question today.
Sooo, according to the Keyboard API:
keyboard.press(hotkey) emulates pressing a hotkey on keyboard.
keyboard.release(hotkey) emulates releasing a hotkey on keyboard.
A hotkey is an key number, key name, or combination of two or more keys.
keyboard.press_and_release(hotkey) or keyboard.send(hotkey) emulates pressing and releasing a key or hotkey.
Pressing and releasing behaviour depends on values of do_press and do_release when using keyboard.send().
For example:
keyboard.send('space', do_press=True, do_release=True)
will emulate pressing and releasing space key, but:
keyboard.send('space', do_press=False, do_release=True)
will emulate only releasing space key.
keyboard.is_pressed(key) returns True if a specified key has been pressed, and False otherwise
Hope I've helped!

How can make the program continue only when enter key is hit?

ent = input("Press the Enter key to spin the roulette wheel.")
if ent == "":
print("Roulette wheel spinning...")
print("Roulette wheel spinning...")
else:
#I want it to do nothing till only enter is hit What can I write here?
Is there way so when a key other than enter alone is pressed that it wont do anything until only enter is pressed? Using input allows the users to write something and therefore when they hit enter it will always run through. I'd prefer it if I can disable the writing and only respond when the enter key is hit.
You could put a while loop around the input so until the input equals enter the user can't continue. For example:
while True:
ent = input("Press the Enter key to spin the roulette wheel.")
if ent == "":
break
print("Roulette wheel spinning...")
print("Roulette wheel spinning...")
Hope this could help.

Categories

Resources