Python-----Related judgment statement exercises and question analysis (1)

Python judgment statement exercises and analysis

1. You can find out what day of the month your friend's birthday is by asking 5 questions. Each question asks whether this day falls within the 5-number set.
Insert image description here
The birthday is the sum of the - Lth ​​number in the set in which this number appears. For example: if the birthday is 19, then it will appear in setl. set2 and set5. The first numbers of these three sets are 1.2.16 respectively. The sum of them is 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 = 0#定义生日日期为0
print(set1)#将set1进行输出
print("看上表,你的生日日期是否在里面?")
num1 = int(input("有 输:1 没有 输:2 请选择:  "))#进行选择,若set1中有生日日期则选1,反之选2
if num1 == 1 :#若选1则执行下面程序,若选2则跳过day += 1程序
       day += 1#将生日日期进行加一
print(set2)#同样将set2进行输出
print("看上表,你的生日日期是否在里面?")
num2 = int(input("有 输:1 没有 输:2 请选择:  "))#进行选择,若set2中有生日日期则选1,反之选2
if num2 == 1 :#若选1则执行下面程序,若选2则跳过day += 2程序
       day += 2#将生日日期进行加二
#下面原理同上
print(set3)
print("看上表,你的生日日期是否在里面?")
num3 = int(input("有 输:1 没有 输:2 请选择:  "))
if num3 == 1 :
       day += 4
print(set4)
print("看上表,你的生日日期是否在里面?")
num4 = int(input("有 输:1 没有 输:2 请选择:  "))
if num4 == 1 :
       day += 8
print(set5)
print("看上表,你的生日日期是否在里面?")
num5 = int(input("有 输:1 没有 输:2 请选择:  "))
if num5 == 1 :
       day += 16
elif day == 0 :#判断进行到最后输出生日日期,若set1-set5都选2,则day为0,输出“玩呢???”
       print("玩呢???")
else :
       print("你的生日日期是%d号"%day)#若正常进行则输出day最后的值

operation result:Insert image description here

2.BMI is a way of measuring health based on weight. BMI is calculated by dividing weight in kilograms by height in meters squared. Here's a chart of BMI for people 16 and older: Insert image description here
Write a program that prompts the user to enter their weight in pounds and height in inches, and then displays the BMI values. Note: 1 pound is 0.453592 37 kilograms and 1 inch is 0.0254 meters. Listing 4-6 shows this program.

(height,weight) = eval(input("请输入身高体重: "))
height1 = height / 0.0254#将英寸化为米
weight1 = weight / 0.45359237#将镑换化为千克
bmi = weight1 / (height1 ** 2)#BMI公式
#进行if判断语句
if 30 <= bmi :
	print("痴肥,你好自为之!")
elif 30 > bmi >= 25 :
	print("超重,该减肥了!")
elif 25 > bmi >= 18.5 :
	print("标准,但别太飘了,小心长胖!")
elif bmi < 18.5 :
	print("超轻,太瘦了,多吃点!")

operation result:Insert image description here

3. If a year is divisible by 4 but not 100, or divisible by 400, then the year is a leap year.

year = float(input("请输入一个年份:  "))
if year % 4 == 0 and year % 100 != 0 :#对是否为闰年进行判断
	print("该年为闰年!!!")
elif year % 400 == 0 :
	print("该年为闰年!!!")
else :#若以上两项都不符,则为平年
	print("又不是闰年,激动啥???")

operation result:Insert image description here

4. Suppose you want to develop a program for playing lottery. The program randomly generates a two-digit number, then prompts the user to enter a two-digit number, and determines whether the user wins the bonus according to the following rules.
1) If the numbers entered by the user are exactly the same as the randomly generated numbers (including the order), the bonus is US$10,000.
2) If the number entered by the user is the same as the randomly generated number (not including the order), the bonus is US$3,000.
3) If the number entered by the user has the same digit as the randomly generated number, the bonus is US$1,000.

import random
num1 = random.randint(10,99)#定义电脑随机抽取10-99之间的数
num2 = int(input("请输入一个两位数: "))#参加该游戏的人进行猜数
player1 = num2 % 10#取数的个位
player2 = num2 // 10#取数的十位
computer1 = num1 % 10
computer2 = num1 // 10
print("我们的数字是:" + str(num1))
if player1 == computer1 and player2 == computer2:#若猜的数,个位十位都相同
	print("恭喜你,猜的完全正确,获得10000美元奖金!!!")
elif player1 == computer2 and player2 ==computer1:#若猜的数,个位等于十位
	print("恭喜你,两个数都猜对了,获得3000美元奖金!!!")
elif player1 == computer1 or player1 == computer2 or player2 == computer1 or player2 == computer2:#有一个数猜中
	print("恭喜你,猜对了一个数,获得1000美元奖金!!!")
else :#全都猜不中
	print("恭喜你成功避开了答案,欢迎下次再来!")

operation result:Insert image description here

5. (In algebra: solving quadratic equations of one variable) For example: the square root of ax^2+ bx+c=0 can be obtained using the following formula.
Insert image description here

b^2 - 4ac is called the discriminant of the quadratic equation. If it is positive, then the equation has two real roots. If it is zero, then the equation has one root. If it is negative, then the equation has no real roots.
Write a program that prompts the user to enter the values ​​of a, b, and c, and then displays the result of the discriminant. If the discriminant is positive, two roots are shown. If the discriminant is zero, a root is shown. Otherwise, "The equation has no real roots" is displayed.

import math#导入数学库
( a , b , c ) = eval(input("请输入a,b,c的值:  "))#定义a,b,c的值
n = b ** 2 - 4 * a * c
if n > 0:
	r1 = ((-b) + math.sqrt(b ** 2 - 4 * a * c)) / (2 * a)#math.sqrt表示运用数学库中的算术平方根
	r2 = ((-b) - math.sqrt(b ** 2 - 4 * a * c)) / (2 * a)
	print("该方程有两个根")
	print("r1 = %0.3f   r2 = %0.3f" %(r1,r2))#  %0.3f表示取小数点后3位
