编写一个选课报名程序,一共三门课,编码,足球,机器人,程序依次提示是否喜欢,并提示(y/n)来回答,喜欢编码到教室一登记,喜欢足球到教室二登记,喜欢机器人到教室三登记,默认输出选修课报名,
#【望采纳】
dic_classes = {"教室一": 0, "教室二": 0, "教室三": 0}
def match_class():
match_num = input("请输入你的爱好,所对应的编号:1:编程 2:足球 3:机器人")
if eval(match_num) == 1:
dic_classes["教室一"] += 1
elif eval(match_num) == 2:
dic_classes["教室二"] += 1
else:
dic_classes["教室三"] += 1
if __name__ == '__main__':
i = 0
while i <= 5:
match_class()
i += 1
print(dic_classes)
用input方法
下列代码采用循环结构、选择结构,具体运用input()函数、逻辑运算等。
courses={1:'编码',2:'足球',3:'机器人'}
classrooms={1:'教室一',2:'教室二',3:'教室三'}
count=0
print('下面,你将选择你喜欢的选修课程,只能选择一门。')
while count==0:
for course in courses:
choice=input(f'你喜欢{courses[course]}课程吗?\n喜欢请输入"y",不喜欢请输入"n"。>>[')
if count==0:
if 'y' in choice:
print(f'你选择了{courses[course]}课程,请到{classrooms[course]}登记。')
count+=1
break
if count==0:
print('请你选择一门选修课!')
coding = 0
football = 0
robot = 0
while 1==1:
first = int(input("你是否是最后一个报名者(请输入数字1或者2 (1 是 2 不是)):"))
if first == 1:
print("你喜欢编码吗?")
xx = input("喜欢请输入y,不喜欢请输入n:")
if xx == "y":
coding += 1
else:
pass
print("你喜欢足球吗?")
xc = input("喜欢请输入y,不喜欢请输入n:")
if xc == "y":
football += 1
else:
pass
print("你喜欢机器人嘛?")
xv = input("喜欢请输入y,不喜欢请输入n:")
if xv == "y":
robot += 1
else:
pass
print("bye bye")
break
else:
print("你喜欢编码吗?")
xx = input("喜欢请输入y,不喜欢请输入n:")
if xx == "y":
coding += 1
else:
pass
print("你喜欢足球吗?")
xc = input("喜欢请输入y,不喜欢请输入n:")
if xc == "y":
football += 1
else:
pass
print("你喜欢机器人嘛?")
xv = input("喜欢请输入y,不喜欢请输入n:")
if xv == "y":
robot += 1
else:
pass
print("bye bye")
print("本次爱好选择完毕。编码被选择{}次,足球被选择{}次,机器人被选择{}次。".format(coding,football,robot))
print('有以下三门课,分别为编码、足球、机器人,请通过输入y(表示喜欢)或者n(表示不喜欢)回答您是否喜欢以下课程')
dic = {'编码': '教室一', '足球': '教室二', '机器人': '教室三'}
for key in dic.keys():
while True:
result = input('您喜欢'+key+'吗?(请用y/n回答)')
if result == 'n' or result == 'y':
break
if result == 'y':
print('您选择了', key, ',请进入', dic[key])
break