Exit the cycle by pressing ENTER

I am new to python and I was asked to do some exercises using while and for loop. I was asked to do a program loop until the request was requested by the user only by pressing <Return> . So far I:

 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 tried: (as indicated in the exercise)

 if User == <Carriage return> 

and

 if User == <Return> 

but this leads to invalid syntax. Please could you advise me how to do this in the easiest way. Thanks

+8
python while-loop
source share
14 answers

I stumbled upon this page while (no pun intended) was 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") 
+13
source share

The exact thing you want;)

stack overflow

 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)[0]: line = raw_input() break i += 1 
+10
source share

Actually, I suppose you are looking for code that starts a loop until a key is pressed from the keyboard. Of course, the program should not wait for the user to enter it all the time.

  • If you use raw_input() in python 2.7 or input() in python 3.0, the program waits for the user to press a key.
  • If you do not want the program to wait for the user to press the key, but still want to run the code, you need to do a little more complicated thing when you need to use the kbhit() function in msvcrt .

Actually, there is a recipe in ActiveState where they solved this problem. Please follow this link.

I think the following links will also help you better understand.

Hope this helps you do your job.

+4
source share

Use the print statement to find out what raw_input returns when you press enter . Then modify your test to compare with it.

+2
source share

You need to know what the User variable will look like when you just press Enter. I will not give you the full answer, but the advice: Burn the translator and try it. It's not that complicated;) Note that print sep defaults to "\ n" (there were too many: o)

0
source share
 if repr(User) == repr(''): break 
0
source share

a very simple solution would be, and I see, you said that you would like a simple solution possible. A request to continue by the user after stopping the Etc. cycle

 raw_input("Press<enter> to continue") 
0
source share
 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. ''' 
0
source share

Here is the best and easiest 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." 
0
source share

You are almost there. the easiest way to do this is to look for an empty variable that you get when you press enter when prompted for input. My code is below 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...') 
0
source share

The following works from me:

 i = '0' while len(i) != 0: i = list(map(int, input(),split())) 
0
source share

This works for python 3.5 using parallel streaming. You can easily adapt this to be sensitive only to 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() 
0
source share

Here's a solution found (similar to the original):

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

Please note that the source question code has several problems:

  • if / else is outside the while loop, so the loop will run forever.
  • else missing a colon.
  • The else clause uses a double equal instead. It does not perform assignment; it is a useless expression of comparison.
  • It does not need a working variable, since the if clause executes a break .
-one
source share

If you want your user to press enter, then 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 
-2
source share

All Articles