August Williams - 7 months ago 26

Python Question

I am starting to use functions for my first time and the issue I am having is declaring a variable inside a function that is used more than once. I got this program working before my attempt to add functions, so the only thing incorrect (im assuming) is my attempt to use functions.

`print ("Welcome to August's binary arithemetic caclulator.")`

firstvalue = input("What is the first binary value?")

secondvalue = input("What is the second binary value?")

operation = input("What operation would you like to carry out? + or - or * or ^")

def bintoint():

prod, ans, power = 0, 0, 0

for i in range (-1,-len(firstvalue) - 1,-1):

try:

prod = ((int(firstvalue[i])) * ((2**power)))

except ValueError:

continue

ans += prod

prod = 0

power += 1

global ans

firstvalueans = ans

bintoint()

bintoint(firstvalue="secondvalue")

secondvalueans = ans

#prod, ans, power = 0, 0, 0

#for i in range (-1,-len(secondvalue) - 1,-1):

# try:

# prod = ((int(secondvalue[i])) * ((2**power)))

# except ValueError:

# continue

# ans += prod

# prod = 0

# power += 1

# global secondvalueans

# secondvalueans = ans

if operation == "+":

totalans = (firstvalueans + secondvalueans)

if operation == ("-"):

totalans = (firstvalueans - secondvalueans)

if operation == ("*"):

totalans = (firstvalueans * secondvalueans)

if operation == ("^"):

totalans = (firstvalueans ** secondvalueans)

try:

totalans = int(totalans)

except NameError:

print ("Please enter a valid operator.")

import sys

sys.exit()

invertedbinary = []

while totalans >= 1:

totalans = (totalans/2)

invertedbinary.append(totalans)

totalans = int(totalans)

for n,i in enumerate(invertedbinary):

if (round(i) == i):

invertedbinary[n]=0

else:

invertedbinary[n]=1

if (firstvalue[0] == "-") ^ (secondvalue[0] == "-"):

invertedbinary.append("-")

invertedbinary.reverse()

result = ''.join(str(e) for e in invertedbinary)

print ( firstvalue , operation , secondvalue , "=" ,result)

Note the only declared function, and the block of commented code. The code is identical with the exception of a single variable. So I am trying to execute the function twice, by changing the only difference the variable. The variable I would like to change can be seen as

`firstvalue`

`def bintoint()`

`firstvalue`

The idea of this script is to take two binary values, convert them into integers, carry out the respective operation between the two integers, convert back into binary and

`print ( firstvalue , operation , secondvalue , "=" ,result)`

So lets say I input the two values in this order:

`100`

`011`

`*`

Expected Output:

`100 * 011 = 1100`

Actual Output:

`TypeError: bintoint() got an unexpected keyword argument 'firstvalue'`

So I understand it is something wrong with my attempt at changing the variable within the function.

`bintoint(firstvalue="secondvalue")`

What am I doing wrong?

Answer

Two important aspects of using functions are passing in arguments and returning the results. If you pass in an argument, then the function will receive a potentially different value each time it is called. If you return the result, then the calling code will receive a different answer each time it calls your function.

Try something like this:

```
def bintoint(value):
prod, ans, power = 0, 0, 0
for i in range (-1,-len(value) - 1,-1):
try:
prod = ((int(value[i])) * ((2**power)))
except ValueError:
continue
ans += prod
prod = 0
power += 1
return ans
```

Then, in your main code:

```
firstvalueans = bintoint(firstvalue)
secondvalueans = bintoint(secondvalue)
```

More information about functions can be found in The Python Tutorial, in the *Defining Functions* section.

*Aside*: In your example, it may be easier to use the two-argument form of the `int()`

constructor, like so:

```
firstvalueans=int(firstvalue, 2)
```

Source (Stackoverflow)