首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python pass elif

Python pass elif
EN

Stack Overflow用户
提问于 2022-11-03 10:03:42
回答 1查看 37关注 0票数 -2

我对python中的编码很陌生,并希望解决以下问题:如果状态为" new“或"Committed",我希望返回名称,否则,我希望代码正在传递。在我的函数中,我传递一个JSON文件“更新”,其中包含关于状态和更改错误状态的人的姓名的数据。这些州是:“新”、“测试”、“承诺”和“已完成”。

代码语言:javascript
复制
def find_name(updates):
  for update in updates:
    #clear return value
    displayName= ''
    if 'fields' in update:
      fields = update['fields']
      if 'System.State' in fields:
        states = fields['System.State']
        if 'oldValue' not in states:
          if states['newValue'] == 'New':
              return update['displayName']
          elif (states['newValue'] == 'Committed'):
              return update['displayName']
          elif (states['newValue'] == 'Tested'):
              pass
      elif(states['newValue'] == 'Done'):
              pass
  return displayName

但是,如果这个州是经过“考验”或“完成”的,它并没有通过。有谁可以帮我?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-11-03 10:30:01

如果状态与关键字and合并,则可以合并。要了解更多信息,请阅读本文章,这是为初学者准备的。python的一个更高级的特性是,您只需添加反斜杠就可以将一行缩短为新行。如果你不做一些事情,你就没有必要去做一个埃利夫。如果您想一步一步地分解我如何将请求合并为一个。评论一下!

代码语言:javascript
复制
def find_name(updates):
  for update in updates: 
    name = '' # reset the value of name to an empty string 
    if 'fields'       in update and \
       "System.State" in update['fields'] and \
       "oldValue" not in update['fields']['System.State']:
        # You can merge all your if statements into one large.
        # For readability you can put every and clause into it own line by adding a blackshlash to the end of the line 
        # technecally you can also merge the or statement into the first if but this would destroy the easy readability
        if update['fields']['System.State']['newValue'] == 'New' or update['fields']['System.State']['newValue'] == 'Committed':
            return update['name'] # upadte the name 
  return name
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74301180

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档