判断语句练习

1. [趣味题——构造王者荣耀英雄介绍“菜单”]编写程序,显示“菜单”,由用户输入代号后,输出对所选英雄的详细介绍。

代码:

title = '王者荣耀英雄查询'
menu = """
                1.元歌
                2.杨玉环
                3.公孙离
                4.露娜
                5.明世隐
                6.东皇太一
                7.退出
"""
info1 = """
                定位:刺客
                生存能力:D
                攻击伤害:B
                技能效果:A
                上手难度:S
"""
info2 = """
                定位:法师
                生存能力:C
                攻击伤害:C
                技能效果:A
                上手难度:B
"""
info3 = """
                定位:射手
                生存能力:D
                攻击伤害:A
                技能效果:C
                上手难度:B
"""
info4 = """
                定位:战士
                生存能力:B
                攻击伤害:B
                技能效果:A
                上手难度:S
"""
info5 = """
                定位:辅助
                生存能力:B
                攻击伤害:C
                技能效果:B
                上手难度:A
"""
info6 = """
                定位:坦克
                生存能力:A
                攻击伤害:D
                技能效果:B
                上手难度:C
"""
while True:
    print(title.center(50, '*'))
    print(menu)
    print('*'*55)
    choice = int(input('选择你想了解的英雄:'))
    if choice == 1:
        print(title.center(50, '*'))
        print(info1)
        print('*' * 55, '\n')
    elif choice == 2:
        print(title.center(50, '*'))
        print(info2)
        print('*' * 55, '\n')
    elif choice ==3:
        print(title.center(50, '*'))
        print(info3)
        print('*' * 55, '\n')
    elif choice == 4:
        print(title.center(50, '*'))
        print(info4)
        print('*' * 55, '\n')
    elif choice ==5:
        print(title.center(50, '*'))
        print(info5)
        print('*' * 55, '\n')
    elif choice ==6:
        print(title.center(50, '*'))
        print(info6)
        print('*' * 55, '\n')
    elif choice ==7:
        print('感谢您的使用。')
        exit(0)
    else:
        print('请输入正确选项。')

运行结果:

2. 输入年、月,输出本月有多少天。合理选择分支语句完成设计任务。
   输入样例1:2004 2
   输出结果1:本月29天
   输入样例2:2010 4
   输出结果2:本月30天

代码:

year = int(input('year:'))
month = int(input('month:'))
if not 1 <= month <= 12:
    print('请输入正确的月份')
elif month == 2:
    if (year % 4 == 0 and year % 100 != 0) or year % 400 == 0:
        print('本月29天')
    else:
        print('本月28天')
else:
    if month in [1, 3, 5, 7, 8, 10, 12]:
        print('本月31天')
    else:
        print('本月30天')

运行结果:


3.输入三个数,求一元二次方程ax**2 + bx +c = 0的解;

代码:

import math

print('求ax^2+bx+c=0的解')
a = float(input('a = '))
if a == 0:
    print('二元一次方程中a不为0')
    exit()
b = float(input('b = '))
c = float(input('c = '))
delta = b**2-4*a*c
if delta == 0:
    x = (math.sqrt(delta)-b)/(2*a)
    print('方程有一个实根。x = %.2f' %x)
elif delta < 0 :
    print('方程无实根')
elif delta > 0:
    x1 = (math.sqrt(delta)-b)/(2*a)
    x2 = (-math.sqrt(delta)-b)/(2*a)
    print('方程有两个实根.x1 = %.2f  x2 = %.2f' %(x1, x2))

运行结果:

4.用 if 判断输入的值是否为空?如果为空,报错Error。

代码:

In = input('判断输入是否为空:')
if not In:
    print('Error')

运行结果:

5. 根据用于指定月份,打印该月份所属的季节。
**提示: 3,4,5 春季 6,7,8 夏季  9,10,11 秋季 12, 1, 2 冬季

代码:

month = int(input('month:'))
if month in (3,4,5):
    print('春季')
elif month in (6,7,8):
    print('夏季')
elif month in (9,10,11):
    print('秋季')
elif month in (1,2,12):
    print('冬季')
else:
    print('非法输入')

运行结果:

猜你喜欢

转载自blog.csdn.net/Sangyumo/article/details/81626971