Python 错误和异常:C2M5L4错误和异常-V2 随机导入 参与者=[‘杰克’、‘吉尔’、‘拉里’、‘汤姆’] def猜测(参与者): 我的参与者dict={} 对于参与者: my_participant_dict[participant]=random.rand
错误和异常:C2M5L4错误和异常-V2Python 错误和异常:C2M5L4错误和异常-V2 随机导入 参与者=[‘杰克’、‘吉尔’、‘拉里’、‘汤姆’] def猜测(参与者): 我的参与者dict={} 对于参与者: my_participant_dict[participant]=random.rand,python,random,try-catch,except,Python,Random,Try Catch,Except,错误和异常:C2M5L4错误和异常-V2 随机导入 参与者=[‘杰克’、‘吉尔’、‘拉里’、‘汤姆’] def猜测(参与者): 我的参与者dict={} 对于参与者: my_participant_dict[participant]=random.randint(1,9) 如果我的参与者dict['Larry']==9: 返回真值 其他: 返回错误 打印(猜测(参与者)) 1)代码格式很重要。2) 你真正的问题是什么?我强烈建议您阅读有关如何提问和如何回答stackoverflow问题的指南。这
随机导入
参与者=[‘杰克’、‘吉尔’、‘拉里’、‘汤姆’]
def猜测(参与者):
我的参与者dict={}
对于参与者:
my_participant_dict[participant]=random.randint(1,9)
如果我的参与者dict['Larry']==9:
返回真值
其他:
返回错误
打印(猜测(参与者))
1)代码格式很重要。2) 你真正的问题是什么?我强烈建议您阅读有关如何提问和如何回答stackoverflow问题的指南。这是您的解决方案,还是只是对您的问题的编辑?
# Revised Guess() function
def Guess(participants):
my_participant_dict = {}
for participant in participants:
my_participant_dict[participant] = random.randint(1, 9)
try:
if my_participant_dict['Larry'] == 9:
return True
except KeyError:
return None
else:
return False