0
import easygui as eg 
import sys

version = 'Percentage Calculator'

Options = [ 'Percantage Increase', 'Percentage Decrease' ]

button = eg.buttonbox ('What would you like to calculate', title = version, choices =     Options)

if button == Options [0]:
    enter = eg.enterbox ('Please enter a number between 0 and 999.', title = version,     strip=False)
    if enter < '0' or enter > '999':
            eg.msgbox ('please choose a number between 0 and 999.', title = version,     ok_button='OK')
            sys.exit()
    enter2 = eg.enterbox ('please enter a number between 0 and 999, that is bigger than     the first number.', title = version, strip=False)
    if enter2 < '0' or enter2 > '999':
            eg.msgbox ('please choose a number between 0 and 999.', title = version,     ok_button='OK')
            sys.exit()
    elif enter2 < var enter:
            eg.msgbox ('please choose a number bigger than the first number.', title =     version, ok_button='OK')
            sys.exit()
    Subtract = enter2 - enter
    print (Subtract)

this is my code... as you can see, near the bottom, it says 'enter2 - enter' i want this to subtract the second number that the user put in from the first number but it just says...

Traceback (most recent call last):
  File "C:\Users\olitr_000\Desktop\PYTHON\Percentage Calculator\Percentage     Calculator.py", line 22, in <module>
    Subtract = enter2 - enter
TypeError: unsupported operand type(s) for -: 'str' and 'str'

what can i do so that say enter 1 equals 55 and enter2 = 198 how would i get it so that it does 198 - 55?!

please help, i you need any more information please notify me.

2
  • You need to cast the inputs to int as they are string by default. int(enter) and int(enter2) Commented Jun 22, 2014 at 8:18
  • Note that there are more issues with the code. such as enter2 < '0' or enter2 > '999' checks wont work properly. You would have to convert to int before these checks Commented Jun 22, 2014 at 8:29

4 Answers 4

2

You cant subtract two string, cast them first to integers.

Subtract = int(enter2) - int(enter)
Sign up to request clarification or add additional context in comments.

Comments

1

your problem is that by using the eg.enterbox function, the numbers the user inputs will be seen as strings. To fix this, we send the 'string' the enterbox returns, to the int() function. This will translate the string of numbers to an actual integer that can be used to subtract other integers.

eg.

enter = "3"
enter = int(enter)
enter2 = "4" #This is an example of what the variable looks like coming from the enterbox
enter2 = int(enter2) #enter2 now looks like 4 instead of "4"

output = enter2 - enter #this will return 1 to the variable output

Hope this helps ;)

oh, and you don't need the quotation marks in this line:

if enter < '0' or enter > '999':

as you are saying that '0' is a string, and not to be treated like a number ;)

Comments

0

This will help you.

import easygui as eg 
import sys

version = 'Percentage Calculator'

Options = [ 'Percantage Increase', 'Percentage Decrease' ]

button = eg.buttonbox ('What would you like to calculate', title = version, choices =     Options)

if button == Options [0]:
    enter = eg.enterbox ('Please enter a number between 0 and 999.', title = version,     strip=False)
    if enter < '0' or enter > '999':
            eg.msgbox ('please choose a number between 0 and 999.', title = version,     ok_button='OK')
            sys.exit()
    enter2 = eg.enterbox ('please enter a number between 0 and 999, that is bigger than     the first number.', title = version, strip=False)
    if enter2 < '0' or enter2 > '999':
            eg.msgbox ('please choose a number between 0 and 999.', title = version,     ok_button='OK')
            sys.exit()
    elif enter2 < var enter:
            eg.msgbox ('please choose a number bigger than the first number.', title =     version, ok_button='OK')
            sys.exit()
    Subtract = int(enter2) - int(enter)
    print (Subtract)

Comments

0

I think it is just parsing problem you are facing here, and as we knows Python doesn't accept except string values from keyboard as there is no variable type definition in its code, that is why we need always to cast or parse the input to its appropriate type.

>>> a = "3.14"
>>> float(a)
3.14
>>> int(float(a))
3

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.