Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Is there anyway you guys can correct this. It states File main.py, line 28, in temp1 = d1[temp] KeyError: ' Expected output: Input String:

Is there anyway you guys can correct this. It states " File "main.py", line 28, in

temp1 = d1[temp]

KeyError: "'"

Expected output: Input String: characters in a dictionary in a dictionary in a tuple in a list in a dictionary in a string

This assignment is on to make an basic parser for an input string that is somewhat like Python.

Your program will read a data string, identify the control characters, and print out the overall structure of the string. Here's an example:

Input String: [{1}] Output: Number inside a dictionary inside a list 

  • The control characters you need to be concerned with are:, { } (dictionary), () (tuple), and ' " (string). Note that all string start with a single quote and end with a double quote (to reduce complexity in your solution).
  • The program must print out the structures starting with the innermost first and ending with the outermost
  • Once you start getting structure close characters (e.g. ] } " ) you can ignore any new start character structures (e.g. [ } ' )
  • You must also identify an errors in the closing order for the structures. Once you identify an error, print an Error message and stop processing
Input String: {[(abc)}] Output: Characters inside a tuple inside a ERROR 

My code is given below.

My Code:

s = input("Input String: ")

stack = []

c=0

c1=0

c2=0

for i in s:

if(i!=']' and i!=')' and i!='}' and i.isdigit()==False and i.isalpha()==False):

stack.append(i)

c1+=1

if(i=="]" or i=="}" or i==")"):

c2=c

break

c+=1

if(s[c1].isdigit()==True):

print("number ",end="")

elif(s[c1].isalpha()==True):

print("characters ",end ="")

d = {"[":"list ","{":"dictionary ","(":"tuple "}

d1 = {"[":"]","{":"}","(":")"}

while(len(stack)!=0):

temp = stack.pop()

temp1 = d1[temp]

if(s[c2]==temp1):

print("in a "+d[temp],end="")

else:

print("in a ERROR ",end="")

break

c2+=1

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Programming questions