首页 > 编程语言 >Python入门之选择语句

Python入门之选择语句

时间:2023-01-24 16:23:07浏览次数:39  
标签:语句 elif 入门 Python number -- print input 输入

"""
选择语句
"""

sex = input("请输入性别:")
if sex == "男":
print("您好,先生!")
elif sex == "女":
print("您好,女士!")
else:
print("性别未知!")

print("后续逻辑")

# 调试:让程序中断,逐语句执行。
# -- 目的:审查程序运行时变量取值
# -- 审查程序运行的流程
# -- 步骤:
# 1.加断点
# 2.调试运行 shift + F9
# 3.执行一行 F8
# 3.停止 Ctrl + F2
# 练习1:当钱不够时,提示"金额不足",
#       钱够时,提示“应找回”
#       调试程序
price = input("请输入商品单价:")
peice = float(price)
count = int(input("请输入数量:"))
money = float(input("请输入金额:"))
result = money - peice * count 
if result >= 0:
    print("应该找回:" + str(result))
else:
     print("金额不足")
"""
    练习2: 在控制台中获取一个季度(春夏秋冬)
            显示相对应的月份
            春 --> 1月2月3月
            夏 --> 4月5月6月
            秋 --> 7月8月9月
            冬 --> 10月11月12月
"""
# 方法一:
season = int(input("请输入季度:"))
if season == "春":
    print("1月2月3月")
if season == "夏":
    print("4月5月6月")
if season == "秋":
    print("7月8月9月")
if season == "冬":
    print("10月11月12月")

# 方法二: 相比上面代码的优点:如果前面条件满足,后续条件不再判断
season = int(input("请输入季度:"))
if season == "春":
    print("1月2月3月")
elif season == "夏":
    print("4月5月6月")
elif season == "秋":
    print("7月8月9月")
elif season == "冬":
    print("10月11月12月")

 

# 练习3:在控制台中录入一个数字,再录入一个运算符,(+ - * /)
#       最后录入一个数字。
#       根据运算符,计算两个数字
#       要求:如果运算符,不是加减乘除,则提示"运算符有误"
number_one = float(input("请输入一个数字:"))
operator = input("请输入一个运算符:")
number_two = float(input("请再输入二个数字:"))
if operator == "+":
    print(number_one + number_two)
elif operator == "-":
    print(number_one - number_two)
elif operator == "*":
    print(number_one * number_two)
elif operator == "/":
    print(number_one / number_two)
else:
    print("运算符输入有误...")
# 练习4:在控制台中分别录入4个数字
#       打印最大的数字
# 将第一个数字记在心里,然后与第二个比较
# 如果第二个大于心中的,则心中记录第二个
# 然后与第三个比较.....
number_one = float(input("请输入第1个数字:"))
number_two = float(input("请输入第2个数字:"))
number_three = float(input("请输入第3个数字:"))
number_four = float(input("请输入第4个数字:"))
# 假设第一个是最大值
max_value = number_one
# 以此与后面进行比较
if max_value < number_two:
    # 发现更大的,则替换假设的。
    max_value = number_two

if max_value < number_three:
    max_value = number_three

if max_value < number_three:
    max_value = number_three

print("你输入的最大值:" + str(max_value))
# 练习5:在控制台中录入一个成绩,判断等级(优秀/良好/及格/不及格/输入有误)。
# 90分优秀/80分良好/60及格/59分不及格
# 方法一:
# score = float(input("请输入你的分数:"))
# if score >= 90 and score <= 100:
#     print("你的成绩为优秀!")
# elif score >= 80 and score < 90:
#     print("你的成绩为良好!")
# elif score >= 60 and score < 80:
#     print("你的成绩为及格!")
# elif score >=0 and score < 60:
#     print("你的成绩不及格!")
# else:
#     print("输入有误...")

# 方法二:
# score = float(input("请输入你的分数:"))
# if 90 <= score <= 100:
#     print("你的成绩为优秀!")
# elif 80 <= score < 90:
#     print("你的成绩为良好!")
# elif 60 <= score < 80:
#     print("你的成绩为及格!")
# elif 0 <= score < 60:
#     print("你的成绩不及格!")
# else:
#     print("输入有误...")

# 方法三:
score = float(input("请输入你的分数:"))
if score > 100 or score < 0:
    print("输入有误...")
elif 90 <= score:
    print("你的成绩为优秀!")
elif 80 <= score:
    print("你的成绩为良好!")
elif 60 <= score:
    print("你的成绩为及格!")
else:
    print("你的成绩不及格!")

 

# 练习6:在控制台中获取一个月份
# 打印天数,或者提示输入有误
# 1 3 5 7 8 10 12 --> 31天
# 4 6 9 11 --> 30天
# 2 --> 28天
# 大于12月则输入有误
month = int(input("请输入年份:"))
if month < 1 or month > 12:
    print("输入有误....")
elif month == 2:
    print("当前月份是28天")
elif month == 4 or month == 6 or month == 9\
        or month == 11:
    print("当前月份是30天")
else:
    print("当前月份是31天")

 




标签:语句,elif,入门,Python,number,--,print,input,输入
From: https://www.cnblogs.com/Remick/p/17066149.html

相关文章