I am a python newbie and have been asked to carry out some exercises using while and for loops. I have been asked to make a program loop until exit is requested by the user hitting
<Return> only. So far I have:
User = raw_input('Enter <Carriage return> only to exit: ') running = 1 while running == 1: Run my program if User == # Not sure what to put here Break else running == 1
I have tried: (as instructed in the exercise)
if User == <Carriage return>
if User == <Return>
but this only results in invalid syntax. Please could you advise me on how to do this in the simplest way possible. Thanks
If you want your user to press enter, then the raw_input() will return "", so compare the User with "":
User = raw_input('Press enter to exit...') running = 1 while running == 1: Run your program if User == "": break else running == 1
The exact thing you want ;)
import sys, select, os i = 0 while True: os.system('cls' if os.name == 'nt' else 'clear') print "I'm doing stuff. Press Enter to stop me!" print i if sys.stdin in select.select([sys.stdin], , , 0): line = raw_input() break i += 1
Here's a solution (resembling the original) that works:
User = raw_input('Enter <Carriage return> only to exit: ') while True: #Run my program print 'In the loop, User=%r' % (User, ) # Check if the user asked to terminate the loop. if User == '': break # Give the user another chance to exit. User = raw_input('Enter <Carriage return> only to exit: ')
Note that the code in the original question has several issues:
elseis outside the while loop, so the loop will run forever.
elseis missing a colon.
ifclause performs a
Actually, I suppose you are looking for a code that runs a loop until a key is pressed from the keyboard. Of course, the program shouldn't wait for the user all the time to enter it.
raw_input()in python 2.7 or
input()in python 3.0, The program waits for the user to press a key.
Actually, there is a recipe in ActiveState where they addressed this issue. Please follow this link
I think the following links would also help you to understand in much better way.
I hope this helps you to get your job done.
Use a print statement to see what
raw_input returns when you hit
enter. Then change your test to compare to that.
if repr(User) == repr(''): break
This works for python 3.5 using parallel threading. You could easily adapt this to be sensitive to only a specific keystroke.
import time import threading # set global variable flag flag = 1 def normal(): global flag while flag==1: print('normal stuff') time.sleep(2) if flag==False: print('The while loop is now closing') def get_input(): global flag keystrk=input('Press a key \n') # thread doesn't continue until key is pressed print('You pressed: ', keystrk) flag=False print('flag is now:', flag) n=threading.Thread(target=normal) i=threading.Thread(target=get_input) n.start() i.start()
The following works from me:
i = '0' while len(i) != 0: i = list(map(int, input(),split()))
a very simple solution would be, and I see you have said that you would like to see the simplest solution possible. A prompt for the user to continue after halting a loop Etc.
raw_input("Press<enter> to continue")
I ran into this page while (no pun) looking for something else. Here is what I use:
while True: i = input("Enter text (or Enter to quit): ") if not i: break print("Your input:", i) print("While loop has exited")
user_input=input("ENTER SOME POSITIVE INTEGER : ") if((not user_input) or (int(user_input)<=0)): print("ENTER SOME POSITIVE INTEGER GREATER THAN ZERO") #print some info import sys #import sys.exit(0) #exit program ''' #(not user_input) checks if user has pressed enter key without entering # number. #(int(user_input)<=0) checks if user has entered any number less than or #equal to zero. '''
You are nearly there. the easiest way to get this done would be to search for an empty variable, which is what you get when pressing enter at an input request. My code below is 3.5
running = 1 while running == 1: user = input(str('Enter <Carriage return> only to exit: ')) if user == '': running = 0 else: print('You had one job...')
Here is the best and simplest answer. Use try and except calls.
x = randint(1,9) guess = -1 print "Guess the number below 10:" while guess != x: try: guess = int(raw_input("Guess: ")) if guess < x: print "Guess higher." elif guess > x: print "Guess lower." else: print "Correct." except: print "You did not put any number."
You need to find out what the variable User would look like when you just press Enter. I won't give you the full answer, but a tip: Fire an interpreter and try it out. It's not that hard ;) Notice that print's sep is '\n' by default (was that too much :o)