Python Help

Discussion in 'OT Technology' started by SPACECATAZ, Sep 8, 2008.

  1. SPACECATAZ

    SPACECATAZ New Member

    Joined:
    Dec 22, 2006
    Messages:
    2,502
    Likes Received:
    0
    I need help with two programs:

    1. I have to write a Python program that asks a user to enter a list of numbers in [ ] format. It then must count and display how many of those numbers in the list are between 16 and 22 inclusive. My prompt should not have a = sign in it, but my legend should end with a = sign.

    This is what I have so far:

    number1 = 16
    number2 = 22
    number = 0


    while True:
    mynum = raw_input("Enter a series of numbers in [ ]")
    mynum = int(mynum)
    if mynum >= number1 and mynum <= number2:
    number = number + 1

    print number


    But whenever I enter a numbers such as [8,9,10,11,16,22]

    I get back:

    ValueError: invalid literal for int() with base 10: '[8,9,10,11,16,22]'

    How do I get about doing this?



    2. The second program wants me to write a Python program that counts how many times the word 'red' appears when typed into the console by the user. The user may type several words per line, but will not type any punctuation.

    The program should print the result all on one line like this:

    Appearances: n

    where n represents the answer.

    This is what I have so far:

    from coninput import ConsoleInput
    cin = ConsoleInput()

    number = 0


    while True:
    myword = cin.getWord()
    if myword == "red":
    number = number + 1
    print "Appearances:", number

    The coninput is a another python program that let me use user input so ignore that.

    Whenever I type in red on a line like this:

    red red

    It'll print out like this:

    Appearances: 1
    Appearances: 2

    When it should be:

    Appearances: 2

    If I take the print statement out of the loop, then nothing will show up.

    I just started learning Python. Go easy on me. :hs:
     

Share This Page