“”"
在控制檯中錄入多名學生的名字,如果有重複,不存入列表,如果輸入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("沒有中獎")