[Python] Ejercicio --- Declaración de juicio

"""
Demo 20
数据:体重weight,身高hight,求BMI
步骤:
1、提示用户输入以磅为单位的体重和以英寸为单位的身高
2、根据公式计算BMI。
3、BMI<18.5 -->超轻
   18.5<=BMI<25.0 -->标准
   25.0<=BMI<30.0 -->超重
   30.0<=BMI -->痴肥
"""

weight = eval(input("请输入以磅为单位的体重:"))
hight = eval(input("请输入以英寸为单位的身高:"))
weight = weight * 0.45359237
hight = hight * 0.0254
BMI = weight / hight ** 2

if 0<BMI<18.5 :
    print("超轻!")
elif 18.5 <= BMI <25.0 :
    print("标准!")
elif 25.0 <= BMI < 30.0 :
    print("超重!")
else:
    print("痴肥!")

Resultados de la prueba:

 

year = int(input("Enter a year:"))
condition1 = year % 4 == 0 and year % 100 != 0
condition2 = year % 400 == 0
if condition1 or condition2:
    print("%d年是闰年"%year)
else:
    print("%d不是闰年"%year)

Resultados de la prueba:

import random
com = random.randint(10,99)
num = int(input("Enter a number:"))
com1 = com // 10
com2 = com % 10
num1 = com // 10
num2 = com % 10
if com == num:
    print("恭喜!获得一等奖!奖金为10000美元")
elif com2 == num1 and com1 == num2:
    print("恭喜!获得二等奖!奖金为3000美元")
elif com1 == num1 or com1 == num2 or com2 == num1 or com2 == num2:
    print("恭喜!获得三等奖!奖金为1000美元")
else:
    print("谢谢参与!")

 Resultados de la prueba:

a,b,c = eval(input("Enter a,b,c:"))
total = b ** 2 - 4 * a * c
if total > 0 :
    r1 = (-b + total ** 0.5) / (2 * a)
    r2 = (-b - total ** 0.5) / (2 * a)
    print("The roots are %.6f and %.6f"%(r1,r2))
elif total == 0 :
    r = (-b - total ** 2) / (2 * a)
    print("The root is %s"%r)
elif total < 0 :
    print("The equation has no real roots")

Resultados de la prueba:

"""
Demo 24
数据:a,b,c,d,e和f
步骤:
1、提示用户输入a,b,c,d,e和f,求线性方程组的解x,y
2、根据公式计算
3、若 ad-bc=0 --> "The equation has no solution"
      其他情况情况输出x,y结果
"""
a,b,c,d,e,f = eval(input("Enter a,b,c,d,e,f:"))

x = (e * d - b * f) / (a * d - b * c)
y = (a * f - e * c) / (a * d - b * c)

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

Resultados de la prueba:

"""
Demo 25
数据:今天是一周内的哪一天number,未来某天的天数days,求未来这一天是周几
步骤:
1、提示用户分别输入一个数字表示今天是一周的哪天和从今天到未来某天的天数
2、 0--周天,1--周一,2--周二,3--周三,4--周四,5--周五,6--周六
    未来这天星期几---fdays = (number + days) % 7
3、输出未来这天是周几
"""

number = eval(input("Enter today's day:"))
days = eval(input("Enter the number of days elapsed since today:"))
fdays = (number + days) % 7

if fdays == 0:
    Day = 'Sunday'
elif fdays == 1:
    Day = 'Monday'
elif fdays == 2:
    Day = 'Tuesday'
elif fdays == 3:
    Day = 'Wednesday'
elif fdays == 4:
    Day = 'Thursday'
elif fdays == 5:
    Day = 'Friday'
else:
    Day = 'Saturday'


if number == 0:
    today = 'Sunday'
elif number == 1:
    today = 'Monday'
elif number == 2:
    today = 'Tuesday'
elif number == 3:
    today = 'Wednesday'
elif number == 4:
    today = 'Thursday'
elif number == 5:
    today = 'Friday'
else:
    today = 'Saturday'

print("Todays is %s and the future day is %s" %(today,Day))

 Resultados de la prueba:

 

"""
Demo 26
数据:重量weight,价钱price,大米单价a
步骤:
1、提示用户分别输入两种大米的重量和价钱
2、计算大米单价。 a = price/weight
3、比较大小
4、输出价格更低的包装
"""
weight1,price1 = eval(input("Enter weight and price for package 1:"))
weight2,price2 = eval(input("Enter weight and price for package 2:"))
a1 = price1 / weight1
a2 = price2 / weight2
if a1==a2:
    print("Both packages are equally cheap!")
elif a1<a2:
    print("Package 1 has the better price. ")
else:
    print ("Package 2 has the better price.")

Resultado de la prueba:

 

"""
Demo 27
数据:一个整数num
步骤:
1、提示用户输入一个整数
2、判断是否能被5和6的整除关系
3、输出结果
"""

num = int(input("Enter an integer:"))
if num % 30 == 0:
    a="True"
else:
    a="False"

if num % 5 == 0 or num % 6 == 0 :
    b="True"
else:
    b="False"

if num % 5 != 0 and num % 6 == 0 :
    c="True"
elif num % 5 == 0 and num % 6 != 0:
    c="True"
else:
    c="False"

