Python Forum
TypeError: can't multiply sequence by non-int of type 'str' - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: TypeError: can't multiply sequence by non-int of type 'str' (/thread-5526.html)

Pages: 1 2


TypeError: can't multiply sequence by non-int of type 'str' - Beatenberg - Oct-09-2017

Hello again, this time a type error is appearing with the following error (scroll down to the bottom of the post). The aim of this part is for the package to be multiplied by the discount then to be printed out in Pass_Total(). E.g. Pass_Calculate() 7 * 0.6 =  "The total cost is: $ 4.2". I tried to recreate/replicate other code that was similar to this one but did not get any closer. 

def Pass_Show():
    print("Aussie Pass!")
    print("Please select a package and discount depending on age.")
    print("E.g. Package = 7")
    Pass_Package()

def Pass_Package():
    print("7 Day Pass – $139.95")
    print("14 Day Pass - $239.95")
    print("30 Day Pass - $500.00")
    Pass_Discount()

def Pass_Discount():
    print("Children (Under 15) - 60% off")
    print("Students (Over 15 & less than 22) - 40% off")
    print("Seniors (60 or over) - 50% off")
    Pass_Cost()

def Pass_Total():
    cost = Pass_Cost()
    total = Pass_Calculate(cost)
    print("The total cost is: $" + str(total))

def Pass_Cost():#User inputs the type of package they suit and discount
    cost=list()
    package = input('Package: ')
    if package == '7' or '14' or '30': #If they enter those values they will move onto choosing their discount
        print("Proceed...")
        cost.append(package)
        
    if package not in('7','14','30'):
        Pass_Cost()#If they do not enter any of those values they will be prompted to ask again 
    
    p = int(package)
    


    discount = input("Discount: ")     
    if discount == "Children":
        discount == 0.60
        cost.append(discount)
        Pass_Calculate(cost)
    if discount == "Students":
        discount == 0.50
        cost.append(discount)
        Pass_Calculate(cost)
    if discount == "Seniors":
        discount == 0.40
        cost.append(discount)
        Pass_Calculate(cost)
    if discount not in ("Children","Students","Seniors"):
        Pass_Cost()

    d = int(discount)
    
    return p, d
    
    
def Pass_Calculate(cost):
    return (cost[0] * cost[1]) #Calculates the cost from their chosen package and discount e.g. 7 * 0.6
                              #7 being the chosen package (7 Day Pass) and 0.6 being Children discount 




Pass_Show()

Traceback (most recent call last):
  File "F:\Computing\Term 3\Aussie Pass.py", line 67, in <module>
    Pass_Show()
  File "F:\Computing\Term 3\Aussie Pass.py", line 6, in Pass_Show
    Pass_Package()
  File "F:\Computing\Term 3\Aussie Pass.py", line 12, in Pass_Package
    Pass_Discount()
  File "F:\Computing\Term 3\Aussie Pass.py", line 18, in Pass_Discount
    Pass_Cost()
  File "F:\Computing\Term 3\Aussie Pass.py", line 43, in Pass_Cost
    Pass_Calculate(cost)
  File "F:\Computing\Term 3\Aussie Pass.py", line 61, in Pass_Calculate
    return (cost[0] * cost[1]) #Calculates the cost from their chosen package and discount e.g. 7 * 0.6
TypeError: can't multiply sequence by non-int of type 'str'


RE: TypeError: can't multiply sequence by non-int of type 'str' - metulburr - Oct-09-2017

read the BBcode link in my signature


RE: TypeError: can't multiply sequence by non-int of type 'str' - Beatenberg - Oct-09-2017

def Pass_Show():
    print("Aussie Pass!")
    print("Please select a package and discount depending on age.")
    print("E.g. Package = 7")
    Pass_Package()

def Pass_Package():
    print("7 Day Pass – $139.95")
    print("14 Day Pass - $239.95")
    print("30 Day Pass - $500.00")
    Pass_Discount()

def Pass_Discount():
    print("Children (Under 15) - 60% off")
    print("Students (Over 15 & less than 22) - 40% off")
    print("Seniors (60 or over) - 50% off")
    Pass_Cost()

