dictionary - Python throws SyntaxError on this code -
while 1: dic = {} #empty dictionary used storing data dic[raw_input("enter value want store: ")] = input("enter access key of value: ") ans = raw_input("exit:e ; store variable : s; acces variable: a") if ans=="e": break; #exit main loop elif ans == "s": continue; elif ans=="a": pass;
please help
you using input()
instead of raw_input()
; interprets input python expression. easy make throw syntaxerror exception:
>>> input("enter sentence: ") enter sentence: quick brown fox traceback (most recent call last): file "<stdin>", line 1, in <module> file "<string>", line 1 quick brown fox ^ syntaxerror: invalid syntax
use raw_input()
throughout instead:
dic[raw_input("enter value want store: ")] = raw_input("enter access key of value: ")
you want turn these 2 questions around:
dic[raw_input("enter access key of value: ")] = raw_input("enter value want store: ")
python ask value first. if need ask key first, store in separate variable first:
key = raw_input("enter access key of value: ") dic[key] = raw_input("enter value want store: ")
Comments
Post a Comment