我正在尝试制作一个模块,在该模块中我们拥有不同的字典和一个函数来计算每个字典共有的键的值。
我刚刚在IDLE上尝试过,但是存在语法错误。
p3={'name':'Bhuvneshwar Kumar', 'role':'bowl', 'wkts':1, 'overs':10,'runs':71, 'field':1}
p4={'name':'Yuzvendra Chahal', 'role':'bowl', 'wkts':2, 'overs':10, 'runs':45, 'field':0}
p5={'name':'Kuldeep Yadav', 'role':'bowl', 'wkts':3, 'overs':10, 'runs':34, 'field':0}
def fieldfun(player):
fpoint=0
if field>0:
fpoint==10
print("player gets points for fielding")
else:
print("No points for fielding!")
fieldfun(p3)
我应该在该字段中获得分数的所有词典的列表中,如果没有,那么我应该没有分数,而是获取->
Traceback (most recent call last):
File "F:/sem 3/PYTHON/field.py", line 15, in <module>
fieldfun(p1)
File "F:/sem 3/PYTHON/field.py", line 9, in fieldfun
if field>0:
NameError: name 'field' is not defined
答案 0 :(得分:0)
由于播放器是字典,所以您不能直接通过编写键值来评估字典元素。您只需使用#include <stdio.h>
int i = 3, j = 10;
int crypt(int j)
{
return (i = i+j);
}
void decrypt(int x, int i)
{
j += crypt(i);
}
int main(void)
{
int i = 0;
i = crypt(5);
decrypt(i, j);
printf("|%d %d|", i, j);
return 0;
}
或<dict_name>.get('<field_name>')
<dict_name>['<key_name'>]
p3={'name':'Bhuvneshwar Kumar', 'role':'bowl', 'wkts':1, 'overs':10,'runs':71, 'field':1}
p4={'name':'Yuzvendra Chahal', 'role':'bowl', 'wkts':2, 'overs':10, 'runs':45, 'field':0}
p5={'name':'Kuldeep Yadav', 'role':'bowl', 'wkts':3, 'overs':10, 'runs':34, 'field':0}
def fieldfun(player):
fpoint=0
if player.get('field')>0:
fpoint==10
print("player gets points for fielding")
else:
print("No points for fielding!")
fieldfun(p3)