方法模块中的考试即将开始,参与者被分成三个不同的讲堂!让用户输入姓氏的第一个字母,并将结果保存为变量!如果首字母介于* A*和*F*之间,则程序应为
你可以在1号报告厅的输出中写出来!如果字母介于* G*和*N*之间,则应
2.你在报告厅里写的是什么?
会被发布。如果所有条件都不适用,则应该
你在大讲堂里写的文章3要发!
发布于 2019-10-29 23:32:37
您可以创建一个小函数,它接受名字的字符串,将其拆分为第一个和最后一个,然后提取该姓氏的第一个字符。
一旦有了该字符,就可以使用ord()将字符串转换为可用于逻辑目的的数字集。把这个放在一个try catch中,当学生们想要惹恼你的时候,不要失败哈哈
def hall_module(name):
try:
first, last = str.split(name)
letters = [char for char in last]
check = ord(letters[0].capitalize())
if ord('A') <= check <= ord('F'):
print('go to lecture hall 1!')
elif ord('G') <= check <= ord('N'):
print('go to lecture hall 2!')
else:
print('go to lecture hall 3!')
except:
print('please give me a string of your first and last name!')编辑:
正如Taegyung指出的,字母在标准python中已经是可比较的,所以你不需要用数字来解析它们:
def hall_module(name):
try:
first, last = str.split(name)
letter = last[0].capitalize()
if 'A' <= letter <= 'F':
print('go to lecture hall 1!')
elif 'G' <= letter <= 'N':
print('go to lecture hall 2!')
else:
print('go to lecture hall 3!')
except:
print('please give me a string of your first and last name!')https://stackoverflow.com/questions/58610233
复制相似问题