用户输入及交互程序 - 格式化输出
方法1: 字符串的拼接
方法2: %s占位符
name = input("name:") age = int(input("age:")) #integer转成数字 job = input("job:") salary = input("salary:") info =‘‘‘ -----------info of %s --------- name: %s age: %d job: %s salary: %s ‘‘‘%(name, name,age, job,salary) print(info)
方法3: format的使用(推荐)
name = input("name:") age = int(input("age:")) #integer转成数字 job = input("job:") salary = 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)
or
name = input("name:") age = int(input("age:")) #integer转成数字 job = input("job:") salary = input("salary:") info2 =‘‘‘ -----------info2 of {0} --------- name: {0} age: {1} job: {2} salary: {3} ‘‘‘.format(name, age, job,salary) print(info2)
模块引入
import getpass #getpass是个standard moduel, 在pycharm中不好使。 _username = ‘alex‘ _password = ‘abc123‘ username = input("username:") password = input("password:") #password = getpass.getpass("password") # 如果要秘闻psw,就用这个 if _username == username and _password == password: print("welcome user {name} login...".format(name=username)) else: print ("invalid username or password")
时间: 2024-11-09 15:26:16