list - Python Dictionary Type -


i'm trying figure out why type error. possible put integers inside of dictionaries?

math_questions = [     {'question1':'1*1',     'answer1':1,     'quote1' :'what are,you accident of birth; am,i myself.\n there , thousand princes; there 1 beethoven.'},     {'question2':'2*1',     'answer2':2,     'quote2': 'two company, 3 crowd'},     {'question3': '3*1',     'answer3': 3,     'quote3': 'there 3 types of people, can count , cannot'}     ]  # read txt file later???  print math_questions[0]['question1']  math_answer = int(raw_input("what answer " + math_questions["question1"] +"? : "))  if math_answer == math_questions['answer1']:     print math_questions['quote'] else:     print "try again" print math_questions['answer1']  

this error message get.

ps c:\python27\math_game> python math_game.py 1*1 traceback (most recent call last):   file "math_game.py", line 17, in <module>     math_answer = int(raw_input("what answer " + math_questions["question1"] +"? : ")) typeerror: list indices must integers, not str ps c:\python27\math_game> 

thanks in advance.

when access list, need index. looks trying access dict. instead, put:

math_answer = int(raw_input("what answer " + math_questions[0]["question1"] +"? : ")) 

you had few errors:

  1. you had math_questions["question1"] on line 17, 19, 20, 23
  2. you had math_questions["quote"] didn't exist (i changed math_questions["quote1"])

over here, try access list of dicts way used. however, need strip just dict before access way.

>>> obj = [{'data1': 68, ... 'data2': 34, ... 'data3': 79}] >>> obj['data2'] traceback (most recent call last):   file "<stdin>", line 1, in <module> typeerror: list indices must integers, not str >>> obj[0]['data2'] 34 >>>  

here updated code:

math_questions = [     {'question1':'1*1',     'answer1':1,     'quote1' :'what are,you accident of birth; am,i myself.\n there , thousand princes; there 1 beethoven.'},     {'question2':'2*1',     'answer2':2,     'quote2': 'two company, 3 crowd'},     {'question3': '3*1',     'answer3': 3,     'quote3': 'there 3 types of people, can count , cannot'}     ]  # read txt file later???  print math_questions[0]['question1']  math_answer = int(raw_input("what answer " + math_questions[0]["question1"] +"? : "))  if math_answer == math_questions[0]['answer1']:     print math_questions[0]['quote1'] else:     print "try again" print math_questions[0]['answer1']  

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -