help with a function

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Lance Hoffmeyer

    #1

    help with a function

    Hey all,

    I'm new to python. I keep getting an error when running this.
    I'm sure there is an easy fix but I can't figure it out.
    What am I doing wrong? How do I fix it?

    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 "<interacti ve input>", line 1, in ?
    File "<interacti ve input>", line 3, in even_odd_round
    TypeError: int() can't convert non-string with explicit base[color=blue][color=green][color=darkred]
    >>>[/color][/color][/color]
  • Ben Finney

    #2
    Re: help with a function

    Lance Hoffmeyer <lance@augustma il.com> writes:
    [color=blue]
    > 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 "<interacti ve input>", line 1, in ?
    > File "<interacti ve input>", line 3, in even_odd_round
    > TypeError: int() can't convert non-string with explicit base[color=green][color=darkred]
    > >>>[/color][/color][/color]

    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 indistinguishab le from a |
    `\ feature." -- Rich Kulawiec |
    _o__) |
    Ben Finney

    Comment

    • Paul McGuire

      #3
      Re: help with a function

      "Lance Hoffmeyer" <lance@augustma il.com> wrote in message
      news:446a85c6$0 $61170$ae4e5890 @news.nationwid e.net...[color=blue]
      > Hey all,
      >
      > I'm new to python. I keep getting an error when running this.
      > I'm sure there is an easy fix but I can't figure it out.
      > What am I doing wrong? How do I fix it?
      >
      > 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 "<interacti ve input>", line 1, in ?
      > File "<interacti ve input>", line 3, in even_odd_round
      > TypeError: int() can't convert non-string with explicit base[color=green][color=darkred]
      > >>>[/color][/color][/color]

      1 def even_odd_round( num):
      2 if(round(num,2) + .5 == int(round(num,2 )) + 1):
      3 if(int(num,0) % 2): #an odd number
      4 rounded_num = round(num,2) + .1
      5 else: #an even number
      6 rounded_num = round(num,2) - .1
      7 rounded_num = int(rounded_num )
      8 return rounded_num
      9
      10 even_odd_round( 5.5)
      [color=blue]
      > Traceback (most recent call last):
      > File "<interacti ve input>", line 1, in ?
      > File "<interacti ve input>", line 3, in even_odd_round
      > TypeError: int() can't convert non-string with explicit base[/color]

      This


      Comment

      • Paul McGuire

        #4
        Re: help with a function

        "Paul McGuire" <ptmcg@austin.r r._bogus_.com> wrote in message
        news:mYvag.3795 2$CH2.9832@torn ado.texas.rr.co m...[color=blue]
        > "Lance Hoffmeyer" <lance@augustma il.com> wrote in message
        > news:446a85c6$0 $61170$ae4e5890 @news.nationwid e.net...[color=green]
        > > Hey all,
        > >
        > > I'm new to python. I keep getting an error when running this.
        > > I'm sure there is an easy fix but I can't figure it out.
        > > What am I doing wrong? How do I fix it?
        > >
        > > 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 "<interacti ve input>", line 1, in ?
        > > File "<interacti ve input>", line 3, in even_odd_round
        > > TypeError: int() can't convert non-string with explicit base[color=darkred]
        > > >>>[/color][/color]
        >
        > 1 def even_odd_round( num):
        > 2 if(round(num,2) + .5 == int(round(num,2 )) + 1):
        > 3 if(int(num,0) % 2): #an odd number
        > 4 rounded_num = round(num,2) + .1
        > 5 else: #an even number
        > 6 rounded_num = round(num,2) - .1
        > 7 rounded_num = int(rounded_num )
        > 8 return rounded_num
        > 9
        > 10 even_odd_round( 5.5)
        >[color=green]
        > > Traceback (most recent call last):
        > > File "<interacti ve input>", line 1, in ?
        > > File "<interacti ve input>", line 3, in even_odd_round
        > > TypeError: int() can't convert non-string with explicit base[/color]
        >[/color]

        This error message tells us there is a problem on line 3, something to do
        with the int() method call.

        You have posted a number of posts this week, and it feels like you write a
        bunch of code, run into a problem, then post it without trying to figure it
        out. I don't mean to offend, but really, many of your questions are pretty
        basic:
        - how do I use regex's (to extract data from an easily split string of
        space-delimited numbers)?
        - how do I round a number?
        - I get this traceback, what's wrong with my program?
        Please read up on the Python tutorials, and learn how to use the interactive
        help.

        Overall, c.l.py is pretty newbie-friendly, and a "n00b" question every so
        often is no big deal - but you've got to make more of an effort yourself.
        There is also a tutorial mailing list that is - I started to say "more
        geared for beginners", but we have plenty of lurker beginners on c.l.py, I'm
        sure - I'd say the tutorial mailing list is more willing to handhold new
        Python programmers.

        And a posted "thanks" once in a while is not bad etiquette either.

        -- Paul


        Comment

        Working...