0

I am still new to Python and programming and StackOverflow, please excuse my lack of proper coding vocab. My problem is the code below. I can run both while True loops just fine, but as soon as it reaches the code under the comment #PROBLEM STARTS HERE, I have no idea what exactly am I doing wrong. The error message I got is unsupported operand type(s) for /: 'str' and 'int'.

gameName = str(input('Enter game title: '))

while True:
    basePrice = input('Enter the base price: ')
    try:
        check = float(basePrice)
        break
    except ValueError:
        print('Please enter a valid price.')
    continue

while True:
    discountPtg = input('Enter the discount(%): ')
    try:
        check = float(discountPtg)
        break
    except ValueError:
        print ('Please enter just a number for your discount.')

#PROBLEM STARTS HERE    
disctdPrice = (discountPtg / 100) * basePrice

savedCost = basePrice - disctdPrice

print ('\nSTATS\n')

print (gameName)
print ('Base price: RM' + str(basePrice))
print ('Discount: ' + str(discountPtg) + '%')
print ('Discounted price: ' + str(disctdPrice))
print ('You saved: ' + str(savedCost))

Any help will be very appreciated.

clyve
  • 13
  • 1
  • Does this answer your question? [How can I read inputs as numbers?](https://stackoverflow.com/questions/20449427/how-can-i-read-inputs-as-numbers) – G. Anderson Apr 28 '20 at 18:12
  • You know that `discountPtg` is a string (because you cast it to a `float` when assigning to `check`), so why are you using it in an arithmetic expression? – Scott Hunter Apr 28 '20 at 18:13
  • You need to do: basePrice = float(basePrice) and discountPtg = float(discountPtg) – quamrana Apr 28 '20 at 18:15

2 Answers2

1

You checked to see if it is a number, but you never converted it to one. On this line:

disctdPrice = (discountPtg / 100) * basePrice

DicountPtg should be turned into a float like this:

disctdPrice = (float(discountPtg) / 100) * basePrice
Holden
  • 632
  • 4
  • 15
  • Thanks, I have made several fixes based on your reply. They are as follows: ``` disctdPrice = (float(discountPtg)/100) * float(basePrice) savedCost = float(basePrice) - float(disctdPrice) ``` – clyve Apr 28 '20 at 18:19
0

Thanks everyone. I have figured it out by making these modifications:

disctdPrice = (float(discountPtg)/100) * float(basePrice)
savedCost = float(basePrice) - float(disctdPrice)
clyve
  • 13
  • 1