Python练习——判断和循环

Python 基础总结 (判断和循环)

条件判断

# coding: utf-8

# score = input('Please input score: ')
score = 85

'''
1. 根据输入的分数, 判断是否及格
'''
if score >= 60:
    print("及格")
else:
    print("不及格")
    

'''
2. 根据输入的分数, 判断成绩在哪一个分段

100 ~ 80(含80): 优秀
 80 ~ 70(含70): 良好
 70 ~ 60(含60): 一般
 60 ~ 0 (含00): 不及格
'''
if score >= 80 and score <= 100:
    print('优秀')
elif score >= 70 and score < 80:
    print('良好')
elif score >= 60 and score < 70:
    print('一般')
else:
    print('不及格')

循环结构

# coding: utf-8

'''
遍历循环结构
for <循环变量> in <被遍历结构>:
    <循环体>

while <循环条件>:
    <循环体>


可遍历对象的概念
列表, 元组, 字典, 文件, 字符串等

break  continue
'''

scores = [ 80, 76, 54, 87, 90, 61, 56, 89, 93, 58, 98, 71 ]

# 计算 scores 元素之和
s = 0
for x in scores:
    # s = x + s
    s += x
# print(s)

# 计算平均数
avg = round(s / len(scores), 2)
# print(avg)

# 计算最大值和最小值
f = scores[0]
h = scores[0]
for i in scores:
    if i >= f:
        f = i
    elif i <= h:
        h = i
# print(h)

# print(f)
# print('max =', max(scores))
# print('min =', min(scores))


# 计算中位数
copy = scores.copy()
copy.sort()
length = len(copy)
if length == 0:
    print('列表为空')
if length % 2 == 1:
    mid = (length + 1) // 2
    mid_number = copy[mid-1]
elif length % 2 == 0:
    mid = (length) // 2
    mid_number = (copy[mid - 1] + copy[mid]) / 2
# print(mid_number)
# print(copy)



# 打印
students = [
    {
        'name': 'Mika',
        'age': 18,
    },
    {
        'name': 'Ellis',
        'age': 18,
    },
    {
        'name': 'Harriette',
        'age': 22,
    },
    {
        'name': 'Douglas',
        'age': 23,
    },
    {
        'name': 'Lorine',
        'age': 21,
    },
    {
        'name': 'Wendell',
        'age': 19,
    },
    {
        'name': 'Annamaria',
        'age': 24,
    },
    {
        'name': 'Wendell',
        'age': 19,
    },
]

'''
要求打印:
Rank1: My name is Mika, I am 18 years old.
'''
# for student in students:
#     # print(student, end=' : ')
#     for key in student:
#         print(student[key])

# range(len(students)) == [0, 1, 2, 3, 4, 5, 6, 7]
for i in range(len(students)):
    print(i, students[i]['name'], students[i]['age'])

# for index, value in enumerate(students):
#     print(index, value)

你可能感兴趣的:(Python练习——判断和循环)