unsupported operand error?

This is the place for queries that don't fit in any of the other categories.

unsupported operand error?

Postby Seanliu » Sat Apr 05, 2014 8:49 pm

Hello! Its ME again! The budding self-taught programmer! I was developing a calculator, and hit some (very, very hard to break) rocks (it is a pun for bugs, duh!)!

1. (This is not really a bug) I don't know how to structure the code for a memory system!

2. The (fun?) Random Equations system is not working! it says:

Code: Select all
Traceback (most recent call last):
  File "/Users/Seanliu/Desktop/InfinityFighter/Other Stuff/CalculatorV1.py", line 108, in <module>
    main()
  File "/Users/Seanliu/Desktop/InfinityFighter/Other Stuff/CalculatorV1.py", line 86, in main
    randomE()
  File "/Users/Seanliu/Desktop/InfinityFighter/Other Stuff/CalculatorV1.py", line 50, in randomE
    print(ranNum1+'-'+ranNum2,"=", ranNum1-ranNum2)
TypeError: unsupported operand type(s) for +: 'int' and 'str'


Hmm. Very long, and each bit as nasty-looking as the other. From now on I will refer to the text as 'The Red text of evil'. Catchy, eh? Oh yea, please also rate by code!
Code: Select all
import time, random
print('WELCOME TO THE CALCULATOR!')
print('Made by Sean Liu.')
print('this program has no memory. It might be implemented in V2.')
print('UPDATE: Memory has been implemented, but has no use.')
ans = ''

print()

equationList = [1,2,3,4]
equationOp = random.choice(equationList)

def add():

    addN1 = float(input('What is your first number? >>'))
    addN2 = float(input('What is your second number? >>'))
    print('The first number add the second number is...', addN1 + addN2)
    ans = addN1+addN2

def takeaway():
    subN1 = float(input('What is your first number? >>'))
    subN2 = float(input('What is your second number? >>'))
    print('The first number add the second number is...', subN1 - subN2)
    ans = subN1 - subN2

def times ():
    timesN1 = float(input('What is your first number? >>'))
    timesN2 = float(input('What is your second number? >>'))
    print('The first number add the second number is...', timesN1 * timesN2)
    ans = timesN1*timesN2

def divide():
    divN1 = float(input('What is your first number? >>'))
    divN2 = float(input('What is your second number? >>'))
    print('The first number add the second number is...', divN1 / divN2)
    ans = divN1/divN2

def randomE():
    equationOp = random.choice(equationList)
    ranNum1 = random.randint(0, 100000 )
    ranNum2 = random.randint(0, 100000)
    if(equationOp == 1):
        print(ranNum1+'+'+ranNum2,"=", ranNum1+ranNum2)
    elif(equationOp == 2):
        print(ranNum1+'x'+ranNum2,"=", ranNum1*ranNum2)
    elif(equationOp == 3):
        print(ranNum1+'/'+ranNum2,"=", ranNum1/ranNum2)
    elif(equationOp == 4):
        print(ranNum1+'-'+ranNum2,"=", ranNum1-ranNum2)
    else:
        print('Error 2.')



def main():
    print()
    print()
    print()
    print()
    print('''MENU:
         (A)dd
         (S)ubtract
         (M)ultiplication
         (D)ivision
         (Q)uit
         (R)andom Equation (Whole Numbers)''')
    menuChoice = input('         >>').lower()

    if (menuChoice == 'a'):
        add()
        again()
    elif (menuChoice == 's'):
        takeaway()
        again()
    elif (menuChoice == 'm'):
        times()
        again()
    elif(menuChoice == 'd'):
        divide()
        again()
    elif(menuChoice == 'q'):
        print('Come back soon!')
        quit()
    elif(menuChoice == 'r'):
        randomE()
    else:
        print('Do this properly! Restarting...')
        main()


def again():
    print('Do again? Y/N')
    goAgain = input('>>').lower()
    if (goAgain == 'y'):
        print('All Righty! Redirecting...')
        for (i) in range (5):
            time.sleep(0.2)
            print('Redirecting!')
        time.sleep(1)
        main()
    elif(goAgain == 'n'):
        print('Ok! See ya!')
        quit()
    else:
        print('Error 1: Unknown Entry. Please Re-Enter Choice.')
        again()
