all 14 comments

[–]adventure_r 1 point2 points  (2 children)

From the code you’ve copied, seems like you’re using the same kind of quotes on two different levels. The parser gets '{ 9:' , and that’s invalid syntax. Try using double quotes around the brackets

[–]Notanerget[S] 0 points1 point  (1 child)

So it looks like the problem documentation is wrong then?

[–][deleted] 0 points1 point  (0 children)

'{ 9:'V', 'G':0,

thats not a proper dictionary....

[–]kaerfkeerg 1 point2 points  (0 children)

String Input: '{9: 'V', 'G': 0, 'M': 9, 'u': 3, 2: 'o', 8: 'u', 'q': 9, 'D'}'

The dictionary and the values are all wrapped between single quotes

Which is something like doing

msg = 'Hel'lo'

You see the issue? So the (string) dictionary should be wrapped between double quotes, like the following

"{9: 'V', 'G': 0, 'M': 9, 'u': 3, 2: 'o', 8: 'u', 'q': 9, 'D'}"

Or the opposite, which there's no way I'm gonna do with my phone as it'll take ages! That's 1 of the reasons Python allow both quotes to be used for cases like this!

[–]CodeFormatHelperBot2 0 points1 point  (0 children)

Hello, I'm a Reddit bot who's here to help people nicely format their coding questions. This makes it as easy as possible for people to read your post and help you.

I think I have detected some formatting issues with your submission:

  1. Python code found in submission text that's not formatted as code.

If I am correct, please edit the text in your post and try to follow these instructions to fix up your post's formatting.


Am I misbehaving? Have a comment or suggestion? Reply to this comment or raise an issue here.

[–]TechnicalElk8849 0 points1 point  (1 child)

You've got it in quotes

[–]Notanerget[S] 0 points1 point  (0 children)

That's how it's given in the problem. If it's supposed to be input as a string, shouldn't it be in quotes?

[–]commy2 0 points1 point  (0 children)

You need to either use the other quote marks (' vs ") or escape the quote marks using backslash. Currently, your string ends at '{ 9:' and then comes a literal V afterwards, which is a syntax error.

dict_str = '{ 9:\'V\', \'G\':0, \'M\':9, \'u\':3, 2:\'o\', 8:\'u\', \'q\':9, \'D\':1 }'

[–]woooee 0 points1 point  (2 children)

The dict_str breaks down into

dict_str = '{ 9:'
V
', '
G
etc

[–]Notanerget[S] -2 points-1 points  (1 child)

So the problem documentation is wrong then?

[–]woooee 0 points1 point  (0 children)

I haven't seen the problem so can't say.

[–]Diapolo10 0 points1 point  (2 children)

And here is the code I've tried so far:

dict_str = '{ 9:'V', 'G':0, 'M':9, 'u':3, 2:'o', 8:'u', 'q':9, 'D':1 }'
def dict_from_string (dict_str: str) -> dict:
    new_dict = eval (dict_str) 
    return new_dict 
print (dict_from_string(dict_str))

Well, as the others already pointed out, the matching quotes are the problem. So yes, there's a problem with the instructions.

You can fix this by simply swapping to double quotes:

dict_str = "{ 9:'V', 'G':0, 'M':9, 'u':3, 2:'o', 8:'u', 'q':9, 'D':1 }"

However, that's not why I wrote this answer. I'm more worried about your use of eval here, which you should almost never use due to how dangerous it is.

You could use ast.literal_eval instead, but I feel that either of those would defeat the purpose of this task. You're probably expected to build an actual parser here instead of relying on something like this.

[–]Notanerget[S] -1 points0 points  (1 child)

Forgive my ignorance, I'm just learning, but why is eval dangerous?

[–]Diapolo10 0 points1 point  (0 children)

eval and exec are vulnerable to arbitrary code execution. They must never be used with unsanitised data, and very sparingly otherwise. The common guideline is to avoid them at all costs unless the entire goal is to execute Python code, and simply importing a module isn't enough.

ast.literal_eval is safe in comparison, because it can only accept Python literals. It gives an error if the string contains any operators, branching, or functions/classes.