



上面四个图片的九个关于python中程序的流程控制结构的任务
关注引自强大而免费的微信小程序:皆我百晓生。
任务1:
import math
# 输入三个数作为三角形的三边长
a = float(input("请输入第一条边的长度:"))
b = float(input("请输入第二条边的长度:"))
c = float(input("请输入第三条边的长度:"))
# 根据三边长判断是否可以构成三角形
if a + b > c and a + c > b and b + c > a:
# 计算半周长
s = (a + b + c) / 2
# 计算三角形的面积
area = math.sqrt(s * (s - a) * (s - b) * (s - c))
print("这三个边长构成的三角形面积为:", area)
else:
print("这三个边长无法构成三角形")
任务2:
x = float(input("请输入x的值:"))
if x < 0 or x >= 20:
y = 0
elif 0 <= x < 5:
y = x
elif 5 <= x < 10:
y = 3 * x - 5
elif 10 <= x < 20:
y = 0.5 * x - 2
print("x=%.2f, y=%.2f" % (x, y))
任务3:
def is_leap_year(year):
if year % 4 == 0: # 能被4整除的年份为闰年
if year % 100 == 0: # 能被100整除的年份不是闰年
if year % 400 == 0: # 能被400整除的年份是闰年
return True
else:
return False
else:
return True
else:
return False
year = int(input("请输入年份: "))
if is_leap_year(year):
print(year, "年是闰年")
else:
print(year, "年不是闰年")
任务4:
sum_all = 0
sum_even = 0
sum_odd = 0
for i in range(1, 101):
sum_all += i
if i % 2 == 0:
sum_even += i
else:
sum_odd += i
print("1-100之和:", sum_all)
print("1-100中偶数之和:", sum_even)
print("1-100中奇数之和:", sum_odd)
任务5:
# 从键盘输入一行字符
string = input("请输入一行字符:")
# 初始化计数器
count_alpha_upper = 0 # 大写字母个数
count_alpha_lower = 0 # 小写字母个数
count_digit = 0 # 数字个数
count_other = 0 # 其他字符个数
# 遍历字符串的每个字符
for char in string:
if char.isalpha():
if char.isupper():
count_alpha_upper += 1
else:
count_alpha_lower += 1
elif char.isdigit():
count_digit += 1
else:
count_other += 1
# 输出统计结果
print("大写字母个数:", count_alpha_upper)
print("小写字母个数:", count_alpha_lower)
print("数字个数:", count_digit)
print("其他字符个数:", count_other)
任务6:
def currency_converter(amount, currency):
if currency == "USD":
rate = 6.54 # 根据实际情况设置汇率
converted_amount = amount * rate
return round(converted_amount, 2)
elif currency == "EUR":
rate = 7.84 # 根据实际情况设置汇率
converted_amount = amount * rate
return round(converted_amount, 2)
elif currency == "JPY":
rate = 0.059 # 根据实际情况设置汇率
converted_amount = amount * rate
return round(converted_amount, 2)
else:
return "Unsupported currency"
amount = float(input("请输入兑换金额:"))
currency = input("请输入货币代码(USD、EUR、JPY):")
result = currency_converter(amount, currency)
print("兑换结果:", result)
任务七:
def temperature_conversion():
print("温度转换程序")
print("1. 摄氏度转华氏度")
print("2. 华氏度转摄氏度")
option = input("请选择操作(1或2):")
if option == "1":
celsius = float(input("请输入摄氏度:"))
fahrenheit = celsius * 9/5 + 32
print("{:.2f}摄氏度 = {:.2f}华氏度".format(celsius, fahrenheit))
elif option == "2":
fahrenheit = float(input("请输入华氏度:"))
celsius = (fahrenheit - 32) * 5/9
print("{:.2f}华氏度 = {:.2f}摄氏度".format(fahrenheit, celsius))
else:
print("输入无效的选项")
temperature_conversion()
任务八:
def currency_conversion():
print("货币兑换程序")
print("1. 美元兑换人民币")
print("2. 欧元兑换人民币")
print("3. 日元兑换人民币")
print("输入 exit 退出程序")
exchange_rate = {
"美元": 6.4,
"欧元": 7.8,
"日元": 0.058
}
while True:
option = input("请选择操作(1-3):")
if option == "1":
amount = float(input("请输入美元金额:"))
rmb_amount = amount * exchange_rate["美元"]
print("兑换结果:{:.2f}美元 = {:.2f}人民币".format(amount, rmb_amount))
elif option == "2":
amount = float(input("请输入欧元金额:"))
rmb_amount = amount * exchange_rate["欧元"]
print("兑换结果:{:.2f}欧元 = {:.2f}人民币".format(amount, rmb_amount))
elif option == "3":
amount = float(input("请输入日元金额:"))
rmb_amount = amount * exchange_rate["日元"]
print("兑换结果:{:.2f}日元 = {:.2f}人民币".format(amount, rmb_amount))
elif option == "exit":
break
else:
print("输入无效的选项")
currency_conversion()
任务九:
任务七中已经包含了温度转换的代码,所以只需要进行代码复用即可。将任务七中的代码放入一个循环中,直到输入"exit"退出。
def temperature_conversion():
print("温度转换程序")
print("1. 摄氏度转华氏度")
print("2. 华氏度转摄氏度")
print("输入 exit 退出程序")
while True:
option = input("请选择操作(1或2):")
if option == "1":
celsius = float(input("请输入摄氏度:"))
fahrenheit = celsius * 9/5 + 32
print("{:.2f}摄氏度 = {:.2f}华氏度".format(celsius, fahrenheit))
elif option == "2":
fahrenheit = float(input("请输入华氏度:"))
celsius = (fahrenheit - 32) * 5/9
print("{:.2f}华氏度 = {:.2f}摄氏度".format(fahrenheit, celsius))
elif option == "exit":
break
else:
print("输入无效的选项")
temperature_conversion()