• python入门(一)作业


     

    一,流程控制之if...else

    1. 如果:女人的年龄>30岁,那么:叫阿姨,否则:叫小妞

    1 age_of_girl = 21
    2 if age_of_girl >30:
    3     print('阿姨')
    4 else:
    5     print('小妞')

    2.如果:女人的年龄>=18并且<22岁并且身高>170并且体重<100并且是漂亮的,如果表白成功,否则’给我滚‘,那么:表白,否则:叫阿姨

     1 age_of_girl=18
     2 height=171
     3 weight=99
     4 is_pretty=True
     5 seccess=True
     6 
     7 if age_of_girl>=18 and age_of_girl <=22 and  height >170 and weight < 100 and is_pretty==True:
     8     if seccess:
     9         print('表白成功')
    10     else:
    11         print('给我滚')
    12 else:
    13     print('阿姨好')

     3. 如果:成绩>=90,那么:优秀,如果成绩>=80且<90,那么:良好,如果成绩>=70且<80,那么:普通,其他情况:很差,并且到很差的时候退出

     1 while True:
     2     score = input('>>>:')
     3     score = int(score)
     4     if score >=90:
     5         print('优秀')
     6     elif score>=80 and score<90:
     7         print('良好')
     8     elif score>=70 and score<80:
     9         print('普通')
    10     else:
    11         print('很差')
    12         break

     4.用户登录验证  

    1 name=input('>>>:')
    2 password=input('>>>')
    3 if name=='agen' and password=='123':
    4     print('登陆成功')
    5 else:
    6     print('登陆失败')

    5.根据用户输入内容打印其权限

    #(1)定义用户的权限 
     1 while True:
     2     agen='超级管理员'
     3     lisa='内容管理员'
     4     rupee='普通管理员'
     5     asshole='屌丝'
     6     name=input('>>>:')
     7     if name=='agen':
     8         print('超级管理员')
     9     elif name=='lisa':
    10         print('内容管理员')
    11     elif name=='rupee':
    12         print('普通管理员')
    13     elif name=='asshole':
    14         print('屌丝')
    15     else:
    16         print('')
    17         break

    6.

    # 如果:今天是Monday,那么:上班
    # 如果:今天是Tuesday,那么:上班
    # 如果:今天是Wednesday,那么:上班
    # 如果:今天是Thursday,那么:上班
    # 如果:今天是Friday,那么:上班
    # 如果:今天是Saturday,那么:出去浪
    # 如果:今天是Sunday,那么:出去浪
     1 today=input('>>: ')
     2 
     3 if today in ['Saturday','Sunday']:
     4     print('出去浪')
     5 elif today in ['Monday','Tuesday','Wednesday','Thursday','Friday']:
     6     print('上班')
     7 else:
     8     print('''必须输入其中一种:
     9     Monday
    10     Tuesday
    11     Wednesday
    12     Thursday
    13     Friday
    14     Saturday
    15     Sunday
    16     ''')

    二 流程控制之while循环

    7.猜年龄,最多可以猜三次

     1 i=1
     2 while i<=3:
     3     age_of_oldboy = 48
     4 
     5     guess = int(input(">>:"))
     6 
     7     if guess > age_of_oldboy :
     8         print("猜的太大了,可以试3次,第%s次"%i)
     9 
    10     elif guess < age_of_oldboy :
    11         print("猜的太小了,可以试3次,第%s次"%i)
    12     else:
    13         print("恭喜你,猜对了...")
    14     i+=1

    8.循环验证用户输入的用户名与密码 2 认证通过后,运行用户重复执行命令 3 当用户输入命令为quit时,则退出整个程序(###)

     1 name='egon'
     2 password='123'
     3 
     4 while True:
     5     inp_name=input('用户名: ')
     6     inp_pwd=input('密码: ')
     7     if inp_name == name and inp_pwd == password:
     8         while True:
     9             cmd=input('>>: ')
    10             if not cmd:continue
    11             if cmd == 'quit':
    12                 break
    13             print('run <%s>' %cmd)
    14     else:
    15         print('用户名或密码错误')
    16         continue
    17     break

    三 while 循环练习题

    9.使用while循环输出1 2 3 4 5 6 8 9 10

    1 i=1
    2 while i<11:
    3     if i==7:
    4         pass
    5     else:
    6         print(i)
    7     i+=1

    10. 求1-100的所有数的和 

    1 i=0
    2 count=1
    3 while count<=100:
    4     i+=count
    5     count+=1
    6 print(i)

    11.求1-2+3-4+5-6+7-8+9-10+11 ... 99的所有数的和(###)

     1 count=0
     2 sum=0
     3 while count <=100:    #第一轮,count=0      #第二轮,count=1             第三轮,count=2     第四轮....
     4     if count%2 ==0:    #第一轮,count%2=0   #第二轮,count%2==1,不满足    第三轮,满足
     5         sum=sum+count  #第一轮,左sum=0+0                               第三轮,左sum=-1+2
     6 
     7     elif count%2 ==1:                      #第二轮,count%2==1,满足
     8         sum=sum-count                      #第二轮,左sum=0-1
     9     count+=1
    10 else:
    11  print(sum)
    View Code

     12.用户登录(3次重试)

     1 i=1
     2 while i<=3:
     3     name=input('>>>:')
     4     password=input('>>>:')
     5     if name=='egon' and password=='123':
     6         print('登陆成功')
     7         break
     8     else:
     9         print('登陆失败')
    10         i+=1
    

    13 :猜年龄游戏要求

    #允许用户最多尝试3次,3次都没猜对的话,就直接退出,如果猜对了,打印恭喜信息并退出
    
    
     1 age=18
     2 count=0
     3 while count<3:
     4     age1=int(input('>>>:'))
     5     if age1==age:
     6         print('登陆成功')
     7         break
     8     else:
     9         print('登陆失败')
    10         count+=1
    
    

    14 :猜年龄游戏升级版 (###)

    1 要求:
    2     允许用户最多尝试3次
    3     每尝试3次后,如果还没猜对,就问用户是否还想继续玩,如果回答Y或y, 就继续让其猜3次,以此往复,如果回答N或n,就退出程序
    4     如何猜对了,就直接退出 
     1 方法一:
     2 age=18
     3 count=0
     4 while True:
     5     if count==3:
     6         age2 = (input('继续y/n>>>:'))
     7         if age2 == 'y' or age2 == 'Y':
     8             count = 0
     9         else:
    10              break
    11     age1=int(input('>>>:'))
    12     if age1==age:
    13         print('登陆成功')
    14         break
    15     count += 1
    16 方法二:
    17 count=1
    18 while True:
    19     if count ==3 :
    20         age=int(input('age:'))
    21         if age > 18:
    22             print('猜大了,可以重试3次,第 %s 次' %count)
    23         elif age < 18:
    24             print('猜小了,可以重试3次,第 %s 次' %count)
    25         else:
    26             print('猜中了,successful')
    27             break
    28         count += 1
    29     else:
    30         judge = input('是否继续(Y/N):')
    31         if judge in ['Y','y']:
    32             count = 1
    33         else:
    34             break

    四 作业练习题

    15.简述编译型与解释型语言的区别,且分别列出你知道的哪些语言属于编译型,哪些属于解释型? 

    1 答 :编译型相对于解释性而言,编译型的开发效率低,执行效率高。
    2     编译型:C、C++
    3     解释型:java、python、php

    16.执行 Python 脚本的两种方式是什么?

    1 答:交互式方式和文件方式

    17.Pyhton 单行注释和多行注释分别用什么?

    1 答:单行注释“#”,多行注释“ ''' ''' ”

    18.布尔值分别有什么?

    1  答:True、Fasle

    19.声明变量注意事项有那些?

    1  答:变量名只能是数字,字母,下划线的任意结合
    2       不能以数字开头,关键字不能做变量名

    20.如何查看变量在内存中的地址?

    1  答:例name='123',print(id'123')

    21.写代码 

    1  实现用户输入用户名和密码,当用户名为 seven 且 密码为 123 时,显示登陆成功,否则登陆失败!
    1 user=input('user:')
    2 password=input('password:')
    3 
    4 if user=='seven' and password=='123':
    5     print('登陆成功')
    6 else:
    7     print('登陆失败')
    2 实现用户输入用户名和密码,当用户名为 seven 且 密码为 123 时,显示登陆成功,否则登陆失败,失败时允许重复输入三次 
     1    i=0
     2     while i<3:
     3         user = input('user:')
     4         password = input('password:')
     5         if user=='seven' and password=='123':
     6             print('登陆成功')
     7             break
     8         else:
     9             print('登陆失败')
    10             i+=1
    3.实现用户输入用户名和密码,当用户名为 seven 或 alex 且 密码为 123 时,显示登陆成功,否则登陆失败,失败时允许重复输入三次
     1 i=0
     2 while i<3:
     3     user=input('user:')
     4     password=input('password:')
     5     if (user=='seven' or user=='alex') and password=='123':
     6         print('登陆成功')
     7         break
     8     else:
     9         print('登陆失败')
    10         i+=1

    22.写代码

    a. 使用while循环实现输出2-3+4-5+6...+100 的和(###)
    
    
    b. 使用 while 循环实现输出 1,2,3,4,5, 7,8,9, 11,12 使用 while 循环实现输出 1-100 内的所有奇数
    i=1
    while i<13:
        if i==10:
            pass
        else:
            print(i)
        i=i+1
    
    i=1
    while i<100:
        if i%2==0:
            pass
        else:
            print(i)
        i+=1

      

    e. 使用 while 循环实现输出 1-100 内的所有偶数 
    i=1
    while i<101:
        if i%2==1:
            pass
        else:
            print(i)
        i+=1

    23.现有如下两个变量,请简述 n1 和 n2 是什么关系?

    1 n1 = 123456
    2       n2 = n1
    3       答: n1='123456' #'123456'这个值的引用计数为1
    4          n2=n1 #'123456'这个值的引用计数为2
    5          有优化机制,n2和n1开辟了一个空间

    24.编写登录接口?

      基础需求:

         让用户输入用户名密码  认证成功后显示欢迎信息  输错三次后退出程序  流程图

     1 dic={
     2     'name':'egon','password':'123',
     3  }
     4 count=1
     5 tag=True
     6 while count<=3:
     7      name=input('>>>:')
     8      password=input('>>>:')
     9      if name==dic['name'] and password==dic['password']:
    10        print('欢迎光临')
    11        break
    12      else:
    13        print('输入错误,请重新输入,第 <%s> 次' %count)
    14        count+=1

    升级需求:

    • 可以支持多个用户登录 (提示,通过列表存多个账户信息)
    • 用户3次认证失败后,退出程序,再次启动程序尝试登录时,还是锁定状态(提示:需把用户锁定的状态存到文件里)
    
    
     

        



     
     

        
        
      

      

      
    
    
    

      

  • 相关阅读:
    C语言II博客作业04
    C语言II博客作业03
    C语言II博客作业01
    学期总结
    C语言I博客作业08
    C语言I博客作业07
    C语言I博客作业06
    C语言I博客作业05
    C语言I博客作业04
    C语言I博客作业03
  • 原文地址:https://www.cnblogs.com/yinfutao/p/7979407.html
Copyright © 2020-2023  润新知