input()
prompt = "If you tell us who you are, we can personalize the messages you see." prompt += "\nwhat is your first name? " name = input(prompt) print("\nhello, " + name + "!") 输出: If you tell us who you are, we can personalize the messages you see. what is your first name? Eric hello, Eric!
求模运算符:%,它将两个数相除并返回余数
print(4%3)
print(6%3)
print(1%3)
print(-1%3) #为啥是2
输出:
1
0
1
2
余数如果是0,可利用这点来判断奇/偶数
number = input("Enter a number, and I'll tell you if it's even or odd: ") number = int(number) if number % 2 == 0: #numbber % 2 == 0 ,number就是偶数,否则是奇数 print("\nThe number " + str(number) + " is even.") else: print("\nThe number " + str(number) + " is odd.") 输出: Enter a number, and I'll tell you if it's even or odd: 5 The number 5 is odd.
while循环:不断地运行,知道指定的条件不满足为止。
prompt = "\nTell me something, and I will repeat it back to you:" prompt += "\nEnter 'quit' to end the program." message = "" while message !='quit': message = input(prompt) if message != 'quit': print(message) 输出: Tell me something, and I will repeat it back to you: Enter 'quit' to end the program.hello everyone! hello everyone!
Tell me something, and I will repeat it back to you: Enter 'quit' to end the program.hello again! hello again!
Tell me something, and I will repeat it back to you:
Enter 'quit' to end the program.quit
标记
prompt = "\nTell me something, and I will repeat it back to you:" prompt += "\nEnter 'quit' to end the program." active = True while active: message = input(prompt) if message == 'quit': active = False else: print(message)
break
prompt = "\nPLS enter the name of a city you have visited:" prompt += "\n(Enter 'quit' when you are finished.) " while True: city = input(prompt) if city == 'quit': break #退出循环 else: print("I'd love to go to " + city.title() + "!") 输出: PLS enter the name of a city you have visited: (Enter 'quit' when you are finished.) Beijing I'd love to go to Beijing! PLS enter the name of a city you have visited: (Enter 'quit' when you are finished.) ShenYang I'd love to go to Shenyang! PLS enter the name of a city you have visited: (Enter 'quit' when you are finished.) quit
continue
current_number = 0 while current_number < 10: current_number += 1 if current_number % 2 == 0: continue print(current_number) 输出: 1 3 5 7 9
注:如果命令行出现无限循环,Ctrl+C退出
验证新注册用户,将未验证用户移动到已验证用户列表中
unconfirmed_users = ['alice','brian','candace'] #待验证用户 confirmed_users = [] #已验证的用户 while unconfirmed_users: #验证每个用户 current_user = unconfirmed_users.pop() print("Verifying user: " + current_user.title()) confirmed_users.append(current_user) #显示所有已验证用户 print("\nThe following users have been confirmed: ") for confirmed_user in confirmed_users: print(confirmed_user.title()) 输出: Verifying user: Candace Verifying user: Brian Verifying user: Alice The following users have been confirmed: Candace Brian Alice
删除列表中所有的指定元素
pets = ['dog','cat','dog','goldfish','cat','rabbit','cat'] print(pets) while 'cat' in pets: pets.remove('cat') print(pets) 输出: ['dog', 'cat', 'dog', 'goldfish', 'cat', 'rabbit', 'cat'] ['dog', 'dog', 'goldfish', 'rabbit']
responses = {} active = True while active: name = input("what's your name? ") response = input("what's your favorite language:") responses[name] = response repeat = input("go on?(yes/no)") if repeat == 'no': active = False print("\n -- Result -- ") for name,response in responses.items(): print(name.title() + "'s favorite language is :" + response.title()) 输出: what's your name? alex what's your favorite language:python go on?(yes/no)yes what's your name? suki what's your favorite language:C go on?(yes/no)no -- Result -- Alex's favorite language is :Python Suki's favorite language is :C