Question: (Python code): user input string: int A1=5. Need help fixing code. It only output the (key, [int]) Code below and output picture: def CutOneLineTokens(strs): Keywords
user input string: int A1=5.
Need help fixing code. It only output the (key, [int])
Code below and output picture:
def CutOneLineTokens(strs):
Keywords = (\"if\", \"else\", \"int\", \"float\")
Operator = \"=+>*\"
Separators = (\"(\", \")\", \":\", '', '', ';')
tokenDict = {\"key\": [], \"id\": [], \"op\": [], \"lit\": [], \"sep\": []}
l = len(strs)
x = 0
while (x
if strs[x] == '\"':
temp = strs.find('\"', x + 1)
tokenDict[\"sep\"].append(strs[x])
tokenDict[\"sep\"].append(strs[temp])
tokenDict[\"lit\"].append(strs[x:temp + 1:])
else:
temp = strs.find(\" \", x)
if temp == -1:
temp = l
if strs[x:temp:] in Operator:
tokenDict[\"op\"].append(strs[x:temp:])
elif strs[x:temp:] in Keywords:
tokenDict[\"key\"].append(strs[x:temp:])
elif strs[x:temp:] in Separators:
tokenDict[\"sep\"].append(strs[x:temp:])
elif strs[x].isalpha():
if strs[x + 1:temp:].isalpha() or strs[x + 1:temp:].isalnum(): # Identifiers
tokenDict[\"id\"].append(strs[x:temp:])
elif strs[x:temp:].isdigit():
tokenDict[\"lit\"].append(strs[x:temp:])
else:
temp2 = strs.find(\".\", x, temp)
if temp2 != -1:
if strs[x:temp2:].isdigit() and strs[temp2 + 1:temp:].isdigit():
tokenDict[\"lit\"].append(strs[x:temp:])
else:
if strs[x:temp:].isdigit():
tokenDict[\"lit\"].append(strs[x:temp:])
x = temp + 1
return tokenDict
USER_INPUT = CutOneLineTokens(input(\"Please enter one line expression : \"))
for x in USER_INPUT.items():
print(x)
My Output: key -> [int], id -> [A1], op -> [=], lit -> [5]
Correct Output:
Test input string: int A1=5
Output list: [, , , ],5>,=>,a1>,int>,>
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
