Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 514281
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T07:30:30+00:00 2026-05-13T07:30:30+00:00

I am a noob to Python and have not had any luck figuring this

  • 0

I am a noob to Python and have not had any luck figuring this out. I want to be able to keep the tax variable in the code so it would be easily updated should it change. I have experimented with different means but was only able to get it to skip the print tax line and print the same values for the total and subtotal. How do I multiply the tax variable by sum(items_count)? Here is the code:

   items_count = []
tax = float(.06)
y = 0

count = raw_input('How many items do you have? ')

while count > 0:
    price = float(raw_input('Please enter the price of your item: '))
    items_count.append(price)
    count = int(count) - 1

print 'The subtotal of your items is: ' '$%.2f' % sum(items_count)
print 'The amount of sales tax is: ' '$%.2f' % sum(items_count) * tax
total = (sum(items_count) * tax) + sum(items_count)
print 'The total of your items is: ' '$%.2f' % total
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T07:30:31+00:00Added an answer on May 13, 2026 at 7:30 am

    It would help if you provide the back-trace for the error. I ran your code, and got this back-trace:

    Traceback (most recent call last):
      File "t.py", line 13, in <module>
        print 'The amount of sales tax is: ' '$%.2f' % sum(items_count) * tax
    TypeError: can't multiply sequence by non-int of type 'float'
    

    The answer is that this is a precedence problem. If you just did this:

    sum(items_count) * tax
    

    it would work, but because you have the expression with the string and the % operator, the call to sum() is tied to the string, and effectively you have:

    <string_value> * tax
    

    The solution is to add parentheses to force the precedence you want:

    print 'The amount of sales tax is: ' '$%.2f' % (sum(items_count) * tax)
    

    Here is documentation of operator precedence in Python.

    http://docs.python.org/reference/expressions.html#summary

    Note that % has the same precedence as *, so the order is then controlled by the left-to-right rule. Thus, the string and the call to sum() are connected with the % operator, and you are left with <string_value> * tax.

    Note that instead of parentheses, you could also use an explicit temporary:

    items_tax = sum(items_count) * tax
    print 'The amount of sales tax is: ' '$%.2f' % items_tax
    

    When you aren’t sure what is going on, sometimes it’s a good idea to start using explicit temporary variables, and check to see that each one is set to the value you were expecting.

    P.S. You don’t actually need all the calls to float(). The value 0.06 is already a float value, so it is sufficient to just say:

    tax = 0.06
    

    I like to put the initial zero on fractions, but you can use either of tax = 0.06 or tax = .06, it doesn’t matter.

    I like how you convert the prices to float by wrapping the raw_input() call in float(). I suggest that you should do the same thing for count, wrap the raw_input() call in int() to get an int value. Then the later expression can simply be

    count -= 1
    

    It’s a bit tricky that count is initially set to a string and then re-bound later. If a silly or crazy user enters an invalid count, int() will raise an exception; it is better if the exception happens right away, right on the call to raw_input(), rather than later in a seemingly simple expression.

    And of course you aren’t using y for anything in your code sample.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 410k
  • Answers 410k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You assigned the received data as $rows, but you are… May 15, 2026 at 7:34 am
  • Editorial Team
    Editorial Team added an answer That didn't exist for EJB until 3.1. With EJB 3.1… May 15, 2026 at 7:34 am
  • Editorial Team
    Editorial Team added an answer I would be curious to know the reason your aversion… May 15, 2026 at 7:34 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.