Dictionary problem(?)
- MrMojoRisin5
-
MrMojoRisin5
- Member since: Jun. 21, 2011
- Offline.
-
- Forum Stats
- Member
- Level 12
- Programmer
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?
- Diki
-
Diki
- Member since: Jan. 31, 2004
- Offline.
-
- Forum Stats
- Moderator
- Level 13
- Programmer
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
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.
- MrMojoRisin5
-
MrMojoRisin5
- Member since: Jun. 21, 2011
- Offline.
-
- Forum Stats
- Member
- Level 12
- Programmer
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.
- Diki
-
Diki
- Member since: Jan. 31, 2004
- Offline.
-
- Forum Stats
- Moderator
- Level 13
- Programmer
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)
> add(foo,bar)
> 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
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!!!
- MrMojoRisin5
-
MrMojoRisin5
- Member since: Jun. 21, 2011
- Offline.
-
- Forum Stats
- Member
- Level 12
- Programmer
On second thoughts though, I can't find a way to fix it, lol. Can you help me?
- MrMojoRisin5
-
MrMojoRisin5
- Member since: Jun. 21, 2011
- Offline.
-
- Forum Stats
- Member
- Level 12
- Programmer


