博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Python实例1—格式化输出
阅读量:5289 次
发布时间:2019-06-14

本文共 2565 字,大约阅读时间需要 8 分钟。

老男孩教学学习笔记:

 

 实例1:格式化输出

# Author:Alex Liname = input("name:")# raw_input 2.x   input 3.x# input 2.x =age = int(input("age:"))  # integerprint(type(age), type(str(age) ))job = input("job:")salary= input("salary:")info = '''-------- info of  %s  -----Name:%sAge:%dJob:%sSalary:%s''' % (name, name, age, job, salary)info2 = '''-------- info of {_name}  -----Name:{_name}Age:{_age}Job:{_job}Salary:{_salary}'''.format(_name=name,           _age=age,           _job=job,           _salary=salary)info3 = '''-------- info of {0} -----Name:{0}Age:{1}Job:{2}Salary:{3}'''.format(name, age, job, salary)print(info)print(info2)print(info3)

 

实例2:密码密文输入

# Author:Alex Liimport getpass_username = 'alex'_password = 'abc123'username = input("username:")# password = getpass.getpass("password:")password = input("password:")if _username == username and _password == password:    print("Welcome user {name} login...".format(name=username))else:    print("Invalid username or password!")

 

实例3:

猜年龄(一次)

# Author:Alex Li age_of_oldboy = 56guess_age = int(input("guess age:"))if guess_age == age_of_oldboy:    print("yes, you got it")elif guess_age > age_of_oldboy:    print("think smaller ...")else:    print("think bigger!")

猜年龄(多次)——循环

# Author:Alex Liage_of_oldboy = 56count = 0while count < 3:    guess_age = int(input("guess age:") )    if guess_age == age_of_oldboy:        print("yes, you got it. ")        break    elif guess_age > age_of_oldboy:        print("think smaller...")    else:        print("think bigger!")    count += 1else:    print("you have tried too many times..bye")

循环学习:

# Author:Alex Li'''count = 0while True:    print("count:",count)    count = count +1  #count +=1    if count == 1000:        break''''''for i in range(0,10):    if i <3:        print("loop ",i)    else :        continue    print("hehe...")'''for i in range(10):    print('----------',i)    for j in range(10):        print(j)        if j > 5:            break

循环结束后询问是否继续:

# Author:Alex Liage_of_oldboy = 56count = 0while count < 3:    guess_age = int(input("guess age:") )    if guess_age == age_of_oldboy :        print("yes, you got it. ")        break    elif guess_age > age_of_oldboy:        print("think smaller...")    else:        print("think bigger!")    count += 1 if count == 3:        countine_confirm = input("do you want to keep guessing..?")        if countine_confirm != 'n':            count = 0

 

作业:

一、编写登录接口:

  1、输入用户名密码

  2、认证成功后显示欢迎信息

  3、用户名3次输入错误后,退出程序

  4、密码3次输入错误后,锁定用户名

二、多级菜单

  三级菜单

  可依次选择进入各子菜单

  所需知识点:列表、字典

  

 

转载于:https://www.cnblogs.com/ranxf/p/8066958.html

你可能感兴趣的文章
[Flex] flex手机项目如何限制横竖屏?只允许横屏?
查看>>
tensorflow的graph和session
查看>>
Benelux Algorithm Programming Contest 2014 Final(第二场)
查看>>
jq实战-表单验证
查看>>
随机变量的期望为什么把不是自己密度函数当成自己的权重来求期望呢?
查看>>
6-1 并行程序模拟 uva210
查看>>
JavaScript动画打开半透明提示层
查看>>
Mybatis生成resulteMap时的注意事项
查看>>
上周热点回顾(6.17-6.23)
查看>>
第一章 Python基础
查看>>
jquery-jqzoom 插件 用例
查看>>
javascript知识点记录01
查看>>
javascript事件代理
查看>>
02编程语言及python初识
查看>>
es6 新增特性
查看>>
1007. Maximum Subsequence Sum (25)
查看>>
《算法》C++代码 快速排序
查看>>
iframe的父子层跨域 用了百度的postMessage()方法
查看>>
Js apply方法与call方法详解 附ES6新写法
查看>>
linux php全能环境一键安装,小白福利!
查看>>