def Pass_Total():
    cost = Pass_Cost()
    total = Pass_Calculate(cost)
    print("The total cost is: $" + str(total))

def Pass_Cost():#User inputs the type of package they suit and discount
    cost=list()
    package = input('Package: ')
    if package == '7' or '14' or '30': #If they enter those values they will move onto choosing their discount
        print("Proceed...")
        cost.append(package)
        
    if package not in('7','14','30'):
        Pass_Cost()#If they do not enter any of those values they will be prompted to ask again 
    
    p = int(package)
    


    discount = input("Discount: ")     
    if discount == "Children":
        discount == 0.60
        cost.append(discount)
        Pass_Calculate(cost)
    if discount == "Students":
        discount == 0.50
        cost.append(discount)
        Pass_Calculate(cost)
    if discount == "Seniors":
        discount == 0.40
        cost.append(discount)
        Pass_Calculate(cost)
    if discount not in ("Children","Students","Seniors"):
        Pass_Cost()

    d = int(discount)
    
    return p, d
    
    
def Pass_Calculate(cost):
    return (cost[0] * cost[1]) #Calculates the cost from their chosen package and discount e.g. 7 * 0.6
                              #7 being the chosen package (7 Day Pass) and 0.6 being Children discount 
Error:
Traceback (most recent call last): File "F:\Computing\Term 3\Aussie Pass.py", line 67, in <module> Pass_Show() File "F:\Computing\Term 3\Aussie Pass.py", line 6, in Pass_Show Pass_Package() File "F:\Computing\Term 3\Aussie Pass.py", line 12, in Pass_Package Pass_Discount() File "F:\Computing\Term 3\Aussie Pass.py", line 18, in Pass_Discount Pass_Cost() File "F:\Computing\Term 3\Aussie Pass.py", line 43, in Pass_Cost Pass_Calculate(cost) File "F:\Computing\Term 3\Aussie Pass.py", line 61, in Pass_Calculate return (cost[0] * cost[1]) #Calculates the cost from their chosen package and discount e.g. 7 * 0.6 TypeError: can't multiply sequence by non-int of type 'str'



RE: TypeError: can't multiply sequence by non-int of type 'str' - j.crater - Oct-09-2017

I have not really analysed your code, but clearly there is a mismatch with types when multiplying "cost[0] * cost[1]", such as multiplying a numeric variable with a string. If you are unsure what happens on that line, you can print cost[0] and cost[1] before the multiplication/error.


RE: TypeError: can't multiply sequence by non-int of type 'str' - Beatenberg - Oct-09-2017

I printed cost[0] and cost[1] in the same function. This was the result and the code again with the change
Error:
Traceback (most recent call last): File "F:\Computing\Term 3\Aussie Pass.py", line 67, in <module> Pass_Show() File "F:\Computing\Term 3\Aussie Pass.py", line 6, in Pass_Show Pass_Package() File "F:\Computing\Term 3\Aussie Pass.py", line 12, in Pass_Package Pass_Discount() File "F:\Computing\Term 3\Aussie Pass.py", line 18, in Pass_Discount Pass_Cost() File "F:\Computing\Term 3\Aussie Pass.py", line 55, in Pass_Cost d = int(discount) ValueError: invalid literal for int() with base 10: 'Children'
def Pass_Show():
    print("Aussie Pass!")
    print("Please select a package and discount depending on age.")
    print("E.g. Package = 7")
    Pass_Package()

def Pass_Package():
    print("7 Day Pass – $139.95")
    print("14 Day Pass - $239.95")
    print("30 Day Pass - $500.00")
    Pass_Discount()

def Pass_Discount():
    print("Children (Under 15) - 60% off")
    print("Students (Over 15 & less than 22) - 40% off")
    print("Seniors (60 or over) - 50% off")
    Pass_Cost()

def Pass_Total():
    cost = Pass_Cost()
    total = Pass_Calculate(cost)
    print("The total cost is: $" + str(total))

