Thread: Big confusion

    #1
  1. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2004
    Location
    There where the rabbits jump
    Posts
    556
    Rep Power
    11

    Big confusion


    Hi
    Here I am with another problem!!!
    I attached the script to save space. I'll show a part though.
    I have a use program to run this script since it is functions.


    Code:
    count = 0
    relist = []
    if "." in value:
          for integer in str(value):
               if integer == ".":
                   string.replace(integer,".","")
                   print "This is a float(decimal point number)."
               relist.append(integer)
               if "." not in integer:
                   count = count + 1
           print "There are",count,"integers in this sequence."
    This is the out put of this part:


    Code:
           What do u want to do?
    
           This is a float(decimal point number).
    
           There are 4 integers in this sequence.
    
           There are 9 integers in this sequence.

    It does everything right except for the last line(There are 9 integers in this sequence.)

    Input was (45.45).

    I need help and I would like it if you help me.
    I don't seem to be get it.

    Thanks
    Attached Files
  2. #2
  3. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Hey monkey,

    it would help if you told us what output you want from the function. From what I can see you want to count the number of integers in the users input; printing 'This is a float(decimal point number).' if the number contains a decimal point.

    If this is the case then you could do something like this:

    Code:
    >>> userInput = raw_input('Enter a Number. ')
    Enter a Number. 45.45
    >>> count = 0                   
    >>> for character in userInput:
    ...     if character.isdigit(): count += 1
    ... 
    >>> count
    4
    >>>
    This won't tell you if the number is a float but it will give you the correct number of digits in the string. In any case you will probably need to do some kind of check to see if the user entered a valid value or not .

    The main problem with your script seems to be that you're not resetting the count variable. Also, in the last part of the function you forget not to count the '.' in the string; this could have something to do with the call to string.replace() earlier in the function, which you're forgetting to assign to the value variable.

    Note: you shouldn't be using the string module for this. Instead use the Pythons built-in string methods .

    Hope this helps,

    Mark.
    programming language development: www.netytan.com Hula

  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2004
    Location
    There where the rabbits jump
    Posts
    556
    Rep Power
    11
    Hey good but I did it so that i get some expirence with the string module
  6. #4
  7. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Slight problem there: the string module has been depreciated, or rather the vast majority of the sting module has.

    If you check out the documentation for the string module you'll see what I mean. My point being that you shouldn't use functions/modules that Python has depreciated.

    This is a recipe for disaster IMO,

    Mark.
    programming language development: www.netytan.com Hula

  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2004
    Location
    There where the rabbits jump
    Posts
    556
    Rep Power
    11
    One addition to this thread...

    How do I do a for loop on the second thing in a dictionary.
    G = {"Gello":"Hello"}
    How can I reach "Hello" with a for loop

    And is there a isdigit() thing for characters(letters)
  10. #6
  11. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Python doesn't have a separate char type, instead a character is just a string containing a single letter; so you can use any of Pythons string methods on your "characters" .

    Here's an example of how to iterate over each character for each value in a Dictionary.

    Code:
    >>> aDict = {'hello': 'monkey'}
    >>> for key in aDict:
    ...     for character in aDict[key]:
    ...         print character
    ... 
    m
    o
    n
    k
    e
    y
    >>>
    As you can see it's pretty simple, you simply iterate over each key and use that key to access the value you want to loop with.

    For a full list of all the string methods available & a description of what they do you can use the help() function, like this:

    Code:
    >>> help(str)
    Take care,

    Mark.
    programming language development: www.netytan.com Hula

  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2005
    Posts
    78
    Rep Power
    10
    Originally Posted by monkeyman23555
    Code:
    count = 0
    relist = []
    if "." in value:
          for integer in str(value):
               if integer == ".":
                   string.replace(integer,".","")
                   print "This is a float(decimal point number)."
               relist.append(integer)
               if "." not in integer:
                   count = count + 1
           print "There are",count,"integers in this sequence."
    You should be using the len() function, and value.split() [since value is a string it can be split]... use the help() function at the command line - 'help(len)' or 'help("".split)' - to see their usage. Your finished code if you use them will be _much_ prettier (and less buggy.

    -T.

IMN logo majestic logo threadwatch logo seochat tools logo