0

Im' trying to make a program that is a tip calculator with graphics. I'm trying to do the output but I keep getting this error:

Traceback (most recent call last):
   File "C:/Users/simps/Desktop/tip calculator.py", line 30, in <module>
    float (taxsum = check / 4)
TypeError: unsupported operand type(s) for /: 'str' and 'int'

For the life of me I can't figure out what this means, This is my code if that helps

#A program that allows people to enter information and calculate how much a tip should be
from graphics import *
import math
#setting up the window
win = GraphWin("Tip Calculator", 400, 400)
win.setBackground("teal")
#setting up the input
checksub = Text(Point(150, 150,), "What is the subtotal of the check?:")
checksub.draw(win)
checksub2 = Entry(Point(300,152), 5)
checksub2.draw(win)

tiprate = Text(Point(150,190), "What is the tip rate?:")
tiprate.draw(win)
tiprate2 = Entry(Point(250,190), 3)
tiprate2.draw(win)
#button
Buttontext = Text(Point(150,210), "Compute")
Buttontext.draw(win)
Buttonbox = Rectangle(Point(100,230),Point(200,199))
Buttonbox.draw(win)
#Calculations
win.getMouse()
tip = tiprate2.getText()
check = checksub2.getText()
float (taxsum = check / 4)
float (checktax = taxsum + check)
float (tipsum = checktax / tip)
float (checksum = (tipsum + checktax))
#presenting the output
tipoutput = Text(Point(150,250), "The tip rate is: tipsum")
tipoutput.draw(win)
checkoutput = Text(Point(150,260), "The Check total is: checksum")
checkoutput.draw(win)
martineau
  • 119,623
  • 25
  • 170
  • 301
Simpson
  • 37
  • 1
  • 4
  • 3
    That error message is *very* clear. You can't divide a string by an integer. In your case, `check` is a string. You need to convert it to an `int` or a `float` first. For instance, you could do: `check = float(checksub2.getText())` – pault Feb 06 '18 at 23:13
  • `float (taxsum = check / 4)` → `taxsum = float(check) / 4` – Andrea Corbellini Feb 06 '18 at 23:19
  • Oh okay, thanks that makes more sense – Simpson Feb 06 '18 at 23:22

2 Answers2

1

You need to convert the variable check from a string to a float or an int, you can do it like this

int: taxsum = int(check) / 4

float: taxsum = float(check) / 4

bhazero025
  • 337
  • 4
  • 15
0

Convert check to an int or float and then divide. It should start working then.

carstenbauer
  • 9,817
  • 1
  • 27
  • 40
Shouv
  • 47
  • 4
  • 1
    Thanks for posting your first answer. You could improve your answer by explicitly demonstrating the change, i.e. quoting the problematic code line and proposing a fix. Also, as this has already been done in the comments, you could/should refer to those as well. – carstenbauer Feb 06 '18 at 23:51
  • Thanks a lot. I will definitely refer to it from next time – Shouv Feb 07 '18 at 02:35