elif n == 0:
	r = ((-b) + math.sqrt(b ** 2 - 4 * a * c)) / (2 * a)
	print("该方程有相同的一个根")
	print("r1 = r2 = %0.3f" %r)
elif n < 0:
	print("该方程没有根")

operation result:Insert image description here

6. (Algebra: Solving 2x2 Linear Equations) You can use Clem’s rule to solve the following 2x2 system of linear equations:
Insert image description here

Write a program that prompts the user to enter a, b, c, d, e, and f, and then displays the results. If ad-bc is zero, "Theequation has no solution" is displayed.

(a,b,c,d,e,f) = eval(input("请输入a,b,c,d,e,f的值: "))
n = a * d - b * c
if n == 0:
	print("哪有答案啊???")
else :
	x = (e * d - b * f) / (a * d - b * c)
	y = (a * f - e * c) / (a * d - b * c)
	print("结果:x = %0.2f  y = %0.2f"%(x,y))

Running results: Insert image description here
7. (Finding future data) Write a program to prompt the user to enter a number indicating which day of the week today is (Sunday is 0, Monday is 1, ...Saturday is 6). It also prompts the user to enter the number of days from today to a certain day in the future, and then displays the day of the week in the future.

today = int(input("请输入今天的日期(0-6): "))
day = int(input("请输入经过多少天: "))
day1 = (today + day) % 7
if 0 <= today <=6 :#定义只有星期一到星期天,所以只能输入0-6
	print("今天是星期%s过了%s天后是星期%s"%(today,day,day1))
else :
	print("哪有星期%d啊,你这个小可爱!!!"%today)

Operation results: Insert image description here
8. (Financial aspects: compare prices) Suppose you buy rice and find that it comes in two packages. You'll want to write a program that compares the prices of these two packages. The program prompts the user to enter the weight and price of each package, then displays the package with a better price.

a1, a2 = eval(input("请输入第一种包装的重量和价格: ") )
b1, b2 = eval(input("请输入第二种包装的重量和价格: ") )
if (a2 / a1) > (b2 / b1) :#计算每斤重量值多少钱,并进行比较
	print("第一种包装更nice!!!")
elif (a2 / a1) < (b2 / b1) :
	print("第二种包装更nice!!!")
elif (a2 / a1) == (b2 / b1) :
	print("两种包装一样nice!!!")

Running results: Insert image description here
9. (Detect - a number) Write a program that prompts the user to enter - an integer, and then detects whether the number is divisible by both 5 and 6, by 5 or 6, or only by one of them. (but not divisible by them at the same time).

a = int(input("请输入一个数: "))
if a % 5 ==0 and a % 6 ==0 :
	print("该数能同时被5和6整除")
elif a % 5 ==0 and a % 6 != 0 :
	print("该数能被5整除但不能被6整除")
elif a % 6 ==0 and a % 5 != 0 :
	print("该数能被6整除但不能被5整除")
elif a % 5 !=0 and a % 6 !=0 :
	print("该数5和6都不能整除")

Running results: Insert image description here
10. (Game: Scissors, Rock, Paper) Write a program to play the popular scissors-rock-paper game. (Scissors can cut paper, rocks can collide with scissors, and paper can wrap rocks.) The program randomly generates a number 0, 1, or 2 to represent scissors, rock, and paper. The program prompts the user to enter the number 0, 1, or 2 and then displays a message indicating whether the user or the computer wins, loses, or draws.

import random
c = random.randint(0,2)#定义电脑随机输入0,1,2中任意一个数
p = int(input("请输入一位数0(剪刀),1(石头),2(布): "))
if p > 2 or p < 0 :
	print("认真审题!哥们!!!")
elif p - c == 0 :
	print("平局")
elif p - c == 1 or p - c == -2 :
	print("恭喜你,赢了!!!")
elif p - c ==2 or p - c == -1 :
	print("很遗憾,你输了!!!")

Running results: Insert image description here
11. (Financial Issues: Currency Exchange) Write a program to prompt the user to enter the currency exchange rate between the US dollar and the RMB. The user is prompted to enter 0 to convert USD to RMB and 1 to convert RMB to USD. The user is prompted to enter the amount of US dollars or RMB to convert it into RMB or US dollars respectively.

num = float(input("请输入你要转换的数值:  "))
a = int(input("请输入转换方式0(美元转换为人民币),1(人民币转换为美元):  "))
num1 = num * 6.3634#进行人民币转换成美元
num2 = num / 6.3634#进行美元转换为人民币
if a != 0 and a != 1 :
	print("没有该选项,请重新选择")
elif a == 0 :
	print("你能兑换%f的人民币: "%num1)
elif a == 1 :
	print("你能兑换%f的美元: "%num2)

Running results: Insert image description here
12. (Calculate the perimeter of the triangle) Write a program to read the three sides of the triangle, and calculate its perimeter if the inputs are all legal. Otherwise, displaying this input is illegal. If the sum of two sides is greater than the third side, the input is legal.

side1,side2,side3 = eval(input("请输入三角形的三条边: "))
per = side3 + side2 + side1
if ((side1 + side2) > side3) and ((side1 + side3) > side2) and ((side2 + side3) > side1) :
	print("该三角形的周长为: %d"%per)
elif ((side1 + side2) < side3) or ((side1 + side3) < side2) or ((side2 + side3) < side1) :
	print("该三角形不成立!!! ")

operation result:Insert image description here

Guess you like

Origin blog.csdn.net/Nirvana92/article/details/123938477