格式化字符串:
name=input("input your name:")age=int(input("input your age:"))job=input("input your job:")msg='''information of user %s:============================Name: %sAge: %dJob: %s-----------End--------------'''%(name,name,age,job)print(msg)
Tab补全:
#!/usr/bin/env python # python startup file import sysimport readlineimport rlcompleterimport atexitimport os# tab completion readline.parse_and_bind('tab: complete')# history file histfile = os.path.join(os.environ['HOME'], '.pythonhistory')try: readline.read_history_file(histfile)except IOError: passatexit.register(readline.write_history_file, histfile)del os, histfile, readline, rlcompleter
if...else语句 验证用户登录:
#!/usr/bin/python#-*-coding:utf-8 -*-age=25counter=0for i in range(10): if counter<3: guess_num=int(input("Input an num:")) if guess_num==age: print("you are right") break elif guess_num>age: print("it is > age") else: print("it is < age") else: #print("你已经输错了3次") #break continue_confirm=input("you are attempt three times,Are you continue") if continue_confirm=='y': counter=0 continue # 跳出当次循环进入下一个循环 else: print("bye") break counter=counter+1 if i>10: print("你已经超过10次")