[Tutor] 'int' object has no attribute 'items'
Steven D'Aprano
steve at pearwood.info
Sat Sep 3 21:35:27 EDT 2016
Hi Chidinma,
I'm afraid it is very difficult for me to understand your code, because
your email program (Yahoo mail perhaps?) has mangled the code and put it
all on one single line:
On Sat, Sep 03, 2016 at 09:45:17PM +0000, Chidinma via Tutor wrote:
> def calculate_tax(dict_inp): result = {} if dict_inp == {}: result = "Please enter valid inputs" else: for k, v in dict_inp.items(): try: x = int(dict_inp[k]) except ValueError: print("That's not an int!") break if(x): if x > 50000: tax = ((x - 50000) * 0.3) + 4812.5 + 2110 + 1530 + 900 result[k] = tax elif x > 30750: tax = ((x - 30750) * 0.25) + 2110 + 1530 + 900 result[k] = tax elif x > 20200: tax = ((x - 20200) * 0.2) + 1530 + 900 result[k] = tax elif x > 10000: tax = ((x - 10000) * 0.15) + 900 result[k] = tax elif x > 1000: tax = ((x - 1000) * 0.1) result[k] = tax else: tax = 0 result[k] = tax else: print("Yearly income is not an integer") return result dict_inp = {'Alex': 500,'James': 20500,'Kinuthia': 70000}#dict_inp = {200: 1500,300: 20500,400: 70000}print(calculate_tax(dict_inp))
You may be able to prevent that by turning of "formatted text", or "rich
text", or "HTML email", or whatever your email program calls this
feature.
> But I get the result:
>
> THERE IS AN ERROR/BUG IN YOUR CODE
How are you running this? Python doesn't normally print "THERE IS AN
ERROR/BUG IN YOUR CODE". My guess is that you are using one of the
on-line Python courses where you type your code into the web page. Am I
right? Which one?
> Results: Internal Error: runTests aborted: TestOutcomeEvent(handled=False, test=, result=, outcome='error', exc_info=(, AttributeError("'int' object has no attribute 'items'",), ), reason=None, expected=False, shortLabel=None, longLabel=None) is not JSON serializable{'James': 2490.0, 'Alex': 0, 'Kinuthia': 15352.5}
That error doesn't seem to have anything to do with your code. Are you
sure it is connected to the code you give above?
If you are using a website, it might be a bug in the website.
> But when i take away the .items(), i get:
> for k, v in dict_inp:
> ValueError: too many values to unpack
The first thing to confirm that dict_inp is a dict. Run:
print( isinstance(dict_inp, dict) )
just before that "for ..." line. If it prints True, then change the for
line to:
for k, v in dict_inp.items():
What happens then?
More information about the Tutor
mailing list