Why does my code for adding two numbers within my minipc project not work?


Repl link:


code snippet
```import BRModules.BRMEM
def add2(a,b):
	BRModules.BRMEM.mem0 = int(input(a))
	BRModules.BRMEM.mem1 = int(input(b))
	BRModules.BRMEM.mem0 = str(BRModules.BRMEM.mem0)
	BRModules.BRMEM.mem1 = str(BRModules.BRMEM.mem1)
1 Like

Actually, it does work, just not as intended. By using the input function, you are asking the user for their input. So, what your program does now is prompts input by displaying a and b rather than using a and b as numbers. After answering the input, it does its math. To fix this, take away the input part (which also makes int unnecessary).

def add2(a,b):
	BRModules.BRMEM.mem0 = a
	BRModules.BRMEM.mem1 = b
	BRModules.BRMEM.mem0 = str(BRModules.BRMEM.mem0)
	BRModules.BRMEM.mem1 = str(BRModules.BRMEM.mem1)

The question is, why does the BRMEMFUNC module print everything that’s in memory? It makes the output confusing. 2 is the result of add2(1,1), but the rest is just confusing. For storing variables like you seem to be trying to do, you might be better off to store them in a text file.
EDIT: I got variables stored in text files working. Either use this code, or run in Replit shell pip install coderelijah, then add to your Py file from CoderElijah import addStorage, accessStorage. Basically, addStorage writes data to a text file, and accessStorage retrieves data from a given line of a text file. However, you can’t (at least with these functions), write to a specific line in a text file, so you would want to make a separate file for each variable. You also have to create the text file beforehand. There are only three reasons I know of that you would want to store a variable in a text file:

  1. To archive data
  2. To make a variable to use across different files
  3. Because you’re having trouble with global variables.
def addStorage(fileName, store, append=''):
  if append in ('append', 'a', 'add'):
    file = open(str(fileName), 'a')
    file = open(str(fileName), 'w')
def accessStorage(fileName, lineNum=1):
  lineNum = int(lineNum) - 1
  file = open(str(fileName))
  contents = file.readlines()
  return contents[lineNum].strip('\n')

thanks! sorry if the code looks odd, this is my first time trying something like this.

1 Like

oof, that’s a stupid mistake on my part!

1 Like

If I solved your problem, please mark this topic as “solved”. I’ve made mistakes in coding too.


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.