根据课程进度编写,绝无超前知识或复杂代码块
本节习题本来是将三名老师分配进办公室,因为我个人感觉有点枯燥,所以改成把女孩分配进房间了,原理都是一样的,没什么区别
刚学习时写的答案,中规中矩,挺标准的
# 3位女孩,3个房间
girls = ["虞书欣","邓紫棋","欧阳娜娜"]
bedrooms = [[], [], []]
# 分配女孩进房间
for girl_name in girls:
index = random.randint(0, 2)
bedrooms[index].append(girl_name)
i = 1
for bedrooms_number in bedrooms:
print("第%d个房间有%d位女孩,她的名字是:" %(i,len(bedrooms_number)))
for girl_name in bedrooms_number:
print("%s" % girl_name, end="\t")
print("\n")
print("-"*20)
i += 1
复习时写的,加入了一些自己的想法,代码没有超纲,可以看看,但是如果你是不会做想参考的话建议只看答案1
就可以了
import random
girl = ["欧阳娜娜", "杨幂", "佘诗曼", "迪丽热巴", "钟嘉欣"]
room = [[], [], []]
print("你旗下的女艺人有",end="")
for girl_name in girl:
print(girl_name,end="、")
print("\b")
# 邀请女孩进房间
for girl_name in girl:
number = random.randint(0,2)
room[number].append(girl_name)
roomA_len = len(room[0])
roomB_len = len(room[1])
roomC_len = len(room[2])
print("你好,我是客服伊兹!\n请输入以下命令进行查询\na代表打印所有房间信息;b代表查询房间号;c代表查询女艺人")
print("-"*50)
order = input("请输入命令:")
if order == "a":
d = 0
for i in range(0,2+1):
if i == 0:
print("房间%d有%d人"%(i,roomA_len))
if roomA_len != 0:
print("名字为",end="")
for i in room[i]:
print(i,end="、")
print("\b")
print("*"*50)
if i == 1:
print("房间%d有%d人"%(i,roomB_len))
if roomB_len != 0:
print("名字为", end="")
for i in room[i]:
print(i, end="、")
print("\b")
print("*" * 50)
if i == 2:
print("房间%d有%d人"%(i,roomC_len))
if roomC_len != 0:
print("名字为", end="")
for i in room[i]:
print(i, end="、")
print("\b")
print("*" * 50)
elif order == "b":
room_number = int(input("请输入你要查询的房间号:"))
print("-" * 50)
print("该房间一共有%d人" % len(room[room_number]))
if len(room[room_number]) != 0:
print("名字为",end="")
for i in room[room_number]:
print(i,end="、")
print("\b")
elif order == "c":
name = input("请输入你要查询的女艺人名字:")
print("-" * 50)
save = 100
if name in room[0]:
save = 0
elif name in room[1]:
save = 1
elif name in room[2]:
save = 2
else:
print("抱歉,这位女士她不在我们的房间内入住")
if save <= 2:
print("她在%d号房间"%save)
else:
pass