473,387 Members | 1,745 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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(range(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 UsenetRevolution.com - Revolutionary Usenet
** HIGH RETENTION ** Specializing in Large Binaries Downloads **
http://www.UsenetRevolution.com
Jul 19 '05 #1
5 1350


Nathan Pinno a écrit :
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?
Add "import random" at the top of your file

Cheers,

SB
# 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(range(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 UsenetRevolution.com - Revolutionary Usenet
** HIGH RETENTION ** Specializing in Large Binaries Downloads **
http://www.UsenetRevolution.com


Jul 19 '05 #2
Nathan Pinno wrote:
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?


You need to import random.

--
Robert Kern
rk***@ucsd.edu

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

Jul 19 '05 #3
"Nathan Pinno" <fa********@hotmail.com> wrote:

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(range(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"


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(range(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, ti**@probo.com
Providenza & Boekelheide, Inc.
Jul 19 '05 #4
Nathan Pinno wrote:
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?
Others have already answered that question. This posting is a
pre-emptive strike to head off the next half-a-dozen questions.

# Plays the guessing game higher or lower.
# Originally written by Josh Cogliati, improved first by Quique, then by
Nathan Pinno.
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?
print "Higher or Lower"
print
number = random.choice(range(100))
"number" will refer to one of: 0, 1, ......, 98, 99
guess = 0
so you'll get a strange result by using zero here; try -1 instead
while guess != number:
guess = input("Guess a number: ")
"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: "))
if guess > number:
print "Too high"
guess = input("Guess a number: ")
This will cause your program to ask TWICE per trip around the loop. Lose it.
elif guess < number:
print "Too low"
guess = input("Guess a number: ")
.... and again.
print "Just right"


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
Jul 19 '05 #5
John Machin wrote:
Nathan Pinno wrote:

guess = input("Guess a number: ")

"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: "))


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
Jul 19 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

125
by: Sarah Tanembaum | last post by:
Beside its an opensource and supported by community, what's the fundamental differences between PostgreSQL and those high-price commercial database (and some are bloated such as Oracle) from...
72
by: E. Robert Tisdale | last post by:
What makes a good C/C++ programmer? Would you be surprised if I told you that it has almost nothing to do with your knowledge of C or C++? There isn't much difference in productivity, for...
121
by: typingcat | last post by:
First of all, I'm an Asian and I need to input Japanese, Korean and so on. I've tried many PHP IDEs today, but almost non of them supported Unicode (UTF-8) file. I've found that the only Unicode...
51
by: WindAndWaves | last post by:
Can anyone tell me what is wrong with the goto command. I noticed it is one of those NEVER USE. I can understand that it may lead to confusing code, but I often use it like this: is this...
46
by: Keith K | last post by:
Having developed with VB since 1992, I am now VERY interested in C#. I've written several applications with C# and I do enjoy the language. What C# Needs: There are a few things that I do...
13
by: Jason Huang | last post by:
Hi, Would someone explain the following coding more detail for me? What's the ( ) for? CurrentText = (TextBox)e.Item.Cells.Controls; Thanks. Jason
1
by: GS | last post by:
I got a combobox box that I load at load time. the Item and vales ended up in reverse order of each other, what went wrong? the database table has the following row code value ebay ...
98
by: tjb | last post by:
I often see code like this: /// <summary> /// Removes a node. /// </summary> /// <param name="node">The node to remove.</param> public void RemoveNode(Node node) { <...> }
9
by: Pyenos | last post by:
import cPickle, shelve could someone tell me what things are wrong with my code? class progress: PROGRESS_TABLE_ACTIONS= DEFAULT_PROGRESS_DATA_FILE="progress_data" PROGRESS_OUTCOMES=
20
by: Daniel.C | last post by:
Hello. I just copied this code from my book with no modification : #include <stdio.h> /* count characters in input; 1st version */ main() { long nc; nc = 0;
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.