赞
踩
实验四 分支结构
- x=eval(input("边长一"))
- y=eval(input("边长二"))
- z=eval(input("边长三"))
- #代码开始
- #代码结束
- l1 = [x,y,z]
- a,b,c = sorted(l1)
- if a+b<c:
- print('无法构成三角形')
- else:
- if a==b==c:
- print('等边三角形')
- elif a**2+b**2 == c**2:
- print('直角三角形')
- else:
- print('普通三角形')
先比较再判断
- x=eval(input())
- #代码开始
- y = 0
- if x<1:
- y = x
- elif 1<=x<10:
- y = 2*x - 1
- else:
- y = 3*x - 1
- #代码结束
- print("{:.2f}".format(y))
- sy=eval(input("输入上月电表度数"))
- by=eval(input("输入本月电表度数"))
- yf=eval(input("输入月份"))
- #代码开始
- ds = by - sy
- df1 = 200*0.588
- if ds<200:
- df = 0.588*ds
- else:
- if yf in [3,4,5,9,10,11]:
- if ds<=350:
- df = df1 + (ds - 200)*0.638
- else:
- df = df1 + 150*0.638 + (ds-350)*0.888
- else:
- if ds <= 450:
- df = df1 + (ds - 200)*0.638
- else:
- df = df1 + 250*0.638 + (ds - 450)*0.888
- #代码结束
- print("用电度数{:.2f}电费{:.2f}".format(ds,df))
这种是期中考试大题(必考?),好好熟悉。
- sy=eval(input("输入上月水表吨数"))
- by=eval(input("输入本月水表吨数"))
- rs=eval(input("输入家庭人数"))
- db=input("是否低保(y/n)")
- #代码开始
- userge = by - sy
- if db =='y':
- sl = userge - 6
- else:
- sl = userge
- if rs < 5:
- if sl <= 15 :
- sf = 2.58 * sl
- elif 15 < sl <= 22:
- sf = 3.34*(sl-15)+2.58*15
- else:
- sf = 2.58*15+3.34*7+(sl-22)*4.09
- else:
- if sl/rs <= 4:
- sf = 2.58*sl
- elif 4 < sl/rs <= 5:
- sf = 3.34*(sl - rs*4)+4*rs*2.58
- else:
- sf = 4.09*(sl - rs*5)+rs*3.34+2.58*rs*4
- if sf<0:
- sf = 0
- print("用水吨数(减免后){:.2f}水费{:.2f}".format(sl,sf))
注意考虑减免太多,水费为负数,难道还有倒贴给你?QAQ
- ll=eval(input("流量"))
- thsj=eval(input("通话时间"))
- #代码开始
- #代码结束
- cost = 128
- ct_cost = (thsj-500)*0.15
- if ll<33:
- md_cost = (ll-30)*5.00
- else:
- md_cost = 15 + (ll-33)*3.00
- if ct_cost>0:
- cost += ct_cost
- if md_cost > 0:
- cost += md_cost
- print("费用"+'{:.2f}'.format(cost))
大家如果碰到给你推销这种套餐的人,果断拉黑,想把你当猪宰。
第6关:血压判断
- x=eval(input())
- y=eval(input())
- #代码开始
- if x>=90 or y>=140:
- print('高血压')
- elif x<60 or y<90:
- print('低血压')
- else:
- print('正常血压')
- #代码结束
大学生也要好好锻炼,我就很喜欢学校的乐跑:(
- ch=input("")
- s=eval(input(""))
- rate = 0
- if ch=='n' or ch=='N':
- if s<=3000:
- rate = 0.005
- elif 3000<s<=5000:
- rate = 0.01
- elif 5000<s<=10000:
- rate = 0.015
- else:
- rate = 0.02
- else:
- if s<=5000:
- rate = 0.005
- else:
- rate = 0.01
- df = s*rate
-
- #代码开始
- #代码结束
- print("{:.2f}".format(df))
第8关 周薪计算
- gzbz=eval(input("工资标准"))
- gzr=eval(input("工作日工作时间"))
- xxr=eval(input("休息日工作时间"))
- if xxr<=4:
- xxrgz = xxr*2*gzbz
- else:
- xxrgz = 2*4*gzbz+(xxr-4)*gzbz*3
- if gzr<=40:
- gzrgz = gzr * gzbz
- else:
- gzrgz = 40*gzbz+(gzr-40)*1.5*gzbz
- gz = xxrgz + gzrgz
- #代码开始
- #代码结束
- print("工资{:.2f}".format(gz))
总结:
多多动手,熟悉阶梯计价,考完期中你就知道了。
实验3-1-程序控制-选择结构
第1关:关系运算符与单分支选择语句
- #######Begin your code here#######
- n = int(input())
- if n>0:
- print('正数\n')
- #######End your code here#######
- light=input() #从测试集读入light值,此部分代码不用修改
-
- ######begin 从此处向下,开始你的编码,根据ligth的值进行输出###
-
- light = '关' if light == '开' else '开'
- print(light)
-
-
-
- #####end###
第3关:求解一元二次方程组
- import math
- a=float(input())
- b=float(input())
- c=float(input())
-
- def quadratic_equation(a, b, c):
- discriminant = b ** 2 - 4.0 * a * c
-
- if discriminant < 0:
- sb = -b/2/a
- xb1 = math.sqrt(- b ** 2 + 4.0 * a * c) / (2 * a)
- xb2 = -math.sqrt(- b ** 2 + 4.0 * a * c) / (2 * a)
- x1 = complex(sb,xb1)
- x2 = complex(sb, xb2)
- return x1,x2
- elif discriminant == 0:
- x = -b / (2 * a)
- return x
- else:
- if a != 0:
- x1 = (-b + math.sqrt(discriminant)) / (2 * a)
- x2 = (-b - math.sqrt(discriminant)) / (2 * a)
- return x1, x2
- else:
- x = -c/b
- return x
- roots = quadratic_equation(a,b,c)
- if type(roots)==float:
- print("x=", roots)
- elif type(roots)==tuple:
- x1,x2 = roots
- print("x1=",x1,"x2=",x2)
- else:
- print("x1=", x1, "x2=", x2)
这个有点小难度了,需要考虑的情况有点多,多写两遍吧!(可以不定义函数)
- score=float(input()) #读入测试变量score
-
-
- #**********begin*******以下编写代码实现
- if score >= 90 and score <= 100:
- print("等级A")
- elif score >= 80 and score < 90:
- print("等级B")
- elif score >= 70 and score < 80:
- print("等级C")
- elif score >= 60 and score < 70:
- print("等级D")
- else:
- print("等级E")
- #**********end***********
第5关:3个数按从大到小依次输出
- #######Begin your code here#######
- l1 = input().split()
- l2 = [eval(num) for num in l1]
- num3,num2,num1 = sorted(l2)
- print(num1, num2, num3)
- #######End your code here#########
第6关:水仙花数与四叶玫瑰数
- #######Begin your code here#######
- num = int(input())
- if num in[153,370,371,407]:
- print('narcissistic')
- elif num in [1634,8208,9474]:
- print('rose')
- else:
- print('neither')
- #######End your code here#########
偷懒了,其实和前面回文数核心一样的,懒得写啦QAQ
第二章-Python语言基础-2.2条件分支(理科)
-
- # -*- coding: utf-8 -*-
- """
- Created on Tue Sep 8 06:29:54 2020
- @author: hzh
- """
-
- # 第1题:
- year = int(input())
- # 如果是闰年输出yes
- ####### begin #######
- if year%4 == 0 or year%400 == 0:
- print('yes')
- else:
- print('no')
- ####### end #########
- # 如果不是闰年,输出no
- ####### begin #######
-
- ####### end #########
-
-
- # 第2题:
- month = int(input())
- # 30天的月份:4,6,9,11
- # 如果是30天的月份输出yes
- ####### begin #######
- if month in [4,6,9,11]:
- print('yes')
- else:
- print('no')
- ####### end #########
- # 如果不是30天的月份,输出no
- ####### begin #######
-
- ####### end #########
-
-
- # 第3题:
- # 从键盘输入成绩,等级默认为0级
- scores = int(input())
- grade = '0'
- # 如果成绩>=90分的输出'A'
- ####### begin #######
- if scores>=90:
- grade = 'A'
- ####### end #########
- # 否则成绩60-89分之间的用B表示
- if 60<=scores<=89:
- grade = 'B'
- ####### end #########
- # 60分以下的用C表示
- if scores<60:
- grade = 'C'
- ####### end #########
- print(grade)
-
- # 第4题:
- # 从键盘输入风速
- v = int(input())
- # 默认是0级
- rank = 0
- # 如果风速在74到95之间,输出1
- ####### begin #######
- if 74<=v<=95:
- rank = 1
- elif 96<=v<=110:
- rank = 2
- elif 111<=v<=130:
- rank = 3
- elif 131<=v<=154:
- rank = 4
- elif 155<=v:
- rank = 5
-
- print(rank,end='')
-
我还以为点错题目了
- def print_(x):
- if type(x) == float:
- print("%.4f" % x)
- else:
- print(x)
-
- import math
- # 第1题
- # 数据来源于列表
- for temp in [-280, -100, 0, 20, 120, 200]:
- # 请在下面编写代码
- # ********** Begin ********** #
- if temp>-273.5:
- F = 9/5*temp+32
- else:
- F = None
- # ********** End ********** #
- # 请不要修改下面的代码
- print_(F)
-
- print('\n***********************\n')
-
- # 第2题
- # 数据来源于列表
- for number in [-101.23, -3.14, 0, 12.55, 1908]:
- # 请在下面编写代码
- # ********** Begin ********** #
- number = number if number >= 0 else -number
- # ********** End ********** #
- # 请不要修改下面的代码
- print_(number)
-
- print('\n***********************\n')
-
- # 第3题
- # 数据来源于列表
- for (num1, num2) in [(12, 34), (-6, -7), (-12, 23), (-273, 0), (0, 199)]:
- # 请在下面编写代码
- # ********** Begin ********** #
- a = min(num2,num1)
- b = max(num2,num1)
- # ********** End ********** #
- # 请不要修改下面的代码
- print(a,' ',b)
-
- print('\n***********************\n')
-
- # 第4题
- # 数据来源于列表
- for (num1, num2, num3) in [(-231, -321, 123), (100, 0, -99), (-980, -1002, -1), (6, 1, 2017)]:
- # 请在下面编写代码
- # ********** Begin ********** #
- l = sorted([num1,num2,num3])
- num1 = l[0]
- num2 = l[1]
- num3 = l[2]
- # ********** End ********** #
- # 请不要修改下面的代码
- print(num1, num2, num3)
-
- print('\n***********************\n')
-
- # 第5题
- # 数据来源于列表
- for (num1, num2) in [(10, 5), (12, 34), (89, 0), (-100, 23), (789, -123)]:
- # 请在下面编写代码
- # ********** Begin ********** #
- if num2 != 0:
- result = num1/num2
- else:
- result = None
- # ********** End ********** #
- # 请不要修改下面的代码
- print_(result)
-
- print('\n***********************\n')
-
- # 第6题
- # 数据来源于列表
- def quadratic_equation(a, b, c):
- discriminant = b ** 2 - 4 * a * c
-
- if discriminant < 0:
- return None
- elif discriminant == 0:
- x = -b / (2 * a)
- return x
- else:
- x1 = (-b + math.sqrt(discriminant)) / (2 * a)
- x2 = (-b - math.sqrt(discriminant)) / (2 * a)
- return x1, x2
-
-
- for (a, b, c) in [(1, 2, 1), (1, -2, 1), (1, 2, -3), (12, 34, 56), (78, 89, 100)]:
- roots = quadratic_equation(a, b, c)
- if roots is None:
- print(None,None)
- elif isinstance(roots, float):
- print(roots, roots)
- else:
- x1, x2 = roots
- print(x1,x2)
- # else:
- # print("方程的实根为:", roots)
- # ********** End ********** #
- # 请不要修改下面的代码
-
- print('\n***********************\n')
-
- # 第7题
- # 数据来源于列表
- for x in [-9, -8, -7, -6, -5, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8]:
- # 请在下面编写代码
- # ********** Begin ********** #
- if -10 <= x< -8:
- fx = x-2
- elif -8 <= x < -6:
- fx = x+3
- elif -6 <= x <= -2:
- fx = x**2
- elif -2 < x < 2:
- fx = x if x >= 0 else -x
- elif 2 <= x <= 4:
- fx = x**3
- elif 4 < x <= 6:
- fx = 3*x-4
- elif 6 < x <= 8:
- fx = 4*x+1
- # ********** End ********** #
- # 请不要修改下面的代码
- print_(fx)
- # 第一题
- # 数据来源列表,每组数据输出一个结果
- for (Weight, Height) in [(60, 1.65), (40, 1.60), (80, 1.70), (75, 1.83), (88, 1.80)]:
- bmi = float(Weight / pow(Height, 2))
- wto, dom = " ", " "
- # 请在下面编写代码
- # wto, dom的结果为字符串类型的"肥胖","偏瘦","偏胖","正常"
- # ********** Begin ********** #
- bmi1,bmi2 = bmi,bmi
- if bmi<18.5:
- wto, dom = "偏瘦", "偏瘦"
- elif 18.5<bmi1<=25:
- wto = '正常'
- elif 25< bmi1 <=30:
- wto = '偏胖'
- elif 30<= bmi1:
- wto = '肥胖'
- if 18.5 < bmi2 <= 24:
- dom = '正常'
- elif 24 < bmi2 <= 28:
- dom = '偏胖'
- elif 28 <= bmi2:
- dom = '肥胖'
- # ********** End ********** #
- # 请不要修改下面的代码
- print("BMI 指标为:国际'{0}', 国内'{1}'".format(wto, dom))
- #print(bmi1,bmi2)
- print('\n******************************\n')
-
- # 第二题
- # 数据来源列表,每组数据输出一个结果
- for salary in [-1000, 0, 40000, 47450, 98000, 114650, 14980, 17470, 25670, 311950, 360000]:
- # 请在下面编写代码
- # ********** Begin ********** #
- if 0<salary<=47449:
- salaTax = salary*0.22
- elif 47450<=salary<=114649:
- salaTax = salary * 0.25
- elif 114650<=salary<=174699:
- salaTax = salary * 0.28
- elif 174700<=salary<=311949:
- salaTax = salary * 0.33
- elif 311950<=salary:
- salaTax = salary * 0.35
- else:
- salaTax = 0
-
- # ********** End ********** #
- # 请不要修改下面的代码
- print("%.4f" % salaTax) # 输出个人所得税salaTax
- print('\n***********************\n')
-
- # 第三题
- # 数据来源列表,每组数据输出一个结果
- for (p1, p2) in [('s', 'j'), ('b', 'j'), ('j', 'j'), ('b', 's')]:
- # 请在下面编写代码
- # p1,p2代表玩家1,玩家2,'s','j','b' 分别代表石头、剪刀和布
- # game=-1,0,1分别代表p1输局,平局,赢局
- # ********** Begin ********** #
- if p1 == p2:
- game = 0
- elif p1 == 's' and p2 == 'j':
- game = 1
- elif p1 == 'j' and p2 == 'b':
- game = 1
- elif p1 == 'b' and p2 == 's':
- game = 1
- else:
- game = -1
- # ********** End ********** #
- # 请不要修改下面的代码
- print(game)
- print('\n***********************\n')
总结:
很多题目都是思路简单,写起来麻烦,感觉只能用来练习打字》-《
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。