main()
again(


Last edited by Yoriz on Sun Apr 06, 2014 12:10 am, edited 2 times in total.
Reason: Added code tags to the traceback, Changed title.
The CoDeR!


--Sean Liu
User avatar
Seanliu
 
Posts: 29
Joined: Sun Mar 30, 2014 4:03 pm

Re: Code Not Working! Calculator

Postby stranac » Sat Apr 05, 2014 10:42 pm

Code: Select all
    print(ranNum1+'-'+ranNum2,"=", ranNum1-ranNum2)

You are trying to add to numbers and a string together.
There are a few possible solutions:
  • Just separate them with commas instead of adding
  • Use string formatting
  • Convert the numbers to strings using str() (I wouldn't recommend using this one)
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1144
Joined: Thu Feb 07, 2013 3:42 pm

Re: unsupported operand error?

Postby Seanliu » Sun Apr 06, 2014 11:30 am

Thanks! I used the commas instead of the plus signs, and it is much easier to read, not to mention it works! But I really want to implement memory. How would I do that?
The CoDeR!


--Sean Liu
User avatar
Seanliu
 
Posts: 29
Joined: Sun Mar 30, 2014 4:03 pm

Re: unsupported operand error?

Postby stranac » Sun Apr 06, 2014 11:32 am

Seanliu wrote:Thanks! I used the commas instead of the plus signs, and it is much easier to read, not to mention it works! But I really want to implement memory. How would I do that?

Don't know. What does "implement memory" mean?
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1144
Joined: Thu Feb 07, 2013 3:42 pm

Re: unsupported operand error?

Postby Seanliu » Sun Apr 06, 2014 3:05 pm

Like on a calculator when you punch in :

52 + 45

then you punch in

Ans + 34

and then = the answer is 131.
Last edited by Mekire on Sun Apr 06, 2014 4:20 pm, edited 1 time in total.
Reason: Quote removed.
The CoDeR!


--Sean Liu
User avatar
Seanliu
 
Posts: 29
Joined: Sun Mar 30, 2014 4:03 pm

Re: unsupported operand error?

Postby stranac » Sun Apr 06, 2014 4:30 pm

You'll have to return values from your functions, if you want to reuse your results, you will have to return them from the functions.
When you want to use the result in another function, just pass it to that function.

It's pretty hard to do this with your current program structure, so I would suggest changing it so that your functions take arguments, instead of asking the user for numbers.
Then you can just use the previous result if the user enters 'Ans'.

Something like this:
Code: Select all
def main():
    result = 0
    while True:
        print('''MENU:
             (A)dd
             (S)ubtract
             (M)ultiplication
             (D)ivision
             (Q)uit
             (R)andom Equation (Whole Numbers)''')
        menuChoice = input('         >>').lower()

        if menuChoice == 'q':
            print('Come back soon!')
            return

        elif menuChoice == 'r':
            result = do_random_stuff()

        else:
            a = input('What is your first number? >>')
            b = input('What is your second number? >>')
            # use result if it's Ans, or convert to float
            a = result if a == 'Ans' else float(a)
            b = result if b == 'Ans' else float(b)

            if menuChoice == 'a':
                result = add(a, b)
            elif menuChoice == 's':
                result = subtract(a, b)
            elif menuChoice == 'm':
                result = multiply(a, b)
            elif menuChoice == 'd':
                result = divide(a, b)

        # display the result either here, or in your functions
        # I would probably do it here
        print('The result is', result)
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1144
Joined: Thu Feb 07, 2013 3:42 pm

Re: unsupported operand error?

Postby Seanliu » Sun Apr 06, 2014 9:53 pm

Thanks stracnac! Great help! You should check out my new post on the Challenges section!
The CoDeR!


--Sean Liu
User avatar
Seanliu
 
Posts: 29
Joined: Sun Mar 30, 2014 4:03 pm

Re: unsupported operand error?

Postby Mekire » Sun Apr 06, 2014 11:50 pm

No offense, but I have deleted your post from the challenges section. This is something that you want implemented, but don't yet have the knowledge to implement. This doesn't qualify as a challenge. Please first work on your own skills before trying to throw out the gauntlet to others.

-Mek
User avatar
Mekire
 
Posts: 988
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan


Return to General Coding Help

Who is online

Users browsing this forum: Google [Bot] and 5 guests