Python user input & while loop beginner notes

input () get user input (acquired are strings oh) // function input () so that the program stops running, waiting for the user to enter some text.

// C is different from the user can be prompted to add the input, and scanf does not have this feature.

// prompt hint may be stored in a variable before being passed inpu when more than one line ()

continue to ignore the rest of the code in the loop, and return to the beginning of the current cycle

break exits the current loop, executing code outside of the current cycle

A small example:

. 1 prompt = " . The If you Tell US WHO you are, WE CAN PE rsonalize The messages you See " 
2 prompt + = " \ nWhat IS your FirstName: " 
. 3 Active = True # control cycle 
. 4  the while Active:
 . 5      name = INPUT ( prompt)
 . 6      IF name == ' quit ' : # control loop 
. 7      Active = False
 . 8      # alternative to break, the end of cycle 
9      # transducer continue to become the dead loop 
10      the else :
 . 11          Print( " The Hello! " + Name + " ! " )
 12 is  
13 is      Print ( ' . 4 ' ) # test whether out of the loop

 

* Python 2.7 is used raw_input () get user input

Use while loop to process lists and dictionaries

A simple move list:

1 unconfirmed_users =['alice', 'brain', 'cand', 'ace',]
2 confirmed_users =[]
3 while unconfirmed_users:
4     current_user = unconfirmed_users.pop()#弹出并记录
5     print("Verifying user: " + current_user.title())
6     confirmed_users.append(current_user)#添加到新列表中
7     print("\nThe following users have been confirmed: ")
8     for confirmed_user in confirmed_users:#显示已认证用户
9         print(confirmed_user.title())

Delete all elements in the list of specific values:

1  the while  ' alice '  in unconfirmed_users # long as there is this element unconfirmed_users alice list has been circulating 
2      unconfirmed_users.remove ( ' alice ' ) # delete alice elements unconfirmed_users list

Filled with user input dictionary:

. 1 names = {}
 2  the while . 1 :
 . 3      name = INPUT ( " What apos your name: " )
 . 4      IF name == ' NO ' : # forced input ha. 
. 5          Continue 
. 6      Day = INPUT ( " How are you Old: " )
 . 7      if Day == ' NO ' :
 . 8          Continue 
. 9  # than if the two-elif be taken if 
10      # if name == NO: 
. 11          # Continue 
12 is     # Elif Day == NO: 
13 is          # Continue 
14      names [name] = Day # automatically Add pair 
15      # alien_0 [ 'x_position'] = 0 to add a dictionary key 
16      # alien_0 [ 'x_position'] = 25 Review dictionary value 
17      REPEAT = the INPUT ( " Would you like the respond to the let the Person AN OTHER (yes / NO)? " )
 18      IF REPEAT == ' NO ' :
 19          BREAK 
20  for a, b in names.items (): # print key-value pairs 
21      Print (a.title () + b)

 

Guess you like

Origin www.cnblogs.com/MR---Zhao/p/12325685.html