Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 错误和异常:C2M5L4错误和异常-V2 随机导入 参与者=[‘杰克’、‘吉尔’、‘拉里’、‘汤姆’] def猜测(参与者): 我的参与者dict={} 对于参与者: my_participant_dict[participant]=random.rand_Python_Random_Try Catch_Except - Fatal编程技术网

Python 错误和异常:C2M5L4错误和异常-V2 随机导入 参与者=[‘杰克’、‘吉尔’、‘拉里’、‘汤姆’] def猜测(参与者): 我的参与者dict={} 对于参与者: my_participant_dict[participant]=random.rand

Python 错误和异常: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问题的指南。这

错误和异常:C2M5L4错误和异常-V2
随机导入
参与者=[‘杰克’、‘吉尔’、‘拉里’、‘汤姆’]
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