What's wrong with this code?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Nathan Pinno

    What's wrong with this code?


    Hi all,

    What's wrong with the following code? It says there is name error, that
    random is not defined. How do I fix it?

    # Plays the guessing game higher or lower.
    # Originally written by Josh Cogliati, improved first by Quique, then by
    Nathan Pinno.
    print "Higher or Lower"
    print
    number = random.choice(r ange(100))
    guess = 0
    while guess != number:
    guess = input("Guess a number: ")
    if guess > number:
    print "Too high"
    guess = input("Guess a number: ")
    elif guess < number:
    print "Too low"
    guess = input("Guess a number: ")
    print "Just right"

    Thanks.
    Nathan Pinno




    --


    ----------------------------------------------------------------
    Posted via UsenetRevolutio n.com - Revolutionary Usenet
    ** HIGH RETENTION ** Specializing in Large Binaries Downloads **

  • Sébastien Boisgérault

    #2
    Re: What's wrong with this code?



    Nathan Pinno a écrit :[color=blue]
    > Hi all,
    >
    > What's wrong with the following code? It says there is name error, that
    > random is not defined. How do I fix it?[/color]

    Add "import random" at the top of your file

    Cheers,

    SB
    [color=blue]
    > # Plays the guessing game higher or lower.
    > # Originally written by Josh Cogliati, improved first by Quique, then by
    > Nathan Pinno.
    > print "Higher or Lower"
    > print
    > number = random.choice(r ange(100))
    > guess = 0
    > while guess != number:
    > guess = input("Guess a number: ")
    > if guess > number:
    > print "Too high"
    > guess = input("Guess a number: ")
    > elif guess < number:
    > print "Too low"
    > guess = input("Guess a number: ")
    > print "Just right"
    >
    > Thanks.
    > Nathan Pinno
    > http://www.npinnowebsite.ca/
    >
    >
    >
    > --
    >
    >
    > ----------------------------------------------------------------
    > Posted via UsenetRevolutio n.com - Revolutionary Usenet
    > ** HIGH RETENTION ** Specializing in Large Binaries Downloads **
    > http://www.UsenetRevolution.com[/color]

    Comment

    • Robert Kern

      #3
      Re: What's wrong with this code?

      Nathan Pinno wrote:[color=blue]
      > Hi all,
      >
      > What's wrong with the following code? It says there is name error, that
      > random is not defined. How do I fix it?[/color]

      You need to import random.

      --
      Robert Kern
      rkern@ucsd.edu

      "In the fields of hell where the grass grows high
      Are the graves of dreams allowed to die."
      -- Richard Harter

      Comment

      • Tim Roberts

        #4
        Re: What's wrong with this code?

        "Nathan Pinno" <falcon3166@hot mail.com> wrote:[color=blue]
        >
        > Hi all,
        >
        > What's wrong with the following code? It says there is name error, that
        >random is not defined. How do I fix it?
        >
        > # Plays the guessing game higher or lower.
        > # Originally written by Josh Cogliati, improved first by Quique, then by
        >Nathan Pinno.
        > print "Higher or Lower"
        > print
        > number = random.choice(r ange(100))
        > guess = 0
        > while guess != number:
        > guess = input("Guess a number: ")
        > if guess > number:
        > print "Too high"
        > guess = input("Guess a number: ")
        > elif guess < number:
        > print "Too low"
        > guess = input("Guess a number: ")
        > print "Just right"[/color]

        There is a problem with this, caused by having to repeat the same code in
        multiple places. Sa that the number is 50. You get to the first "input"
        statment, and you enter 30. It prints "Too low", and asks you to enter
        another number. You enter 40. The "while" expression is true, so it will
        loop again, and prompt you to enter ANOTHER number, without telling you
        whether it was high or low.

        Better to eliminate duplicated code:

        import random
        print "Higher or Lower"
        print
        number = random.choice(r ange(100))
        while 1:
        guess = input("Guess a number: ")
        if guess == number:
        break
        elif guess > number:
        print "Too high"
        else:
        print "Too low"
        print "Just right"

        --
        - Tim Roberts, timr@probo.com
        Providenza & Boekelheide, Inc.

        Comment

        • John Machin

          #5
          Re: What's wrong with this code?

          Nathan Pinno wrote:[color=blue]
          > Hi all,
          >
          > What's wrong with the following code? It says there is name error, that
          > random is not defined. How do I fix it?[/color]

          Others have already answered that question. This posting is a
          pre-emptive strike to head off the next half-a-dozen questions.
          [color=blue]
          >
          > # Plays the guessing game higher or lower.
          > # Originally written by Josh Cogliati, improved first by Quique, then by
          > Nathan Pinno.[/color]

          Some of us are interested in the process by which great pieces of code
          arise, how they are meticulously honed and polished, which craftpersons
          contributed what ... is it possible for you to publish the earlier versions?
          [color=blue]
          > print "Higher or Lower"
          > print
          > number = random.choice(r ange(100))[/color]

          "number" will refer to one of: 0, 1, ......, 98, 99
          [color=blue]
          > guess = 0[/color]

          so you'll get a strange result by using zero here; try -1 instead
          [color=blue]
          > while guess != number:
          > guess = input("Guess a number: ")[/color]

          "guess" will refer to a string e.g. "42" which will *not* compare equal
          to the integer 42. Also you should use raw_input, not input.

          so do this:

          guess = int(raw_input(" Guess a number: "))
          [color=blue]
          > if guess > number:
          > print "Too high"
          > guess = input("Guess a number: ")[/color]

          This will cause your program to ask TWICE per trip around the loop. Lose it.
          [color=blue]
          > elif guess < number:
          > print "Too low"
          > guess = input("Guess a number: ")[/color]

          .... and again.
          [color=blue]
          > print "Just right"
          >[/color]

          General advice:
          1. Look on the Python web site (http://www.python.org) for an
          introduction to Python for non-programmers.
          2. Join the Python tutor list.
          3. In this news group, read a little more than the threads that you have
          started; this guessing game was discussed in a thread started by
          somebody calling themselves ChuckDubya on 29 June!! Homework??

          HTH,
          John

          Comment

          • John Machin

            #6
            Re: What's wrong with this code?

            John Machin wrote:[color=blue]
            > Nathan Pinno wrote:[/color]
            [color=blue][color=green]
            >> guess = input("Guess a number: ")[/color]
            >
            >
            > "guess" will refer to a string e.g. "42" which will *not* compare equal
            > to the integer 42. Also you should use raw_input, not input.
            >
            > so do this:
            >
            > guess = int(raw_input(" Guess a number: "))
            >[/color]

            Ahem ... I'll redo that:

            You'd be better using raw_input, which *then* means you need to wrap
            int() around it, ending up with the same recommendation:

            guess = int(raw_input(" Guess a number: "))

            Cheers,
            John

            Comment

            Working...