前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python(入门小练习1)

Python(入门小练习1)

作者头像
py3study
发布2020-01-09 10:34:54
9370
发布2020-01-09 10:34:54
举报
文章被收录于专栏:python3python3

金融方面:比较价钱

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

wKiom1l7RWzQQWa-AAClZh8hkY4144.png
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
wKiom1l7RWywLMfHAAA3KfLaxvQ386.png
wKioL1l7RWyAjMEIAAA6bppCXqc429.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
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()

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-08-23 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档