zoukankan      html  css  js  c++  java
  • 条件、循环、函数定义等练习

    
    
    

    对前面的代码进行优化,用for,while,if,def实现:

    1. 用循环画五角星
    2. 用循环画同心圆
    3. 用while循环画太阳花
    4. 用函数定义画五个五角星
    5. 用函数定义画钻石花瓣的太阳花

    字符串操作

    1. 输入学号,识别年级、专业、序号。
    2. 输入1-7的数字,输出对应的“星期几”。
    3. 识别身份证号中的省市区、年龄、性别。

      用循环画五角星

    import turtle
    turtle.fillcolor('yellow')
    turtle.begin_fill()
    for i in range (5):
        turtle.forward(200)
        turtle.right(144)
    turtle.end_fill()

    用循环画同心圆

    import turtle
    for i in range(5):
        turtle.up()
        turtle.goto(0,-20*i)
        turtle.down()
        turtle.circle(20*i)

    用while循环画太阳花

    from turtle import *
    color('red','yellow')
    begin_fill()
    while True:
        forward(200)
        left(170)
        if abs(pos())<1:
            break
    end_fill()
    done()



    用函数定义画五个五角星

    import turtle
    
    turtle.setup(600,400,0,0)
    turtle.bgcolor("red")
    turtle.color("yellow")
    turtle.fillcolor("yellow")
    
    
    
    #def start_star(x,y):
        turtle.up()
        turtle.goto(x,y)
        turtle.down()
    
    #def star_more(r):
        turtle.begin_fill()
        for i in range(5): 
            turtle.forward(r)
            turtle.right(144)
        turtle.end_fill()
        
    start_star(-260,140)
    star_more(100)
    
    start_star(-120,180)
    star_more(25)
    
    start_star(-100,140)
    star_more(25)
    
    start_star(-100,100)
    star_more(25)
    
    start_star(-120,60)
    star_more(25)

     用函数定义画钻石花瓣的太阳花

    import turtle
    
    turtle.color("blue")
    
    for i in range(35):
        turtle.right(12)
        for i in range(2):
            turtle.forward(150)
            turtle.right(30)
            turtle.forward(100)
            turtle.right(150)

     输入学号,识别年级、专业、序号。

    idnumber=input("请输入学号:")
    print("你的年级为{}级".format(idnumber[0:4]))
    print("你的专业编号为{}".format(idnumber[4:8]))
    print("你的序号为{}".format(idnumber[8:12]))

     输入1-7的数字,输出对应的“星期几”。

    number=input("请输入星期几:")
    string="星期一星期二星期三星期四星期五星期六星期日"
    if number == '1':
        print("输入的是{}".format(string[0:3]))
    elif number=='2':
        print("输入的是{}".format(string[3:6]))
    elif number=='3':
        print("输入的是{}".format(string[6:9]))
    elif number=='4':
        print("输入的是{}".format(string[9:12]))
    elif number=='5':
        print("输入的是{}".format(string[12:15]))
    elif number=='6':
        print("输入的是{}".format(string[15:18]))
    elif number=='7':
        print("输入的是{}".format(string[18:21]))

    识别身份证号中的省市区、年龄、性别。

    (只以广东为例)

    idnumber=input("请输入身份证号:")
    if idnumber[0:2] == '44' :
        print("本人身份为广东户籍")
    else:
        print("本人身份非广东户籍")
    if number == '1':
    year=2017-int(idnumber[6:10])
    print("你的年龄为"+year+"")  
    if int(idnumber[-2,-3])%2==0:
      print('性别:女')
    else:
      print('性别:男')
  • 相关阅读:
    c++基础_矩阵乘法
    c++基础_字符串对比
    c++基础_时间转换
    c++基础_特殊回文数
    c++基础_回文数
    c++基础_特殊的数字
    c++基础_杨辉三角形
    c++基础_字母图形
    c++基础_01字串
    java 常用集合类型--以及其特性
  • 原文地址:https://www.cnblogs.com/murasame/p/7513505.html
Copyright © 2011-2022 走看看