print("Is %d divisible by 5 and 6?%s"%(num,a))
print("Is %d divisible by 5 or 6?%s"%(num,b))
print("Is %d divisible by 5 or 6,but not both?%s"%(num,c))

Resultados de la prueba:

"""
0-剪刀
1-石头
2-布
"""
import random
com = random.randint(0,2)
user = int(input("0-剪刀;1-石头;2-布:"))
com_str = ""
user_str = ""
if com == 0 :
    com_str = "剪刀"
elif com == 1:
    com_str = "石头"
elif com == 2 :
    com_str = "布"

if user == 0 :
    user_str = "剪刀"
elif user == 1 :
    user_str = "石头"
elif user == 2:
    user_str = "布"

if com - user == 1 or com - user == -2:
    print("电脑出%s,你出%s,电脑赢!"%(com_str,user_str))
elif com - user == 0 :
    print("电脑出%s,你出%s,平局!"%(com_str,user_str))
else:
    print("电脑出%s,你出%s,用户赢!"%(com_str,user_str))

Resultados de la prueba:

"""
Demo 29
数据:美元dollars,人民币RMB,转换的汇率rate,选择转化货币的方式choice,金额大小amount
步骤:
1、提示用户输入美元和人民币之间的货币汇率
2、提示用户输入转化的货币(0--美元变人民币,1--人民币变美元)
3、求出转换的金额
4、输出转换的金额
"""

rate = eval(input("Enter the exchange rate from dollars to RMB:"))
choice = eval(input("Enter 0 to convert dollars to RMB and 1 vice versa:"))

if choice == 0:
    amount= eval(input("Enter the dollars amount:"))
    a = amount * rate
    print("$%.1f is %.1f yuan"%(amount,a))
elif choice == 1:
    amount= eval(input("Enter the RMB amount:"))
    b = amount / rate
    print("%.1f yuan is $%.2f"%(amount,b))
else:
    print("Incorrect input")

Resultados de la prueba:

a,b,c = eval(input("Enter three edges:"))
if a + b > c and a + c > b and b + c > a:
    print("The perimeter is",a+b+c)
else:
    print("The input is invalid!")

Resultados de la prueba:

 

"""
Demo 31
数据:一周的星期几h,一个月的哪一天q,月份m,世纪数j,一个世纪的某一年k
步骤:
1、提示用户输入年份
2、提示用户输入月份
3、提示用户输入一个月中的那一天
4、计算一周的星期几
5、计算世纪数
6、计算是世纪的某一年
7、求出是一周的星期几
8、输出
"""
import math
year = eval(input("Enter years:(e.g.:2008):"))
month = eval(input("Enter month:1-12:"))
q = eval(input("Enter the days of the month:1-31:"))

if month == 1:
    m = 13
    k = year % 100 - 1
elif month == 2:
    m = 14
    k = year % 100 - 1
else:
    m = month
    k = year % 100

j = math.floor(k / 100)
h = (q + math.floor(26*(m + 1) / 10)+ k + math.floor(k / 4) + math.floor(j / 4) + 5 * j) % 7

a = ''
if h == 0:
    a ='Saturday'
elif h == 1:
    a ='Sunday'
elif h == 2:
    a ='Monday'
elif h == 3:
    a ='Tursday'
elif h == 4:
    a ='Wednesday'
elif h == 5:
    a ='Thursday'
elif h == 6:
    a = 'Friday'

print("Day of the week is :" ,a)

Resultados de la prueba:

 

 

"""
Demo 32
数据:一个点坐标(x,y),圆半径r
步骤:
1、提示用户输入一个点坐标
2、求点到(0,0)的距离
3、判断是否在圆内
    s <= 10 --->在圆内
    s > 10 ---> 在圆外
4、输出结果
"""
x1,y1 = eval(input("Enter a point with two coordinates:"))
x2 = 0
y2 = 0
s = ((x1 - x2)**2 + (y1 - y2)**2)**0.5

if s <= 10:
    a="in"
else:
    a="not in"

print("Point (%.1f,%.1f) is %s the circle" %(x1,y1,a))

Resultados de la prueba:

 

x,y = eval(input("Enter x,y:"))
if -5 < x < 5 and -2.5 < 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))

Resultados de la prueba:

 

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

Resultados de la prueba:

"""
y = kx + b
"""
x,y = eval(input("Enter a point:"))
if 0 <= x <= 200 and 0 <= 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")

Resultados de la prueba:

"""
Demo 36
数据:圆心坐标(x1,y1)(x2,y2);半径r1,r2;距离d
步骤:
1、提示用户输入两个圆心坐标
2、判断距离
   d <= r1+r2 ---> overlap
   d <=|r1-r2| --->inside
   d > r1+r2 ---> not overlap
3、输出结果
"""

x1,y1,r1 = eval(input("Enter circle1's center x-,y-coordinates,and radius:"))
x2,y2,r2= eval(input("Enter circle2's center x-,y-coordinates,and radius:"))

d = ((x1 - x2)** 2 + (y1 - y2)** 2)** 0.5
if d<= r1+r2:
    s='overlaps'
elif d<=abs(r1-r2):
    s='is in inside'
else:
    s='does not overlap'

print("cirle2 %s circle1" %s)

Resultados de la prueba:

 

 

Supongo que te gusta

Origin blog.csdn.net/trichloromethane/article/details/108348301
Recomendado
Clasificación