【问题标题】:Errors and Exceptions:C2M5L4_Errors_and_Exceptions-V2错误和异常:C2M5L4_Errors_and_Exceptions-V2
【发布时间】:2021-08-15 01:21:53
【问题描述】:
import random

participants = ['Jack','Jill','Larry','Tom']

def Guess(participants):
    my_participant_dict = {}
    for participant in participants:
        my_participant_dict[participant] = random.randint(1, 9)
    if my_participant_dict['Larry'] == 9:
        return True
    else:
        return False
    
print(Guess(participants))

【问题讨论】:

  • 1) 代码格式很重要。 2)你的实际问题是什么?我强烈建议您阅读help section of the site,以获取有关如何在 stackoverflow 上提问和回答问题的指导。

标签: python random try-catch except


【解决方案1】:
# 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

【讨论】:

  • 这是您的解决方案,还是只是对您问题的修改?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-09-10
  • 1970-01-01
  • 2020-10-25
  • 2014-04-06
  • 2015-03-28
  • 2021-05-24
  • 1970-01-01
相关资源
最近更新 更多