## Dictionary problem(?)

• 268 Views
• 7 Replies
MrMojoRisin5
MrMojoRisin5
• Member since: Jun. 21, 2011
• Offline.
Forum Stats
Member
Level 12
Programmer
Dictionary problem(?) 2013-06-20 10:59:07

So, I made a REPL in Python...and, I map my commands to some functions. For example, I have a function that adds a value to a variable (syntax: "add(myvar, 5)", adds 5 to "myvar") (variables are created with the "newvar" command, which takes as arguments a name and a value. It then stores the name in a list, the value in another and zips them together in a dictionary). Now, I want to make it so the second argument of the "add" command can be a value or another variable. I wrote some code but it doesn't work:

``````def add(varname, num):
if varname in variables:
if num in variables:
variables[varname] = int(variables[varname]) + int(variables[num])
else:
variables[varname] = int(variables[varname]) + int(num)
else:
return "Error: There is no variable \"" + str(varname) + "\"."``````

Does anybody know why?

-Got to keep on risin'...
-Check out FlashFrame
-Check out my YouTube channel and my blog

Diki
Diki
• Member since: Jan. 31, 2004
• Offline.
Forum Stats
Moderator
Level 13
Programmer
Response to Dictionary problem(?) 2013-06-20 12:33:15

Can be a little more specific than "it doesn't work"? What does it do? Looks like it should work to me, so the problem is probably elsewhere.

At 6/20/13 10:59 AM, MrMojoRisin5 wrote: variables[varname] = int(variables[varname]) + int(variables[num])

You don't need to be converting variables[varname] to an int because it's already an int. You could also shorten that slightly by using the += operator:

``variables[varname] += int(variables[num])``

That adds the given value on the right to the variable on the left.

At 6/20/13 10:59 AM, MrMojoRisin5 wrote: return "Error: There is no variable \"" + str(varname) + "\"."

You could simplify this a bit by using string formatting, which can be done two different ways:

``return 'Error: There is no variable "%s".' % varname``
``return 'Error: There is no variable "{:s}".'.format(varname)``

All they do is insert values into strings.

Also note that I used single quotes (') instead of double ones ("). This allows you to use the double quotes inside the string without escaping them (and vice versa).

You can read more about string.format here, as well as the syntax for it. I can't link to the documentation on the % operator because I can't find where it is.

It's really up to you which one you want to use since they really do the same thing, but I personally prefer, and recommend, the string.format option because it's more customisable and easier to read.

MrMojoRisin5
MrMojoRisin5
• Member since: Jun. 21, 2011
• Offline.
Forum Stats
Member
Level 12
Programmer
Response to Dictionary problem(?) 2013-06-20 16:50:41

I mean that, when I have two variables and I add them together, for some reason it thinks that the nested if is false, which means that the second argument of the "add" function is a variable. That, of course, isn't the case, so when it tries to make an integer out of the variable name it throws an error.

-Got to keep on risin'...
-Check out FlashFrame
-Check out my YouTube channel and my blog

MrMojoRisin5
MrMojoRisin5
• Member since: Jun. 21, 2011
• Offline.
Forum Stats
Member
Level 12
Programmer
Response to Dictionary problem(?) 2013-06-20 16:55:47

What I'm trying to say is that when I try to add two variables together, for some reason it can't find the second one in the variables dictionary and it treats it like a number.

-Got to keep on risin'...
-Check out FlashFrame
-Check out my YouTube channel and my blog

Diki
Diki
• Member since: Jan. 31, 2004
• Offline.
Forum Stats
Moderator
Level 13
Programmer
Response to Dictionary problem(?) 2013-06-20 20:02:45

Do you have a copy of your current source code? I grabbed the version from your blog here, and put your new add() function code in and it worked fine:

``````> newvar(foo,42)

> newvar(bar,10)

> showvar(foo)
foo : 52``````

You must have changed something somewhere else that is causing the problem.

MrMojoRisin5
MrMojoRisin5
• Member since: Jun. 21, 2011
• Offline.
Forum Stats
Member
Level 12
Programmer
Response to Dictionary problem(?) 2013-06-21 04:29:27

Oh, I see where the error is. When you use the add function as "add(foo,bar)" it works fine, but when you use it as "add(foo, bar)" it throws an error, because of the space between the comma and "bar". This is easy to fix. Thanks for your help!!!

-Got to keep on risin'...
-Check out FlashFrame
-Check out my YouTube channel and my blog

MrMojoRisin5
MrMojoRisin5
• Member since: Jun. 21, 2011
• Offline.
Forum Stats
Member
Level 12
Programmer
Response to Dictionary problem(?) 2013-06-21 04:36:08

On second thoughts though, I can't find a way to fix it, lol. Can you help me?

-Got to keep on risin'...
-Check out FlashFrame
-Check out my YouTube channel and my blog

MrMojoRisin5
MrMojoRisin5
• Member since: Jun. 21, 2011
• Offline.
Forum Stats
Member
Level 12
Programmer
Response to Dictionary problem(?) 2013-06-21 05:19:11

Nevermind, fixed it!

-Got to keep on risin'...
-Check out FlashFrame
-Check out my YouTube channel and my blog