1、函数input()接受一个参数:即要向用户显示的提示或说明。有时候提示可能超过一行,在这种情况下,可将提示存储在一个变量中,再将该变量传递给函数input()。这样使 得input()语句非常清晰。
prompt = "If you tell us who you are,we can personalize the message you see." prompt += "\nWhat is your first name? " name = input(prompt) print("\nHello," + name.title() + "!")
输出结果:
If you tell us who you are,we can personalize the message you see. What is your first name? juzi Hello,Juzi!
2、使用函数input()时,Python将用户输入解读为字符串。函数int(),它让Python将输入视为数值。
在Python2.7中获取输入是使用raw_input()
3、求模运算符(%),将两个数相除并返回余数。
判断一个数是奇数还是偶数:
number = input("Enter a number , and I'll tell you if it's even or odd: ") number = int(number) if number % 2 ==0: 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: 29 The number 29 is odd.
4、使用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) 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.quit! quit! Tell me something, and I will repeat it back to you: Enter 'quit' to end the program.quit quit
经过测试发现,当用户想要结束时,’quit’也被打印出来。为了解决这个问题,需要使用一个if测试:
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.quit Process finished with exit code 0
在要求很多条件都满足才继续运行的程序中,可以定义一个变量,可用于判断整个程序是否处于活动状态。这个变量被称为标志。
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)
5、要立即退出while循环,不再运行循环余下的代码,也不管条件测试的结果如何,可使用break语句。在任何Python循环中都可使用break语句。
要返回到循环开头,并根据条件测试结果决定是否继续执行循环,可使用continue语句。
如果程序陷入无限循环,可按Ctrl+C,也可以关闭显示程序输出的终端窗口。(笔者用的是pycharm,发现Ctrl+C不可以终止程序,可以用Crtl+f2)
6、isdigit()判断是否为整数。
active =True while active: age = input("how old are you? ") if age.isdigit(): age = int(age) if age<3: print("free") elif age<=12: print ("$10") elif age <100: print ("$15") else: active=False
必须按要求输入,输入其他的都会跳出循环。
how old are you? 23 $15 how old are you? Process finished with exit code 0
7、假设有一个列表,其中包含新注册但未验证的网站用户;想要验证后将他们移到另一个已验证用户列表。可以使用一个while循环,在验证用户的同时将其从未验证用户中提取出来,再将其加入到另一个已验证用户列表中。代码类似于下面这样:
# 首先,创建一个待验证用户列表 # 和一个用于存储已验证用户的空列表 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 condined:") for confirmed_user in confirmed_users: print(confirmed_user.title())
输出结果:
Verifying user:Candace
Verifying user:Brian
Verifying user:Alice
The following users have been condined:
Candace
Brian
Alice
8、使用用户输入来填充字典。
responses = {} #设置一个标志,指出调查是否继续 polling_active = True while polling_active: # 提示输入被调查者的名字和回答 name = input("\nWhat is your name? ") response = input("Which mountion would you like to climb someday? ") #将答卷存储在字典中 responses[name]=response #看看是否还有人要参与调查 repeat = input("Would you like to let another person respond?(yes/no) ") if repeat == 'no': polling_active = False #调查结束,显示结果 print("\n- - - Poll Results - - -") for name,response in responses.items(): print(name + " would like to climb "+response + ".")
输出测试:
What is your name? Eric Which mountion would you like to climb someday? Denali Would you like to let another person respond?(yes/no) yes What is your name? Lynn Which mountion would you like to climb someday? Devil's Thumb Would you like to let another person respond?(yes/no) no - - - Poll Results - - - Eric would like to climb Denali. Lynn would like to climb Devil's Thumb.