Python基础-用户交互程序
注释
当行注释:#被注释内容
多行注释:”””被注释内容””” (3个单引号或3个双引号)
例:
注释单行 #_username='zhang' _password='admin123' username=input("username:") password=input("passowrd:") 注释多行 ''' if _username==username and _password==password: print("Welcome user {name} login...".format(name=username)) else: print("Invalid username or password!") '''
打印多行方法:
info = """ ############################### ######Welcome to My Game####### ############################### Try to guess the age of Oldboy? """ print (info)
用户交互输入:
#!/usr/bin/env python #-*- coding:utf-8 -*- name = input(“what is your name?”) print (“Hello”+name)
username = input(“Username:”) password = input(“Password:”) print (username) print (password)
输入密码时,如果想要不可见,需要利用getpass模块中的getpass方法,即:
#!/usr/bin/env python #-*- coding:utf-8 -*- import getpass #调用getpass库 username = input(“Username:”) password = getpass.getpass(“Password:”) print (username) print (password)
打印内容中加上变量值:
name = input(“name:”) age = int(input(“age:”)) print (type(age)) #打印变量类型 job = input(“job:”) salary = int(input(“salary:”)) info = ‘’’ ---------info of %s------- #%s占位符(s是string,d是data,f是浮点<小数>) Name:%s Age:%d Job:%s Salary:%d ‘’’% (name,name,age,job,salary) Print(info)
另一种方法:
name = input(“name:”) age = int(input(“age:”)) print (type(age)) #打印变量类型 job = input(“job:”) salary = int(input(“salary:”)) info = ‘’’ ---------info of {_name}------- Name:{_name} Age:{age} Job:{job} Salary:{salary} ‘’’.format(_name=name, _age=age, _job=job, _salary=salary) Print(info)
还有一种方法:
name = input(“name:”) age = int(input(“age:”)) print (type(age)) #打印变量类型 job = input(“job:”) salary = int(input(“salary:”)) info = ‘’’ ---------info of {0}------- Name:{0} Age:{1} Job:{2} Salary:{3} ‘’’.format(name,age,job,salary) Print(info)