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 http://www.npinnowebsite.ca/
--
----------------------------------------------------------------
Posted via UsenetRevolutio n.com - Revolutionary Usenet
** HIGH RETENTION ** Specializing in Large Binaries Downloads ** http://www.UsenetRevolution.com 5 1356
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(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
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
"Nathan Pinno" <fa********@hot mail.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(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"
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, ti**@probo.com
Providenza & Boekelheide, Inc.
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(r ange(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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 software giant such as Microsoft SQL
Server, Oracle, and Sybase?
Is PostgreSQL reliable enough to be used for high-end commercial
application? Thanks
|
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 example,
between a C/C++ programmers with a few weeks of experience
and a C/C++ programmer with years of experience.
You don't really need to...
|
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 support IDEs are DreamWeaver 8 and
Zend PHP Studio.
DreamWeaver provides full support for Unicode. However, DreamWeaver is
a web editor rather...
|
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 wrong?????
Function x
select case z
|
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 believe MSFT should do to
improve C#, however.
I know that in the "Whidbey" release of VS.NET currently
| |
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
|
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 http://www.ebay.com
google http://www.google.com
yahoo http://www.yahoo.com
However in the drop down list
displayed value used
ebay ...
|
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) {
<...>
}
|
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=
|
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;
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
| |
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it. ...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...
| |