Re: help with a function



Lance Hoffmeyer <lance@xxxxxxxxxxxxxx> writes:

def even_odd_round(num):
if(round(num,2) + .5 == int(round(num,2)) + 1):
if(int(num,0) % 2): #an odd number
rounded_num = round(num,2) + .1
else: #an even number
rounded_num = round(num,2) - .1
rounded_num = int(rounded_num)
return rounded_num

even_odd_round(5.5)

Traceback (most recent call last):
File "<interactive input>", line 1, in ?
File "<interactive input>", line 3, in even_odd_round
TypeError: int() can't convert non-string with explicit base


Two problems.

One is the simple fact reported by the error message: you're
attempting to specify that a number object (already represented inside
Python as a number) should be converted using a particular base. (Why
you've chosen 0 as the base is beyond me.)

That only makes sense with a string of digit characters, where Python
needs to be told what numeric base each digit is (decimal, octal,
hexadecimal, whatever). It makes no sense for objects that are already
numbers, since they're not represented as digits in a base.

The other problem is specifying a base at all. Why not just convert
the object to an int using the default base?

If you're confused about how built-in types or functions work, you
should become familiar with the help system in the interpreter:

help(int)

--
\ "Any sufficiently advanced bug is indistinguishable from a |
`\ feature." -- Rich Kulawiec |
_o__) |
Ben Finney

.



Relevant Pages

  • Re: Non Continuous Subsequences
    ... v1) original eager Python+Psyco version ... v6) lazy D version with my libs ... lazy Python version, no allocations ... int n = len; ...
    (comp.lang.python)
  • Ref: Toni Erdmann - GCC difference in size of long
    ... It seems unfair to me that the response Must go to the group you have ... order to compile it. ... error signal caused by a single misplaced int is particularly hard to find. ... You can also specify the architecture of the target. ...
    (alt.os.linux.suse)
  • Re: Why is java considered a language for "web" or "internet" programming?
    ... I don't remember reading exact things about Perl interpreter, ... load and parse bytecodes. ... I think, integer in Perl or Python or other scripting languages is some structure, with at the least field for type and value, and pointer to this structure you hold in 'i' variable. ... const int NNUM = 1000000; ...
    (comp.lang.java.help)
  • Re: Official definition of call-by-value (Re: Finding the instance reference...)
    ... Yes, which is why we have int, str, and list classes which, unlike some ... Since pickle can't predict what aspects of the ... you can't do so in Python. ... with a foo attribute. ...
    (comp.lang.python)
  • Re: My first Python program
    ... it's a reasonably expressive language which has a good mix between staying ... My first project was to replace a shell script with a Python script. ... The input is a list of C function declarations, ... int foo{ ...
    (comp.lang.python)