# Function input () works: let the program pauses, waiting to enter some text. After acquiring a user input, which is stored in a variable, to facilitate reuse. such as:
name = input("Please enter your name:")
print("Hello,"+name+" !")
Use # int () to obtain the input values. such as:
age = int(input("How old are you?"))
if age > 30:
print ( "Pentium 4 people!")
else:
print ( "You're young!")
# Modulus operator: to divide two numbers and returns the remainder. such as:
number = int(input("Enter a number,and I'll tell you if it's even or odd:"))
if number % 2 == 0:
print("The number "+str(number)+" is even.")
else:
print("The number "+str(number)+" is odd.")
#for cycle and cycle difference while: for loop for a block of code for each element of the set, and the while loop continues to run, until a specified condition is not satisfied so far. such as:
current_number = 0
while current_number <= 5:
print(current_number)
current_number += 1
# Let the user choose when to exit, such as:
prompt = "Tell me something,and I will repeat it back to you :"
prompt += "\nEnter 'quit' to end the program.\n"
message = "" # is defined as the empty grounds that the message in line with the conditions, can be executed while loop
while message != 'quit':
message = input(prompt)
print(message)
# Asking a lot of conditions are met before the program continues to run, you can define a variable to determine whether the entire program is active. This variable called signs, traffic lights served as a program. Can let the program when the flag is True
# Continue to run and cause the program to stop running when the flag is False in any event. such as:
prompt = "Tell me something,and I will repeat it back to you :"
prompt += "\nEnter 'quit' to end the program.\n"
active = True
while active:
message = input(prompt)
if message == 'quit':
active = False
else:
print(message)
# Use break out of the loop Note: break statement can be used in any python cycle (traversal, while loops, for loops) in. such as:
prompt = "Tell me something,and I will repeat it back to you :"
prompt += "\nEnter 'quit' to end the program.\n"
while True:
message = input(prompt)
if message == 'quit':
break
else:
print(prompt)
# To return to the beginning of the cycle, and decide whether to continue the cycle, you can use the continue statement based on a conditional test. such as:
current_number = 0
while current_number < 10:
current_number += 1
if current_number % 2 == 0:
continue
print(current_number)
# Use a while loop to process lists and dictionaries, reason not to use a for loop for loop is modified in the list will lead to python difficult to track its elements. such as:
# Move elements between lists
unconfirmed_users = ['rose','jack','tony']
confirmed_users = []
while unconfirmed_users:
current_user = unconfirmed_users.pop()
print("Verify user: "+current_user.title())
confirmed_users.append(current_user)
print("The following users have been cnfirmed:")
for confirmed_user in confirmed_users:
print(confirmed_user)
# Delete all list elements contain a specific value
pets = ["cat","dog","goldfish","cat","rabbit","tiger","cat","flyfish"]
print(pets)
while 'cat' in pets:
pets.remove('cat')
print(pets)
# Use user input to populate dictionary
responses = {}
active = True
while active:
name = input("what's your name?")
mountain = input("which mountain would you like to climb someday?")
reponses[name] = mountain
message = input("would you like to let another person respond?(yes or no)")
if message == "no":
active = False
for key,value in reponses.items():
print(key.title() + " would like to climb " + value + " .")