Demo19

你可以通过询问5个问题来找出你朋友的生日在一个月中的哪一天。每个问题都在询问 这一天是否在5个数字集中。
生日就是出现这个数字的集合的第一t 个数字的和,例如:如果生日是19,那它就会在
setl. set2和set5中出现。这三个集合的第一个数字分别是1. 2、16。它们加起来的和就
是19。


set1 = "1\t3\t5\t7\n" \"9\t11\t13\t15\n" \"17\t19\t21\t23\n" \"25\t27\t29\t31\n"
set2 = "2\t3\t6\t7\n" \"10\t11\t14\t15\n" \"18\t19\t22\t23\n" \"26\t27\t30\t31\n"
set3 = "4\t5\t6\t7\n" \"12\t13\t14\t15\n" \"20\t21\t22\t23\n" \"28\t29\t30\t31\n"
set4 = "8\t9\t10\t11\n" \"12\t13\t14\t15\n" \"24\t25\t26\t27\n" \"28\t29\t30\t31\n"
set5 = "16\t17\t18\t19\n" \"20\t21\t22\t23\n" \"24\t25\t26\t27\n" \"28\t29\t30\t31\n"
day = 0print(set1)
choice = input("你的生日在不在以上数字当中(y/n):")
if choice == "y" or choice == "Y" or choice == "yes" or choice =="YES" or choice =="yES":day += 1print(set2)
choice = input("你的生日在不在以上数字当中(y/n):")
if choice == "y" or choice == "Y" or choice == "yes" or choice =="YES" or choice =="yES":day += 2print(set3)
choice = input("你的生日在不在以上数字当中(y/n):")
if choice == "y" or choice == "Y" or choice == "yes" or choice =="YES" or choice =="yES":day += 4print(set4)
choice = input("你的生日在不在以上数字当中(y/n):")
if choice == "y" or choice == "Y" or choice == "yes" or choice =="YES" or choice =="yES":day += 8print(set5)
choice = input("你的生日在不在以上数字当中(y/n):")
if choice == "y" or choice == "Y" or choice == "yes" or choice =="YES" or choice =="yES":day += 16if day == 0 :print("玩我呢?")
else :print("哈哈,你的生日在%d号!"%day)

Demo20

BMI是根据体重测量健康的方式。通过以千克为单位的体重除以以米为单位的身高的 平方计算出BMI。下面是16岁以上人群的BMI图表:
编写一个程序,提示用户输人以磅为单位的体重和以英寸为单位的身高,然后显示BMI值。注意: 1 磅是
0.453 592 37千克而1英寸是0.0254米。程序清单4-6给出这个程序。

weight = float(input("请输入您的体重(以千克为单位):"))
height = float(input("请输入您的身高(以米为单位):"))
BIM = weight / height ** 2
if BIM < 18.5:print("超轻")
elif 18.5 <= BIM < 25.0:print("标准")
elif 25.0 <= BIM < 30.0:print("超重")
elif 30 <= BIM:print("肥胖")
else:print("您输入错误的值")

Demo21

year = int(input("请输入年份:"))
condition1 = year % 4 == 0 and year % 100 != 0
condition2 = year % 400 == 0
if condition1 or condition2:print(year, "是个闰年")
else:print(year, "是个平年")

Demo22
假设你想开发一-个玩彩 票的程序。程序随机产生一个两位数的数字,然后提示用户输入 一个两位数的数字,并根据以下规则判定用户是否赢得奖金。 1)如果用户输人的数字和随机产生的数字完全相同(包括顺序),则奖金为10 000美元。 2)如果用户输人的数字和随机产生的数字相同(不包括顺序),则奖金为3000美元。 3)如果用户输人的数字和随机产生的数字有一位数相同,则奖金为1000美元。

import random
com = random.randint(10,99)
usr = int(input("请输入数字:"))
ca = com // 10
cb = com % 10
ua = usr // 10
ub = usr % 10
if ca == ua and cb == ub :print("一等奖!")
elif ca == ub and cb == ua :print("二等奖")
elif ca == ua or ca == ub or cb == ua or cb == ub :print("三等奖")
else :print("谢谢参与!")

Demo23.
(代数方面:解一元二次方程)例如: ax**2+ bx+c= 0的平方根可以使用下面的公式获取。

a, b, c = eval(input("Enter a,b,c:"))
delt = b ** 2 - 4 * a * c
if delt > 0:x1 = (-b + delt ** 0.5) / (2 * a)x2 = (-b - delt ** 0.5) / (2 * a)print("x1 = %.2f , x2 = %.2f" % (x1, x2))
elif delt == 0:x = (-b - delt ** 0.5) / (2 * a)print("x = %.2f" % x)
else:print("No real roots")

Demo24

