Python’s common questions about some judgment statements

Mainly examine the branch structure (if else elif usage)

Question: 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

Basic idea *: five questions are asked, the user needs to answer, the user needs to observe the table, and the output is accumulated*

code show as below:

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)#输出表格供用户观察
print("上面set中有你生日那天吗??")#显示问题
choice = int(input("有请输入1 反之则输入2:"))
if choice == 1 : #如果回答的是1则会执行下一个程序,反正不予理睬
   day += 1 #在原基础上加 1
print(set2) 
print("上面set中有你生日那天吗??")
choice = int(input("有请输入1 反之则输入2:"))
if choice == 1 :
   day += 2
print(set3)
print("上面set中有你生日那天吗??")
choice = int(input("有请输入1 反之则输入2:"))
if choice == 1 :
   day += 4
print(set4)
print("上面set中有你生日那天吗??")
choice = int(input("有请输入1 反之则输入2:"))
if choice == 1 :
   day += 8
print(set5)
print("上面set中有你生日那天吗??")
choice = int(input("有请输入1 反之则输入2:"))
if choice == 1 :
   day += 16
if day ==0 :
   print("你骗人!!!玩我呢?")
else :
       print("你的生日在%d号!"%day)

Output style:

Insert image description here


topic:Insert image description here

Ideas for doing the questions:

When you see data with this kind of design scope, you must learn to think of the multi-branch (if else) structure to solve problems.

code show as below:

weight,height = eval(input("请输入您的体重和身高:"))
BMI = weight / (height**2)
if BMI < 18.5 :
	print("超轻")
elif 18.5 <= BMI < 25.0 :
    print("标准")
elif 25.0 <= BMI < 30.0 :
    print("超重")
else :
	 print("痴肥")

Insert image description here
I’m super light, so it seems I need to eat more!!!


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

Problem-solving ideas: Leap years are either divisible by 4 and not divisible by 100, or they are divisible by 400. Anyway, they are all ordinary years.

code show as below:

year = int(input("请输入一个年份:"))
if year % 4 == 0 and year % 100 != 0 :
	print("该年为闰年!")
elif year % 400 == 0 :
	print("该年为闰年!")
else :
	print("该年为平年!")

Insert image description here


Question: 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 based on 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 does not match the randomly generated number (excluding the order), the bonus is US$3,000.
(3) If the number entered by the user and the randomly generated number have the same digit, the bonus is US$1,000.

Idea guidance: The numbers are the same, one of them is the same, the winning number is random!

code show as below:

import random
a = random.randint(10,99)#定义电脑随机抽取10-99之间的数
numb = int(input("请输入一个两位数的数字:"))
b = numb % 10 #取个位的数
c = numb // 10 #取十位的数
d = a % 10 #取个位的数
e = a // 10 #取十位的数
if b == d and c == e :
      print("恭喜你中奖了,喜提10000美金!!!")
elif b == e and c ==d :
  	  print("恭喜你中奖了,喜提3000美金!!!")
elif b == d or b == e or c ==d or c == e :
  print("恭喜你中奖了,喜提1000美金!!!")
else :
	print("未中奖!")

Insert image description here



topic:Insert image description here

code show as below:

import math 
(a,b,c) = eval(input("请输入a,b,c的值:"))
v = b**2 - 4 * a * c
if v > 0 :
    r1 = ((-b) + math.sqrt(b**2 - 4 * a * c)) / (2 * a)
    r2 = ((-b) - math.sqrt(b**2 - 4 * a * c)) / (2 * a)
    print("该方程有两个解!")
    print("两个解分别为:r1 = %0.2f , r2 = %0.3f"%(r1,r2))
elif v == 0 :
    r = ((-b) + math.sqrt (b**2 - 4 * a * c)) / (2 * a)
    print("该方程只有一个解!")
    print("解为:r1 = r2 = %0.3f"%(r))
elif v < 0 :
    print ("该方程无解!!!")

Insert image description here


topic:Insert image description here

Idea: Use branch structure

code show as below:

(a,b,c,d,e,f) = eval(input("请分别输入a,b,c,d,e,f的值:"))
v = a * d - b * c
if v == 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 %0.2f and Y is %0.2f"%(x,y))

Insert image description here


Question: (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 future day, and then displays the day of the week that the future day is. Below is an example run.

Insert image description here

code show as below:

today = int(input("今天是星期几呀??(请输入1~7来表示)"))
b = int(input("请问你想知道几天后的星期数?"))
day = (today + b) % 7
if 1 <= day <= 7 :
	print("今天是星期%s,在过%s天就是星期%s了噢"%(today,b,day))
else :
	print("你别拿我不当人啊!!!我可聪明了!!!")

Insert image description here


Question: (Finance: Comparing Prices) Suppose you buy rice and find that it comes in two packages. You'll want to write a program to compare 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. Below is an example run.

Insert image description here
Idea: Calculate how much a pound of rice costs to compare.

code show as below:

(a,b) = eval(input("请输入A类大米的重量和价格"))
(c,d) = eval(input("请输入B类大米的重量和价格"))
if (b / a) > (d / c) :
    print("A类大米性价比更高噢!!!")
else :
	print("B类大米性价比更高噢!!!")

Insert image description here


Question: (Detect a number) Write a program to prompt the user to enter an integer, and then detect whether the number is divisible by both 5 and 6, by 5 or 6, or only by one of them (but not by them at the same time) divisible).

code show as below:

numb = int(input("请输入一个整数:"))
if numb % 5 == 0 and numb % 6 == 0 :
	print("该数能同时被5和6整除喔")
elif numb % 5 == 0 and numb % 6 != 0 :
	print("该数能被5整除,不能被6整除")
elif numb % 5 != 0 and numb % 6 == 0 :
	print("该数不能被5整除,能被6整除")
