python第五天上机练习

“”"
在控制台中录入多名学生的名字,如果有重复,不存入列表,如果输入esc,则停止录入,在每行打印学生姓名
“”"

names = []
while True:
    name = input("请录入学生姓名(输入esc停止录入):")
    if name == 'esc':
        break
    elif name not in names:
        names.append(name)
for i in range(len(names)):
    print(names[i])

“”"
练习1:将list_score列表中大于60的元素存入list01中
练习2:获取list_score列表中的最大值(不能采用max)
list_score = [60,85,35,26,20,90]
“”"

list_score = [60, 85, 35, 26, 20, 90]
list01 = []
for i in range(len(list_score)):
    if list_score[i] >= 60:
        list01.append(list_score[i])
max_value = 0
for i in range(len(list_score)):
    if max_value < list_score[i]:
        max_value = list_score[i]
print(max_value)

“”"
练习;在控制台中重复录入字符串,直到输入esc为止,最后打印字符串
“”"

list_str = []
while True:
    str_input = input("请输入字符串:")
    if str_input == "esc":
        break
    else:
        list_str.append(str_input)
str_input = "".join(list_str)
print(str_input)

“”"
练习:英文单词反转
“”"

str_en = "How are you"
result = str_en.split(" ")
result2 = []
for i in range(len(result)-1, -1, -1):
    result2.append(result[i])
str_en2 = " ".join(result2)
print(str_en2)

import random
“”"
输入购买的红球和蓝球号码判断是否购买彩票是否中奖
“”"

list1 = []
while len(list1) < 7:
    i = random.randint(1, 33)
    if i not in list1:
        list1.append(i)
list2 = []
j = random.randint(1, 17)
list2.append(j)
red_list = []
blue_list = []
while len(red_list) < 7:
    num1 = int(input("请输入第%d个红球号码:" % (len(red_list)+1)))
    if 1 > num1 or num1 >= 34:
        print("号码不在范围内")
    elif num1 in red_list:
        print("号码重复")
    else:
        red_list.append(num1)
while True:
    num_blue = int(input("请输入蓝球号码:"))
    if 1 > num_blue or num_blue >= 18:
        print("号码不在范围内")
    else:
        blue_list.append(num_blue)
        break
for i in range(len(red_list)):
    if red_list[i] == list1[i]:
        print("中奖"+str(list1[i]))
    else:
        print("没有中奖")
for j in range(len(blue_list)):
    if blue_list[j] == list2[j]:
        print("中奖"+str(list2[j]))
    else:
        print("没有中奖")
发布了21 篇原创文章 · 获赞 4 · 访问量 3726

猜你喜欢

转载自blog.csdn.net/adim__/article/details/104076608