Python Forum

Full Version: Need Help with an Error I'm Getting (Very New to this)
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Hello, sorry if this isn't explained well or I'm doing something wrong, I am extremely new to python coding and I need help with an assignment in my coding class. We are making a program that calculates a discount depending on the amount of packages you purchased. Here is the prompt and my code, along with the error I keep receiving.

THE PROMPT
A software company sells a package that retails for $99. Quantity discounts are given according to the following table:

Quantity Discount
10-19 20%
20-49 30%
50-99 40%
100 or more 50%

Design a program that asks the user to enter the number of packages purchased. The program should then display the amount of the discount (if any) and the total amount of the purchase after the discount.


MY CODE

quantity=int(input("Please enter the amount of packages you purchased."))
if quantity >= "100":
discount = 50
else:
if quantity >= "50":
discount = 40
else:
if quantity >= "20":
discount = 30
else:
if quantity >= "10":
discount = 20
else:
discount = 0
print('Your discount is' & discount & ' percent off.')
totaldiscount = discount / 100
discountprice = totaldiscount * 99
finalprice = 99 - discountprice
print("The total amount of the purchase after the discount is $" & finalprice)


THE ERROR MESSAGE

TypeError: '>=' not supported between instances of 'int' and 'str'
>>>


Thank you so much for helping me out, I really appreciate it
Your error message should also have line number information. That will direct you to where in your code the error is happening.

When you put elements in quotes, they are read as strings. Some of the numbers in your code are numbers and some are strings. Why have you put quotes around some of the numbers?
(Nov-03-2020, 12:06 AM)bowlofred Wrote: [ -> ]Your error message should also have line number information. That will direct you to where in your code the error is happening.

When you put elements in quotes, they are read as strings. Some of the numbers in your code are numbers and some are strings. Why have you put quotes around some of the numbers?

Idk, I thought that was how it was supposed to be done. I took away the quotes and I still get the error message directing me towards line 3. Here's the full thing;

Traceback (most recent call last):
File "C:\Users\myname\Downloads\Homework 6.py", line 2, in <module>
if quantity >= 100:
TypeError: '>=' not supported between instances of 'str' and 'int'
>>>
"100" is a string. Quantity is an int. Like the error says, can't compare a string and an int.
(Nov-03-2020, 12:23 AM)jefsummers Wrote: [ -> ]"100" is a string. Quantity is an int. Like the error says, can't compare a string and an int.

Thank you! That fixed that issue, but it also brought to light a new one. I got an error message that reads:

Traceback (most recent call last):
File "C:\Users\myname\Downloads\Homework 6.py", line 17, in <module>
print('Your discount is' & discount & ' percent off.')
TypeError: unsupported operand type(s) for &: 'str' and 'int'
>>>

Any idea what this means?
Please use the "python" bbcode tags (or use the python button above the editor).

You seem to be trying to use & to concatenate strings. You can instead use + for that, but discount isn't a string.

You probably instead should use f-strings to assemble your printout. Something like:

print(f'Your discount is {discount} percent off.')
(Nov-03-2020, 12:41 AM)bowlofred Wrote: [ -> ]Please use the "python" bbcode tags (or use the python button above the editor).

You seem to be trying to use & to concatenate strings. You can instead use + for that, but discount isn't a string.

You probably instead should use f-strings to assemble your printout. Something like:

print(f'Your discount is {discount} percent off.')


quantity=str(input('Please enter the amount of packages you purchased.'))

if quantity >= 100:
        discount = 50
else:
    if quantity >= 50:
            discount = 40
    else:
        if quantity >= 20:
                discount = 30
        else: 
            if quantity >= 10:
                    discount = 20
            else:
                discount = 0
                
print(f'Your discount is {discount} percent off.')
totaldiscount = discount / 100
discountprice = totaldiscount * 99
finalprice = 99 - discountprice
print('The total amount of the purchase after the discount is $' + finalprice)
That is currently the code I have, and I'm still getting this error message:

Error:
Traceback (most recent call last): File "C:\Users\jdeegan3\Downloads\Homework 6-1.py", line 3, in <module> if quantity >= 100: TypeError: '>=' not supported between instances of 'str' and 'int'
In your original code you were casting the input to an int:

quantity=int(input("Please enter the amount of packages you purchased."))
In your latest one, you cast it to a str (which it already is):

quantity=str(input('Please enter the amount of packages you purchased.'))
But you can't numerically compare a str to an int. I don't think anyone suggested changing that to a str. You should leave it an int.

Rather than successive ifs, you can use elif for disjoint tests. Then all the tests will line up and you don't have to continually indent.

if quantity >= 100:
    discount = 50
elif quantity >= 50:
    discount = 40
...
Once you fix and get quantity to be a number, a trick you can use to get that cascade of ifs into a single line would be
discount = 20*(quantity>=10) + 10*(quantity>=20) + 10*(quantity>=30) + 10*(quantity>=40) + 10*(quantity>=50)
This takes advantage of boolean True being equivalent to 1 and boolean False being equivalent to 0

A shorter approach still would be
discount = 20*(quantity>10) + 10*int((quantity-10)/10)
This takes advantage of the discounts going up by the same increment
I think you are overwhelmed by the project because it looks like you are thrashing around and accomplishing nothing. This happens to all programmers, the only difference is how big things have to get before you reach your breaking point.

The way to solve the problem is to solve a problem. Pick one thing your program has to do and solve that problem. I am going to solve this problem:
quantity=int(input("Please enter the amount of packages you purchased."))
print(quantity >= "100")
When I run this program and type in 50 it throws an exception you are familiar with.
Error:
TypeError: '>=' not supported between instances of 'int' and 'str'
I only have two lines of code, so solving this error should be easy. Where am I using '>=' and what are the two things I am comparing. This is the error line:
print(quantity >= "100")
Which of these is an 'int' and which is a 'str'. I think I know but I am going to ask Python to be sure.
quantity=int(input("Please enter the amount of packages you purchased."))
print('quantity is a', type(quantity))
print('100 is a', type("100"))
print(quantity >= "100")
Output:
Please enter the amount of packages you purchased.50 quantity is a <class 'int'> 100 is a <class 'str'> Traceback (most recent call last):
I didn't fix my error, but I didn't expect it to be fixed. But I did get the info I was looking for. 'quantity' is indeed an integer variable and '"100"' is indeed a string. I decide to change "100" from a string to an integer by removing the quotes.
quantity=int(input("Please enter the amount of packages you purchased."))
print(quantity >= 100)
I enter '50' (no quotes) and the program prints 'False' (no quotes). Success!

Next lets see if I can get the discount. I expand my tiny program to add in the part that decides which discount to use. I now know that the numbers in the comparisons should be numbers and not numbers inside quotes.
quantity = int(input("Quantity: "))

discount = 0
if quantity >= 100:
    discount = 50
elif quantity >= 50:
    discount = 40
elif quantity >= 20:
    discount = 30
elif quantity >= 10:
    discount = 20

print(discount)
I run this several times entering different quantities and verifying the correct discount is printed.

I continue using this pattern. Add a small bit of functionality and test. Add a small bit and test. Repeat over and over until done.

By always adding small bits I an never overwhelmed. Each of the bits is pretty easy to write and test. By testing each added bit I know when an error crops up. If my code was working but stopped when I added the last bit, the last bit is likely the problem.
Pages: 1 2