(代数:解2x2线性方程)你可以使用克菜姆法则解下面的线性方程2x2系统: …编写程序,提示用户输人a、b. c. d、e和f,然后显示结果。如果ad-be为零,呈现“The equation has no solution"。

a, b, c, d, e, f = eval(input("Enter a,b,c,d,e,f:"))
if a * d - b * c == 0:print("The equation has no solution")
else:x = (e * d - b * f) / (a * d - b * c)y = (a * f - e * c) / (a * d - b * c)print("x is %.1f and y is %.1f" %(x,y))

Demo25

(找未来数据)编写程序提示用户输入表示今天是一周内哪一天的数字(星期天是0,星期一是 1, …,星期六是6)。还要提示用户输入今天之后到未来某天的天数,然后显示未来这天是星期 几。下面是一个示例运行。

today = int(input("输入今天的日期,例如(星期天是0,星期一是1, ....,星期六是6):"))
a = int(input("请输入要经过多少天:"))
if (today + a) % 7 == 0:print("Today is Sunday and the future day is 星期天")
elif (today + a) % 7 == 1:print("Today is Sunday and the future day is 星期一")
elif (today + a) % 7 == 2:print("Today is Sunday and the future day is 星期二")
elif (today + a) % 7 == 3:print("Today is Sunday and the future day is 星期三")
elif (today + a) % 7 == 4:print("Today is Sunday and the future day is 星期四")
elif (today + a) % 7 == 5:print("Today is Sunday and the future day is 星期五")
elif (today + a) % 7 == 6:print("Today is Sunday and the future day is 星期六")

Demo26

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

a1, a2 = eval(input("请输入第一种所购产品的重量和价格:") )
b1, b2 = eval(input("请输入第二种所购产品的重量和价格:") )
if (a2 / a1) > (b2 /b1):print("第一种产品和第二种产品相比之下,第二种产品更好")
elif (a2 / a1) == (b2 /b1):print("第一种产品和第二种产品相比之下,两种产品一样好")
elif (a2 / a1) < (b2 /b1):print("第一种产品和第二种产品相比之下,第一种产品更好")

Demo27

(检测一个数字)编写一个程序提示用户输入一个整数,然后检测该数字是否能被5和6都整除、 能被5或6整除还是只被它们中的一个整除(但又不能被它们同时整除)。下面是一个示例运行。

a = int(input("请输入一个整数:") )
if a % 5 == 0 and a % 6 == 0:print("%d这个数能被5和6都整除" % a)
elif a % 5 != 0 and (a % 6 == 0):print("%d这个数能被6整除,但不能被5整除" % a)
elif (a % 5 == 0) and (a % 6 != 0):print("%d这个数能被5整除,但不能被6整除" % a)
elif (a % 5 != 0) and (a % 6 != 0):print("%d这个数不能被5或者6整除" % a)

Demo28

(游戏:剪刀、石头、布)编写程序来玩流行的剪刀-石头-布的游戏。(剪刀可以剪纸,石头可 以磕碰剪刀,而布可以包裹石头。)程序随机产生一个数字0、1或2来表示剪刀、石头和布。程 序提示用户输人数字0、1或2然后显示一条消息表示用户或计算机是赢、输还是平局。下面是. 一个示例运行。

import randomcom = random.randint(0, 2)
usr = int(input("剪刀(0),石头(1),布(2):"))com_str = ""
usr_str = ""
if com == 0:com_str = "剪刀"
elif com == 1:com_str = "石头"
else:com_str = "布"if usr == 0:usr_str = "剪刀"
elif usr == 1:usr_str = "石头"
else:usr_str = "布"if com == usr:print("玩家是%s,电脑是%s,是一个平局" % (usr_str, com_str))
elif usr - com == -1 or usr - com == 2:print("玩家是%s,电脑是%s,玩家输" % (usr_str, com_str))
else:print("玩家是%s,电脑是%s,玩家赢" % (usr_str, com_str))

Demo29

(金融问题:货币对换)编写一个程序提示用户输人美元和人民币之间的货币汇率。提示用户输入0表示将美元转换为人民币而1表示将人民币转换为美元。提示用户输人美元数或人民币数将它分别转换为人民币或美元。下面是一些示例运行。

rate = eval(input("Enter the exchange rate from do1lars to RMB: "))
choice = eval(input("Enter 0 to convert do11ars to RMB and 1 vice versa: "))
amount = eval(input("Enter the do1lar amount: "))
if choice == 0:amount1 = amount * rateprint("$%.1f is %.1f yuan"%(amount,amount1))
elif choice == 1:amount1 = amount / rateprint("%.1f yuan is $%.2f"%(amount, amount1))
else :print("Incorrect input")

Demo30

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

a, b, c = eval(input("Enter three sides:"))
if a + b > c and a + c > b and b + c > a:print(a+b+c,"合法")
else :print("不合法")

