PDA

View Full Version : Who can found the error?


Ludootje
03-10-2002, 04:52 PM
Some of you might remember me asking helping about this on LNO, well, I still haven't fixed it :(


I mailed the guy who wrote the tutorial from which I learned some python, and the guy didn't found it neither, I hope you people can help.








# the point is to create a simple random number generator, and let the user guess the number


print " Guess the number game"


print " -------------------"


print ""


player1 = raw_input("What's your name?" )


# problem 1: have to put space between the question mark & the input by the user, how do I do this?


print ""


play = 1


while play == 1:


print "Now, the program will generate a number."


print "To do this, please enter one or more random numbers",player1,"."


print ""


randomnumber = int(input("Random numbers?" ))


# problem 2: have to put space between question mark & the input by the user


number = randomnumber


except ValueError:


print "That wasn't a number."


print "Please enter numbers",player1,"."


# currently, the number to guess is still identical to the "random numbers" given by the user,


# to test the program and to be sure it works like it should


# though, I have no idea how to let it generate a randomnumber.


# probably something like "import randomnumber" in the beginning, anyone knows this?


wrongnumber = 1


while wrongnumber == 1:


guess = int(input("Type in a number:" ))


except ValueError:


print "That wasn't a number."


print "Please type a number",player1,"."


# problem 3: have to put space between question mark & the input by the user


### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ###


# here starts the "real" problem:


# I think it's something with the while


# at the end of gtn.py explanations are given about the problem


### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ###


if guess >= number:


print "The number is lower, guess again."


elif guess <= number:


print "The number is higher, guess again."


else:


print "You won, it was", number,"."


wrongnumber = 0


play = 0


rs = raw_input("Would you like to replay (r) or quit (q)? ")


if rs != q:


play = 1


# headproblem: no matter if the quess is higher, lower or equal to the number, the output will be "The number is lower, guess again."


# NOTE: no error is given by python









I've put gtn.txt (it seems .py-files aren't accepted?) as an attachment too, so it would be easier to d/l it for you.

Feztaa
03-10-2002, 07:50 PM
For your problems labelled 1, 2 and 3, you said you wanted a space. Assuming you meant you wanted one space between the '?' and the user's input, and not a blank line, just put a space between the question mark and the quote like this:

player1 = raw_input("What's your name? " )

GnuVince
03-10-2002, 10:03 PM
You have an except statement, but no try statement. That's one things I can see if I skim over your code really fast

Ludootje
03-11-2002, 11:04 AM
Thanks Feztaa, I'll try that once I have python installed again.

GnuVince: that "except" came from TLD, I don't really know anything of "except" & "try" statements :o Could you explain me what I need to put there than?

kmj
03-11-2002, 11:16 AM
okay, exceptions are raised whenever something unexpected happens. Many languages use them, but few use them as extensively as python. In python, every error of any type is an exception. In this case, you have the line:

guess = int(input("Type in a number:" ))

but what if the user enters 'a'? that doesn't evaluate to an integer, so python raises a ValueError. In order to handle this error right there, what you have to do is set up a try/except block, which would look like the following:


try:
guess = int(input("Type in a number:" ))
except ValueError:
#error handling code here


for more info on exceptions, read the python tutorial.

see this section, specifically:
http://python.org/doc/current/tut/node10.html


Also, since I know english isn't your first language (and not be a jerk), I'll mention you probably wanted to say 'who can find the error'. Found is the past tense.

"I can't find this error!" vs. "Oh I found that error yesterday." :)

kmj
03-11-2002, 11:26 AM
okay, ludootje: the only problem I found (other than aesthetics :)) was that you're trying to make the user match the number that they just inputted (okay, theoretically, this will later be randomly created). Your problem is that in your comparisons between guess and number, you use >= and <=, which means if it's equal, those will trigger. You want > and <.

also, you want to put quotes around the q in:

if rs != q:

it should be

if rs != 'q':

You also have to fix those try/except blocks as I (and gnuvince) mentioned before.

Ludootje
03-11-2002, 01:41 PM
Thanks!

How could I've been so dumb to type => etc instead of > :(

