我对python中的编码很陌生,并希望解决以下问题:如果状态为" new“或"Committed",我希望返回名称,否则,我希望代码正在传递。在我的函数中,我传递一个JSON文件“更新”,其中包含关于状态和更改错误状态的人的姓名的数据。这些州是:“新”、“测试”、“承诺”和“已完成”。
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但是,如果这个州是经过“考验”或“完成”的,它并没有通过。有谁可以帮我?
发布于 2022-11-03 10:30:01
如果状态与关键字and合并,则可以合并。要了解更多信息,请阅读本文章,这是为初学者准备的。python的一个更高级的特性是,您只需添加反斜杠就可以将一行缩短为新行。如果你不做一些事情,你就没有必要去做一个埃利夫。如果您想一步一步地分解我如何将请求合并为一个。评论一下!
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 namehttps://stackoverflow.com/questions/74301180
复制相似问题