Demo31

(科学问题: -周的星期几)泽勒的一致性是- -个由泽勒开发的算法,用于计算-周的星期儿。 这个公式是
●这里的h是指一間的星期几(0:星期六; 1:星期天; 2:星期一; 3:星期二; 4:星期三; 5:
星期四: 6;星期五)。
●q是一个月的哪一天。
●m是月份(3:三月; 4:四月; .; 12:十二月)。一月和二月都是按照前一年的13月和14.
月来计数的。
●j是世纪数(即year/100)
●k是一个世纪的某- -年(即year % 100)。
编写程序提示用户输人一个年份、月份以及这个月的某天,然后它会显示它是一周的星期
几。下面是一些示例运行。

import math
year = eval(input("Enter year:(e,g.,2008):"))
month = eval(input("Enter month:1-12:"))
q = eval(input("Enter the day of the month:1-31:"))
m = 0
if month == 1 or month == 2:m = 12 + monthyear = year - 1
j = year // 100
k = year % 100
h = (q + ((26 * (m + 1)) // 10) + k + k // 4 + j // 4 + 5 * j) % 7
if h == 0:print("Today is Sunday and the future day is 星期六")
elif h == 1:print("Today is Sunday and the future day is 星期天")
elif h == 2:print("Today is Sunday and the future day is 星期一")
elif h == 3:print("Today is Sunday and the future day is 星期二")
elif h == 4:print("Today is Sunday and the future day is 星期三")
elif h == 5:print("Today is Sunday and the future day is 星期四")
elif h == 6:print("Today is Sunday and the future day is 星期五")

Demo32

(几何问题:点在圆内吗? )编写一个程序提示用户输入一个点(x.,),然后检测这个点是否在圆 心为(0,0)半径为10的圆内。例如:点(4,5)在圆内而(9,9)在圆外,如图4-8a所示。

x, y =eval((input("Enter a point with two coordinates:")))
if x <= 10 and y <= 10:print("Point (%.1f,%.1f) is in the circle"%(x,y))
else:print("Point (%.1f,%.1f) is not in the circle"%(x,y))

Demo33

(几何问题:点在矩形内吗? )编写程序提示用户输入点(x.J),然后检测这个点是否在以(0,0)为 中心而宽为10高为5的矩形内。例如: (2,2) 在矩形内而(6,4)在矩形外,如图4-8b所示。(提 示:如果一个点到(0,0)的水平距离小于或等于10/2 而到(0,0)的垂直距离小于或等于5.0/2。测 试你的程序覆盖所有的情况。)下面是两个示例运行。

x, y = eval(input("Enter a point with two coordinates:"))
if x >= -5 and x <= 5 and y <= 2.5 and y >= -2.5:print("Point (%.1f,%.1f) is in the rectangle"%(x,y))
else :print("Point (%.1f,%.1f) is not in the rectangle" % (x, y))

Demo34

(回文数)编写程序提示用户输入一个三位整数,然后决定它是否是-一个回文数。如果一个数从 左向右和从右向左读取时是一-样的,那么这个数就是回文数。下面是 这个程序的示例运行。

number = int(input("Enter a three-digit integer:"))
a = number % 10
b = number // 100
if a == b:print("%d is a palindrome" %number)
else :print("%d not is a palindrome" %number)

Demo35

(几何问题:点在三角形内吗? )假设一个直角三角形被放在个水平面上,如下图所示。直角点是在(0,0)而另外两个点在(200,0)和(0,100)处。编写程序提示用户输入一个带x坐标和y坐标的点,然后决定这个点是否在三角形内。下面是一些示例运行。

x, y = eval(input("Enter a point's x- and y-coordinates:"))
# 先限定xy的范围
if x >= 0 and x <= 200 and y >= 0 and y <= 100:k = y / (200 - x)if k <= 100 / 200:print("The point is in the triangle")else:print("The point is not in the triangle")
else:print("The point is not in the triangle")

Demo36

(几何问题:两个圆)编写程序提示用户输人两个圆的中心的坐标以及它们的半径,然后判断第二个圆是在第一-个圆内还是和第一个圆有重叠部分, 如图4-11所示。(提示:如果两个中心的距 离≤|rl - r2|那么circle2 在circlel内,如果两个中心的距离≤rl + r2那么circle2是和circlel有重叠的。测试你的程序覆盖所有的情况。)


x1, y1, r1= eval(input("Enter circlel's_ center x1,y1coordinates,and radius:"))
x2, y2, r2= eval(input("Enter circlel's_ center x2,y2coordinates,and radius:"))
d = ((x2 - x1) ** 2 + (y2 - y1) ** 2) ** 0.5if d <= abs(r1 - r2):print("circle2 is inside circle1")
elif  d <= r1 + r2:print("circle2 overlaps circle1")
else:print("circle2 does not overlap circle")

Python练习题 判断语句(19~36)相关推荐

  1. Python条件判断语句详解:if、else、switch都有了

    导读:条件语句是指根据条件表达式的不同计算结果,使程序流转至不同的代码块.Python中的条件语句有--if语句.if- else-语句. 作者:黄传禄 张克强 赵越 来源:大数据DT(ID:hzda ...

  2. Python:判断语句

    目录 一.布尔类型 1.1定义 1.2获取 二.逻辑运算符 2.1and运算符 2.2or运算符 2.2not运算符 三.if判断语句 3.1基本格式 3.2if-else 格式 3.3if-elif ...

  3. python基本判断语句_python基础4 - 判断(if)语句

    6. 判断(if)语句 6.1 if 判断语句基本语法 在 Python 中,if语句 就是用来进行判断的,格式如下: if 要判断的条件: 条件成立时,要做的事情 -- 注意:代码的缩进为一个 ta ...

  4. python基本判断语句_python两种简洁的条件判断语句写法

    了Python返回真假值(True or False)小技巧,本文探讨的是最简洁的条件判断语句写法,本文给出了两种简洁写法,需要的朋友可以参考下 如下一段代码:def isLen(strString) ...

  5. Python的判断语句

    比较 在Python中比较语句和其它的一样都是用if来做判断的,只是语法上后面会带上冒号,如if a>b:,相当于if(a>b){}. 同样你可以判断函数的返回值为True或者False来 ...

  6. python中判断语句怎么写_python中判断语句

    Python程序语言指定任何非0和非空(null)值为true,0 或者 null为false. Python 编程中 if 语句用于控制程序的执行,基本形式为: if 判断条件: 执行语句-- el ...

  7. python的判断语句练习

    练习案例1:if练习 成年人判断 练习input输入语句,完成案例: 1.通过input语句,获取键盘输入,为变量age赋值(注意转换成数字类型) 2.通过if判断是否是成年人,满足条件则输出提示信息 ...

  8. python分支判断语句_python 分支语句 等值判断 逻辑运算符

    # 分支语句 age = 233 if age < 18: print('您还未满18岁,禁止入内') elif age > 18 and age < 60: print(" ...

  9. python if语句能否判断中文,Python之判断语句(if语句)

    if与elif及else一起使用,用来判断条件 使用语句时,缩进不一致会导致出错:空格和tab不能混用 条件语句 说明 if 假如 elif 又或 else 否则 pass 空语句,保持结构完整 来个 ...

最新文章

  1. 第十二届全国大学生智能汽车竞赛获奖名单
  2. “手机”是个什么机器?
  3. 制打印如下所示的n行数字金字塔_关于央行数字货币的技术猜测
  4. c++ 三角函数_精准备考 | 初中数学三角函数知识点全归纳
  5. 《Web漏洞防护》读书笔记——第7章,访问控制防护
  6. spring4.1.8扩展实战之七:控制bean(BeanPostProcessor接口)
  7. springboot ---微信ocr身份证识别
  8. Eview操作步骤——数据导入及数据建模
  9. java spring定时器_Spring定时器的两种实现方式
  10. 有关BT5破解wifi密码的流程及当中经历问题的总结——从寻找ISO镜像到破解wifi密码
  11. 面试官问你的缺点是什么,这么回答漂亮!
  12. (旧)springboot 快速实现登录、注册功能(附Demo源码)
  13. python从TXT导入两列数据绘图 直线多点等分坐标可视化
  14. 【论文阅读笔记】BART: Denoising Sequence-to-Sequence Pre-training for Natural Language Generation, Translati
  15. 基于已知点云地图的NDT的激光SLAM定位
  16. 技能梳理36@淘宝/天猫数据爬取
  17. 交通预见未来(1):循环神经网络之LSTM,不只有七秒钟的记忆
  18. 项目管理的前景如何?
  19. renpy-快速入门
  20. 从零开始学习CANoe(七)—— XML 测试节点

热门文章

  1. 我对人口出生率下降的几点小看法
  2. 为什么说道光天和号是茅台酒创始人之一
  3. 力扣 991. 坏了的计算器
  4. 微型计算机性价比,为高性价比游戏电脑找颗超值芯,锐龙5 5600X VS.酷睿i7-11700K...
  5. IMPOSSIBLE FOODS登陆香港酒店
  6. Android--仿爱奇艺 首页轮播图
  7. c语言switch思维导图,思维导图学习C语言,加深知识点记忆
  8. Ppt如何转化为pdf,职场人士必备技能
  9. 江苏版M401A原版 刷机ARMBIAN注意要点
  10. web前端入门到实战:SCSS 常用属性合集