Python练习题 判断语句(19~36)一

Demo19
**

你可以通过询问5个问题来找出你朋友的生日在一个月中的哪一天。每个问题都在询问 这一天是否在5个数字集中。

生日就是出现这个数字的集合的第一t 个数字的和,例如:如果生日是19,那它就会在
setl. set2和set5中出现。这三个集合的第一个数字分别是1. 2、16。它们加起来的和就
是19。
在这里插入图片描述
程序编辑:

"""
数据:5个数字集合 累加和
步骤:
1.先问在不在集合1 在 累加1 否则不管
2.先问在不在集合2 在 累加2 否则不管
3.先问在不在集合3 在 累加4 否则不管
4.先问在不在集合4 在 累加8 否则不管
5.先问在不在集合5 在 累加16 否则不管
6.输出累加的结果
"""
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 = 0

print(set1)
choice = input("你的生日在不在以上数字当中(y/n):")
if choice == "y" or choice == "Y" or choice == "yes" or choice =="YES" or choice =="yES":
       day += 1

print(set2)
choice = input("你的生日在不在以上数字当中(y/n):")
if choice == "y" or choice == "Y" or choice == "yes" or choice =="YES" or choice =="yES":
       day += 2

print(set3)
choice = input("你的生日在不在以上数字当中(y/n):")
if choice == "y" or choice == "Y" or choice == "yes" or choice =="YES" or choice =="yES":
       day += 4

print(set4)
choice = input("你的生日在不在以上数字当中(y/n):")
if choice == "y" or choice == "Y" or choice == "yes" or choice =="YES" or choice =="yES":
       day += 8

print(set5)
choice = input("你的生日在不在以上数字当中(y/n):")
if choice == "y" or choice == "Y" or choice == "yes" or choice =="YES" or choice =="yES":
       day += 16

if day == 0 :
       print("玩我呢?")
else :
       print("哈哈,你的生日在%d号!"%day)

测试:(例如我的生日在21号,咱们可以测试一下,哈哈哈)

J:\python\python.exe K:/python2020.8.4练习/PythonCode/day1/day02/19.py
1	3	5	7
9	11	13	15
17	19	21	23
25	27	29	31

你的生日在不在以上数字当中(y/n):y
2	3	6	7
10	11	14	15
18	19	22	23
26	27	30	31

你的生日在不在以上数字当中(y/n):n
4	5	6	7
12	13	14	15
20	21	22	23
28	29	30	31

你的生日在不在以上数字当中(y/n):Y
8	9	10	11
12	13	14	15
24	25	26	27
28	29	30	31

你的生日在不在以上数字当中(y/n):n
16	17	18	19
20	21	22	23
24	25	26	27
28	29	30	31

你的生日在不在以上数字当中(y/n):y
哈哈,你的生日在21号!

Process finished with exit code 0

在这里插入图片描述

Demo20

BMI是根据体重测量健康的方式。通过以千克为单位的体重除以以米为单位的身高的 平方计算出BMI。下面是16岁以上人群的BMI图表:

编写一个程序,提示用户输人以磅为单位的体重和以英寸为单位的身高,然后显示BMI值。注意: 1 磅是
0.453 592 37千克而1英寸是0.0254米。程序清单4-6给出这个程序。
在这里插入图片描述

程序编辑:

# 数据:体重,身高
# 步骤:
# 1.提示用户输入体重和身高
# 2.计算公式为:BMI=体重(千克)除以身高(米)的平方。
# 3.显示BIM值

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("您输入错误的值")

测试:(看来我还是健康的,还是标准呀,哈哈哈…)

J:\python\python.exe K:/python2020.8.4练习/PythonCode/day1/day02/20.py
请输入您的体重(以千克为单位):60
请输入您的身高(以米为单位):1.68
标准

Process finished with exit code 0

在这里插入图片描述

Demo21
**

一个年份如果能被4整除但不能被100整除,或能被400整除,那么这个年份就是闰年。

**

程序编辑:

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

测试:

J:\python\python.exe K:/python2020.8.4练习/PythonCode/day1/day02/21.py
请输入年份:2020
2020 是个闰年

Process finished with exit code 0

在这里插入图片描述

Demo22
**

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

**
程序编辑:

"""
数据:随机产生的彩票数字com 用户输入的数字usr
      comA comB   usrA usrB
步骤:
1.提示用户输入一个两位数
2.电脑随机生成一个两位数
3.将各自的数字进行拆分
4.ca == ua and cb == ub 一等奖
5.ca == ub and cb == ua 二等奖
6.ca == ua or ca == ub or cb == ua or cb == ub 三等奖
"""
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("谢谢参与!")

测试:

J:\python\python.exe K:/python2020.8.4练习/PythonCode/day1/day02/22.py
请输入数字:98
二等奖

Process finished with exit code 0

在这里插入图片描述
在这里插入图片描述

Demo23.
**

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

Enter a, b, c: 1.0, 3, 1 Penea
The roots are -0.381966 and -2. 61803
Enter a, b, c: 1, 2.0, 1 Ceter
The root is -1
Enter a, b, C: 1, 2, 3 Enter
The equation has no real roots
在这里插入图片描述

程序编辑:

# 数据:a b c delt x1 x2
# 步骤:
# 1.提示用户输入a b c 三个值
# 2.计算delt = b^2-4ac
# 3.判断delt的值
#     delt > 0 两个解
#     delt == 0 一个解
#     delt < 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")

测试:

J:\python\python.exe K:/python2020.8.4练习/PythonCode/day1/day02/23.py
Enter a,b,c:1,3,1
x1 = -0.38 , x2 = -2.62

Process finished with exit code 0

在这里插入图片描述

Demo24
**

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

**
Enter a, b, C, d, e, f: 9.0, 4.0, 3.0, -5.0, -6.0, -21.0 P-Erter
xis-2.0andyis3.0.
Enter a, b, c, d, e, f: 1.0,2.0, 2.0, 4.0,4.0 5.0 FHeer
The equation has no so’ lution
在这里插入图片描述

程序编辑:

# 数据:a b c d e f
# 步骤:
# 1.提示用户输入a b c d e f 六个值
# 2.计算ad - bc 是不是等于 0
# 3.判断 x1 x2的值

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))

测试:

J:\python\python.exe K:/python2020.8.4练习/PythonCode/day1/day02/24.py
Enter a,b,c,d,e,f:9,4,3,-5,-6,-21
x is -2.0 and y is 3.0

Process finished with exit code 0

在这里插入图片描述

Demo25
**

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

**
Enter today’s day: 1
Enter the number of days elapsed since today: 3
Today is Monday and the. future day is Thursday

Enter today’s day: 0
Enter the number of days elapsed since today: 31
Today is Sunday and the future day is Wednesday
程序编辑:

# 步骤:
# 1.提示用户根据要求输入今天的日期,例如(星期天是0,星期一是1, ....,星期六是6)
# 2.提示用户输入过多少天
# 3.计算出周几
# 4.输出周几
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 星期六")

测试:

J:\python\python.exe K:/python2020.8.4练习/PythonCode/day1/day02/25.py
输入今天的日期,例如(星期天是0,星期一是1, ....,星期六是6):3
请输入要经过多少天:14
Today is Sunday and the future day is 星期三

Process finished with exit code 0

在这里插入图片描述

  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值