Help please? TypeError: can't multiply sequence by non-int of type 'str'

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • yumiyay
    New Member
    • Sep 2014
    • 7

    Help please? TypeError: can't multiply sequence by non-int of type 'str'

    HELP please?
    Im new to python. I made this code but I keep getting:

    subtotal= 'shirts' * 'amount' * 0.20

    TypeError: can't multiply sequence by non-int of type 'str'



    the code is:



    shirts="12.99"

    amount=input("H ow many shirts would you like to purchase?")

    subtotal= 'shirts' * 'amount' * 0.20

    percentage= 'shirts' * 'amount' * 0.20

    total= 'subtotal' - 'percentage'
    Last edited by bvdet; Sep 6 '14, 03:04 PM. Reason: Removed reference to homework.
  • wordone
    New Member
    • Aug 2009
    • 9

    #2
    When you put quotes or single quotes around the numbers or words, it reads them as a string of letters, not numbers. Remove the quotes.
    Code:
    shirts=12.99
    
    amount=input("How many shirts would you like to purchase?")
    
    subtotal= shirts * amount * 0.20
    
    percentage= shirts * amount * 0.20
    
    total= subtotal - percentage
    
    print subtotal, percentage, total
    So the error is telling you that you can't use a string of letters to multiply with numbers. Whenever you put quote marks around something, it looks at it like it's a string.
    Last edited by wordone; Sep 6 '14, 12:38 AM. Reason: Added end comment.

    Comment

    • yumiyay
      New Member
      • Sep 2014
      • 7

      #3
      thank you. But i encountered another problem. doing so i get:
      How many shirts would you like to purchase?6


      Execution Successful!


      it still doenst give me the total I need

      Comment

      • wordone
        New Member
        • Aug 2009
        • 9

        #4
        Add a print statement at the end like in my example above.

        Comment

        • yumiyay
          New Member
          • Sep 2014
          • 7

          #5
          thank you again for the help. I really appreciate it.
          I did as you said, all i got back is:

          print subtotal, percentage, total

          ^

          SyntaxError: invalid syntax



          here is the code i put:

          shirts=12.99

          amount=input("H ow many shirts would you like to purchase?")

          subtotal= shirts * amount * 0.20

          percentage= shirts * amount * 0.20

          total= subtotal - percentage

          print subtotal, percentage, total


          My goal here is to get a total. Im trying to get the percentage to be subtracted from the subtotal to get that number. if it makes sense.

          Comment

          • wordone
            New Member
            • Aug 2009
            • 9

            #6
            Try just one print statement and see what it says:

            print subtotal

            Comment

            • yumiyay
              New Member
              • Sep 2014
              • 7

              #7
              thankyou.

              I tried
              print subtotal

              got:

              SyntaxError: invalid syntax

              Comment

              • wordone
                New Member
                • Aug 2009
                • 9

                #8
                Are you running this from IDLE or do you have it saved as a script?

                Comment

                • yumiyay
                  New Member
                  • Sep 2014
                  • 7

                  #9
                  Im trying them in both IDLE and Ninja

                  Comment

                  • wordone
                    New Member
                    • Aug 2009
                    • 9

                    #10
                    Wait....you're probably using a python 3.x version so you have to put parenthesis around things you want to print:

                    print (subtotal)

                    Comment

                    • yumiyay
                      New Member
                      • Sep 2014
                      • 7

                      #11
                      yep yep I am using python 3.x
                      and yep yep I have tried both with parenthesis and without.

                      I changed my code to this:

                      shirts=12.99

                      amount=float(in put("How many shirts would you like to purchase?"))

                      subtotal= shirts * amount * 0.20

                      percentage= shirts * amount * 0.20

                      total= subtotal - percentage

                      print (total)


                      when I print (subtotal) I get the right answer
                      when I print (percentage) I also get the right answer
                      but now when I print (total) I get 0.0 no matter what number I input when I run it to test it.

                      Comment

                      • yumiyay
                        New Member
                        • Sep 2014
                        • 7

                        #12
                        OHHHHHHHHHHHHHH H I FIXED IT!! YAY!!
                        I see what I was doing wrong

                        I had:

                        subtotal= shirts * amount * 0.20
                        percentage= shirts * amount * 0.20

                        when actually suppose to be:

                        subtotal= shirts * amount
                        percentage= shirts * amount * 0.20



                        oh gee I feel soooo sillyyyyy.
                        Thank you so much for taking time to help me. I highly appreciate it.

                        Comment

                        Working...