Python(入门小练习1)

发布时间:2019-08-23 08:00:56编辑:auto阅读(1768)

    金融方面:比较价钱

    假设你购买大米时发现它有两种包装。你会别写一个程序比较这两种包装的价钱。程序提示用户输入每种包装的重量和价钱,然后显示价钱更好的那种包装。下面是个示例运行

    wKiom1l7RWzQQWa-AAClZh8hkY4144.png

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    # @Time : 2017\7\28 0028 18:52
    # @Author : d y h
    # @File : xiaolianxi.py
    #输入对比的重量与价格
    weight1,price1= eval(input("Enter weight  and price for package 1 :"))
    weight2,price2= eval(input("Enter weight  and price for package 2 :"))
    #每斤价格比较
    permj1 = price1 / weight1
    permj2 = price2 / weight2
    #选择价低的
    if permj1 > permj2:
        print("package 2  has the better price ")
    else:
        print("package 1  has the better price ")


    找出一个月中的天数

    编写程序提示用户输入月和年,然后显示这个月的天数。例如:用户输入2而年份为2000,这个程序应该显示2000年二月份有29天.如果用户输入月份3而年份为2005,这个程序应该显示2555年三月份有31天。

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    # @Time : 2017\7\28 0028 19:06
    # @Author : d y h
    # @File : xiaolianxi.py
    #输入月份与年份
    month,year = eval(input("Enter month an year (5,2004):"))
    #闰年
    leapyear =  year % 4
    if month == 1 or  month == 3 or month == 5 or month == 7 or month == 8  or \
                    month == 10 or  month == 12:
        month1 = 31
    elif month ==4 or  month == 6 or  month == 8  or  month ==9 or month ==11 :
        month1 =30
    elif leapyear == 0 and   month == 2:
        month1 = 29
    else:
        month = 28
    print(year,"",month,"","",month1,"")


    计算三角的周长

    编写程序读取三角形的三个边,如果输入是合法的则计算它的周长。否则显示这个输入是非法的。如果两边之和大于第三边则输入都是合法的。下面是一个示例运行。

    wKiom1l7RWywLMfHAAA3KfLaxvQ386.png

    wKioL1l7RWyAjMEIAAA6bppCXqc429.png

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    # @Time : 2017\7\28 0028 19:55
    # @Author : d y h
    # @File : xiaolianxi.py
    #输入三角形的三个边
    lenght1,lenght2,lenght3, =eval(input("Enter three adges:"))
    #算周长
    perimeter = lenght1 + lenght2 + lenght3
    #判断合不合法
    if   lenght1 + lenght2 > lenght3 and   lenght1 + lenght3 > lenght2 and \
        lenght2 + lenght3 > lenght1:
        print("The perimeter is",perimeter)
    else:
        print("The perimeter invalid",)


    Turtle:点在矩形内吗?

    行编写一个程序提示用户输入点(x,y)。然后检测点是否在以(0.0)为中心、宽为100、高为50的矩形内。在屏幕上显示则个点、矩形以及表明这个点是否在矩形内的消息如,图所示



    wKiom1l7RW2Se4pAAADyKdnMIqg481.png

    程序显示矩形,点以及表明这个点是否在矩形内的消息

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    # @Time : 2017\7\28 0028 21:39
    # @Author : d y h
    # @File : zuoye.4.36.py
    #输入一个点的坐标
    x,y =  eval(input("Enter a coordubate :"))
    import turtle
    #画正方形
    turtle.penup()
    turtle.goto(-50,25)
    turtle.pendown()
    turtle.goto(50,25)
    turtle.goto(50,-25)
    turtle.goto(-50,-25)
    turtle.goto(-50,25)
    turtle.hideturtle()

    #设置用户输入的point
    turtle.penup()
    turtle.goto(x,y)
    #设置颜色
    turtle.color("blue")
    #笔尖大小
    turtle.pensize(10)
    turtle.pendown()
    turtle.goto(x,y)
    turtle.penup()
    turtle.goto(-100,-100)

    #x1=用户输入的坐标x绝对值   y1=用户输入的坐标y的绝对值
    x1 = abs(x)
    y1 = abs(y)

    #如果x的绝对值小于50并且y的绝对值小于25,则该点在图形内。否则不在图形内
    if x1 <= 50 and y1 <= 25:
        turtle.write("This point is in the graph",font=("Arial",20,"normal"))
    else:
        turtle.write("This point is not in the graph",font=("Arial",20,"normal"))
    turtle.done()

     


关键字