def Pass_Cost():#User inputs the type of package they suit and discount
    cost=list()
    package = input('Package: ')
    if package == '7' or '14' or '30': #If they enter those values they will move onto choosing their discount
        print("Proceed...")
        cost.append(package)
        
    if package not in('7','14','30'):
        Pass_Cost()#If they do not enter any of those values they will be prompted to ask again 
    
    p = int(package)
    


    discount = input("Discount: ")     
    if discount == "Children":
        discount == 0.60
        cost.append(discount)
        Pass_Calculate(cost)
    if discount == "Students":
        discount == 0.50
        cost.append(discount)
        Pass_Calculate(cost)
    if discount == "Seniors":
        discount == 0.40
        cost.append(discount)
        Pass_Calculate(cost)
    if discount not in ("Children","Students","Seniors"):
        Pass_Cost()

    d = int(discount)
    
    return p, d
    
    
def Pass_Calculate(cost):
    print(cost[0]) #Calculates the cost from their chosen package and discount e.g. 7 * 0.6
    print(cost[1])                          #7 being the chosen package (7 Day Pass) and 0.6 being Children discount 




Pass_Show()



RE: TypeError: can't multiply sequence by non-int of type 'str' - metulburr - Oct-09-2017

Quote:
discount == 0.60
This isnt doing what you think it is
== is comparison while = is assignment


RE: TypeError: can't multiply sequence by non-int of type 'str' - Beatenberg - Oct-09-2017

Great works but another error.
Error:
Traceback (most recent call last): File "F:\Computing\Term 3\Aussie Pass.py", line 67, in <module> Pass_Show() File "F:\Computing\Term 3\Aussie Pass.py", line 6, in Pass_Show Pass_Package() File "F:\Computing\Term 3\Aussie Pass.py", line 12, in Pass_Package Pass_Discount() File "F:\Computing\Term 3\Aussie Pass.py", line 18, in Pass_Discount Pass_Cost() File "F:\Computing\Term 3\Aussie Pass.py", line 33, in Pass_Cost Pass_Cost()#If they do not enter any of those values they will be prompted to ask again File "F:\Computing\Term 3\Aussie Pass.py", line 43, in Pass_Cost Pass_Calculate(cost) File "F:\Computing\Term 3\Aussie Pass.py", line 61, in Pass_Calculate return cost[0] *cost[1] #Calculates the cost from their chosen package and discount e.g. 7 * 0.6 TypeError: can't multiply sequence by non-int of type 'float'



RE: TypeError: can't multiply sequence by non-int of type 'str' - sparkz_alot - Oct-09-2017

Remember that input() will return a string and you cannot multiply strings. Either start with a numeric type (ie integer or float) int(input()) or float(input()), or convert when ready

>>> a_list = ['7', '8']
>>> a_list[0] * a_list[1]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can't multiply sequence by non-int of type 'str'
>>>
>>> int(a_list[0]) * int(a_list[1])
56
>>>



RE: TypeError: can't multiply sequence by non-int of type 'str' - Beatenberg - Oct-10-2017

What about with float? The full error is:

Error:
Traceback (most recent call last): File "F:\Computing\Term 3\Aussie Pass.py", line 67, in <module> Pass_Show() File "F:\Computing\Term 3\Aussie Pass.py", line 6, in Pass_Show Pass_Package() File "F:\Computing\Term 3\Aussie Pass.py", line 12, in Pass_Package Pass_Discount() File "F:\Computing\Term 3\Aussie Pass.py", line 18, in Pass_Discount Pass_Cost() File "F:\Computing\Term 3\Aussie Pass.py", line 33, in Pass_Cost Pass_Cost()#If they do not enter any of those values they will be prompted to ask again File "F:\Computing\Term 3\Aussie Pass.py", line 43, in Pass_Cost Pass_Calculate(cost) File "F:\Computing\Term 3\Aussie Pass.py", line 61, in Pass_Calculate return cost[0] *cost[1] #Calculates the cost from their chosen package and discount e.g. 7 * 0.6 TypeError: can't multiply sequence by non-int of type 'float'



RE: TypeError: can't multiply sequence by non-int of type 'str' - ichabod801 - Oct-10-2017

You can only multiply sequences by integers. That's why the error says 'by non-int'.