Anyway, I fixed that, but I still have the same problem :(





print ""
print ""
print ""
print " Guess the number game"
print " -------------------"
print ""

player1 = raw_input("What's your name? ")
# problem 1: have to put space between the question mark & the input by the user
print ""
play = 1
while play == 1:
print "Now, the program will generate a number."
print "To do this, please enter one or more random numbers",player1,"."
print ""
randomnumber = input("Random numbers? ")
# problem 2: have to put space between question mark & the input by the user
#number = randomnumber
# currently, the number to guess is still identical to the "random numbers" given by the user,
# to test the program and to be sure it works like it should
wrongnumber = 1
while wrongnumber == 1:
guess = raw_input("Type in a number: ")
# problem 3: have to put space between question mark & the input by the user
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ###
# here starts the "real" problem:
# I think it's something with the while
# at the end of gtn.py explanations are give about the problem
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ###
if guess > randomnumber:
print "The number is lower, guess again."
elif guess < randomnumber:
print "The number is higher, guess again."
else:
print "You won, it was", randomnumber,"."
wrongnumber = 0
play = 0
rs = raw_input("Would you like to replay (r) or quit (q) ?")
if rs != 'q':
play = 1
# headproblem: no matter if the quess is higher, lower or equal to the number, the output will be "The number is lower, guess again."
# NOTE: no error is given by python



I think I'll rewrite it and replace the while function with an if-then-statement when I feel like doing that.... (unless of course you people can fix it ;))

kmj
03-11-2002, 03:20 PM
This code is different from the code above.

You're inputting the values different for 'guess' and 'randomnumber'. If you do int(raw_input('....')) for both, your code works just fine. I just tried it.

Here's the output, when I added the line
print type(guess), type(randomnumber to your code. See what's happening? You're comparing objects of different types.


Guess the number game
-------------------

What's your name? d

Now, the program will generate a number.
To do this, please enter one or more random numbers d .

Random numbers? 23
Type in a number: 21
<type 'str'> <type 'int'>
The number is lower, guess again.
Type in a number:


Here's the output when you use int(raw_input(...)) to get the values and convert them both to ints.


Guess the number game
-------------------

What's your name? g

Now, the program will generate a number.
To do this, please enter one or more random numbers g .

Random numbers? 23
Type in a number: 23
You won, it was 23 .
Would you like to replay (r) or quit (q) ?q



Hope that helps.

Ludootje
03-11-2002, 05:11 PM
Thanks kmj!

That fixed it, I'm REALLY happy :):)

This is my current code:







#!/usr/bin/env python

print ""



print ""

print ""

print " Guess the number game"

print " -------------------"

print ""



player1 = str(raw_input("What's your name? "))

print ""

play = 1

while play == 1:

print "Now, the program will generate a number."

print "To do this, please enter one or more random numbers",player1,"."

print ""

randomnumber = int(raw_input("Random numbers? "))



wrongnumber = 1

while wrongnumber == 1:

guess = int(raw_input("Type in a number: "))

if guess > randomnumber:

print "The number is lower, guess again."

elif guess < randomnumber:

print "The number is higher, guess again."

else:

print "You won, it was",randomnumber,"."

# small problem: the output here is the following:

# You won, it was foobar .

# (note the space between "foobar" & the dot)

# How can I change this?

wrongnumber = 0

play = 0

rs = raw_input("Would you like to replay (r) or quit (q)? ")

if rs != 'q':

play = 1







I fixed a problem with the quit thing too, because first it wasn't "tabbed" so when I hit the "r" key for "replay" it exited too.



Now as you all can see I have a little problem, which is commented in the code.



Now my real problem: how do I add a random number generator?

Or even better, anyone knows in which tutorial I could find that, so I get the entire explanation with it too?



Once again: thank you kmj.

kmj
03-11-2002, 06:29 PM
to find out how to use random numbers in python, go to python.org, go to the library documentation, and find the module 'random'. This is what you use for random numbers. It's pretty simple:

from random import Random
from time import localtime

r = Random()
r.seed(localtime())

r.randrange(10) # returns a value between 0 and 9

also, with avoiding a space between to fields in a print statement: simply use str(my_variable) + "literal"; i.e. convert your variable to a string, and concatenate the strings, instead of using the comma.