为什么我会收到密钥错误

时间:2017-05-02 20:13:09

标签: python

导入数学

def Richter_Joules( scale ):
sum=(1.5*scale)+4.8
return 10**sum


def Richter_TNT( R_scale ):
val=Richter_Joules(R_scale)/(4.184*10**9)    # One ton TNT yields 4.184x109 #joules
return val

L=[1.0,  5.0,  9.0,  9.1,  9.5]


def main( ):

print( "Richter       Joules                      TNT" )
print("{ }    { }         { }".format( L[0],  Richter_Joules( L[0] ),  Richter_TNT( L[0] ) ))
print("{ }    { }         { }".format( L[1],  Richter_Joules( L[1] ),  Richter_TNT( L[1] ) ))
print("{ }    { }      { }".format( L[2],  Richter_Joules( L[2] ),    Richter_TNT( L[2] ) ))
print("{ }    { }      { }".format( L[3],  Richter_Joules( L[3] ),    Richter_TNT( L[3] ) ))
print("{ }    { }     { }".format( L[4],   Richter_Joules( L[4] ),    Richter_TNT( L[4] ) ))

print("\n")

r=float( input( "Please enter a Richter Scale Value : " ) )

print( "Richter Scale Value: ", r)
RJ = Richter_Joules(r)
RT = Richter_TNT(r)
print( "Equivalence in joule:", RJ)
print( "Equivalence in tons of TNT: ", RT)  

main()

该程序提供了一个选项,可以输入一个Richter比例值,但是当你输入一个值时,它会给出这个

我查了一个关键错误,我无法弄清楚我的错误是什么。任何帮助表示赞赏

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
c:\users\19ktw01\appdata\local\temp\tmpor5fg1.py in <module>()
     32 print( "Equivalence in tons of TNT: ", RT)
     33 
---> 34 main()

c:\users\19ktw01\appdata\local\temp\tmpor5fg1.py in main()
     16 
     17  print( "Richter       Joules                      TNT" )
---> 18  print("{ }    { }         { }".format( L[0],  Richter_Joules( L[0] ),  Richter_TNT( L[0] ) ))
     19  print("{ }    { }         { }".format( L[1],  Richter_Joules( L[1] ),  Richter_TNT( L[1] ) ))
     20  print("{ }    { }      { }".format( L[2],  Richter_Joules( L[2] ),    Richter_TNT( L[2] ) ))

KeyError: ' ' 

我做错了什么

1 个答案:

答案 0 :(得分:0)

删除大括号之间的空格,即

print("{}    {}         {}".format( L[0],  Richter_Joules( L[0] ),  Richter_TNT( L[0] ) ))