else :
	print("该数不能被5或者6整除")

Insert image description here


Topic: (Game: Scissors, Rock, Paper) Write a program to play the popular game of Scissors-Rock-Paper. (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 that the user Or whether the computer won, lost, or drew. Below is an example run.

Insert image description here

code show as below:

import random
a = random.randint(0,2)#定义电脑随机抽取0-2之间的数
b = int(input("请出拳!用0,1,2代表剪刀石头布"))
if b > 2 or b < 0 :
	print("请读题后重新开始!")
elif a == b:
	print("平局再来一次呗!")
elif b - a == 1 or b - a == -2 :
	print("恭喜你赢得了比赛!")
elif b - a == -1 or b - a== 2 :
	print("你输了,下次加油噢!")

Insert image description here



Topic: (Financial Problem: 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 a dollar amount or a RMB amount to convert it to RMB or USD respectively. Below are some example runs.

Insert image description here

code show as below:

lv = float(input("请输入人民币与美元之间的汇率"))
choose = int(input("请选择:0为转人民币,1为转美元"))
money = float(input("请输入相应的钱"))
if choose == 1 :
    a = money / lv
    print("%s人民币对应的美元为%s"%(money,a))
elif choose == 0 :
	b = money * lv
	print("%s美元对应的人民币为%s"%(money,b))

Insert image description here


Question: (Calculate the perimeter of a triangle) Write a program to read the three sides of a 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 loser is legal. Below is an example run.

code show as below:

(a,b,c) = eval(input("请输入三角形的三边:"))
if (a + b) > c and (a + c) > b and (c + b) > a :
    C = a + b + c
    print("该三角形的周长为%s"%(C))
else :
    print("该三角形不存在")

Insert image description here


topic:

Insert image description here

code show as below:

(year,mouth,q) = eval(input("请输入某年,某月,某日 "))
if mouth == 1 or mouth == 2 :
   m = mouth + 12
   year -= 1
else :
   m = mouth
   year = year
j = year // 100
k = year % 100
h = (q + ((26 * (m + 1))//10) + k + k // 4 + j // 4 + 5 * j) % 7
if h == 0:
    print("该日子对应的是星期六")
elif h == 1:
	print("该日子对应的是星期天")
elif h == 2:
	print("该日子对应的是星期一")
elif h == 3:
	print("该日子对应的是星期二")
elif h == 4:
	print("该日子对应的是星期三")
elif h == 5:
	print("该日子对应的是星期四")
elif h == 6:
	print("该日子对应的是星期五")

Insert image description here


topic:

Insert image description here

code show as below:

import math
(x,y) = eval(input("请输入点的坐标:"))
R = math.sqrt((x**2) + (y**2))# 两点之间的距离公式
if R < 10 :
	print("该点在圆内")
elif R == 10 :
	print("该点在圆上")
else :
	print("该点在圆外")

Insert image description here


topic:

Insert image description here

code show as below:

(x , y )= eval(input("请输入x , y: "))
if -(10/2) <= x <= 10/2 and -(5/2)<= y <= 5/2:#注意考虑四个象线的坐标
	print("该点在该矩形中")
else :
	print("该点不在该矩形中")

Insert image description here

Topic: (Palindrome number) Write a program to prompt the user to enter a three-digit integer, and then determine whether it is a palindrome number. If a number is the same when read from left to right and from right to left, then the number is a palindrome.

Main idea: The question is about three digits. If it is a palindrome number, the first and second digits must be the same, and all the first and third digits must be the same.

code show as below:

numb = int(input("请输入一个三位整数为:"))
a = numb // 100
b = numb % 10
if a == b :
	print("该数为回文数!")
else :
	print("该数不是回文数!")

Insert image description here


Question: (Geometry question: Is the point inside the triangle?) Suppose a right triangle is placed on a horizontal plane, as shown in the figure below. The right angle point is at (0,0) and the other two points are at (200,0) and (0,100). Write a program that prompts the user for a point with x and y coordinates, and then determines whether the point is within the triangle.

Insert image description here

Basic idea: control conditions, restrict some conditions, such as one image line, three image lines and four image lines, to simplify the question

code show as below:

(x,y) = eval(input("请输入该点的坐标:"))
k1 = 100 / 200 #通过斜率的大小来判断,
if 0 <= x <= 200 and 0 <= y <= 100 :
	k = y / x 
	if k > k1 :
		print("该点不在三角形里面")
	else :
		print("该点在三角形里面")
else :
	print("该点不在三角形里面")


Insert image description here


Title: (Geometric problem: two circles) Write a program to prompt the user to enter the coordinates of the centers of the two circles and their radii, and then determine whether the second circle is within the first circle or overlaps with the first circle. , as shown in Figure 4-11. (Tip: If the distance between the two centers is ≤|rl- r2|, then circle2 is within circlel. If the distance between the two centers is ≤rl1+r2, then circle2 overlaps with circlel. Test your program to cover all situations.)

Insert image description here

code show as below:

import math
(x1,y1,r1) = eval(input("请分别输入第一个圆的坐标和半径"))
(x2,y2,r2) = eval(input("请分别输入第二个圆的坐标和半径"))
a = math.sqrt((x1 - x2)**2 + (y1 - y2)**2)
if abs(r1 - r2) < a <(r1 + r2) :#abs是绝对值的意思
   print("两圆的位置关系是香蕉")
elif a < abs(r1 - r2) :
	print("两圆的位置关系是包含")
elif a == abs(r1 - r2) :
    print("两圆的位置关系是内切")
elif a == abs(r1 + r2) : 
	print("两圆的位置关系是外切")
else :
	print("两圆的位置关系是相离")

Insert image description here

Guess you like

Origin blog.csdn.net/Zombie_QP/article/details/124025903