break the loop in one object and then return

A

Alex Pavluck

I am trying to write the following code to block up evaluation and
prompting for entering new information. However, when I break the loop
in one object and then return it does not start at the beginning again
but rather at the point where it exited. Can someone look at the
following code and give me some feedback.





yournum = input("I am thinking of a number between 1 and 100.\n Guess
which number: ")
mynum = (yournum-5)

def eval():
if yournum == mynum:
print "Wow! You got it!"
elif yournum < mynum:
print "Nope. Too low"
again()
elif yournum > mynum:
print "Oh, your too high"
again()

def again():
global yournum
yournum = input("guess again: ")
eval()

eval()
 
S

Simon Forman

Alex said:
I am trying to write the following code to block up evaluation and
prompting for entering new information. However, when I break the loop
in one object and then return it does not start at the beginning again
but rather at the point where it exited. Can someone look at the
following code and give me some feedback.





yournum = input("I am thinking of a number between 1 and 100.\n Guess
which number: ")
mynum = (yournum-5)

def eval():
if yournum == mynum:
print "Wow! You got it!"
elif yournum < mynum:
print "Nope. Too low"
again()
elif yournum > mynum:
print "Oh, your too high"
again()

def again():
global yournum
yournum = input("guess again: ")
eval()

eval()

Your code works fine for me. What's the problem exactly?

Be aware, 'eval' is a built-in function, you might want to use a
different name. And instead of "your" use "you're" the contraction of
"you are"-- but that's not a code problem.


FWIW, here's another way to structure your program without the
recursion you're using.

yournum = input("I am thinking of a number between 1 and 100.\n Guess
which number: ")
mynum = (yournum-5)

def loop():
global yournum, mynum

while yournum != mynum:
if yournum < mynum:
print "Nope. Too low"
elif yournum > mynum:
print "Oh, you're too high"
again()

print "Wow! You got it!"

def again():
global yournum
yournum = input("guess again: ")

loop()


HTH,
~Simon
 
J

Juho Schultz

Alex said:
I am trying to write the following code to block up evaluation and
prompting for entering new information. However, when I break the loop
in one object and then return it does not start at the beginning again
but rather at the point where it exited. Can someone look at the
following code and give me some feedback.
If you want a loop, write a loop.

eval() is a built-in function. Better leave the names of builtins as
they are.
yournum = input("I am thinking of a number between 1 and 100.\n Guess
which number: ")
mynum = (yournum-5)
If the users first guess is 2, mynum becomes -3...

Better use this - to ensure 100 >= mynum >= 1, and the user can guess
right on 1st try.
import random
mynum = random.randint(1,100)

input evaluates a user-supplied string, and is a bit dangerous.
(Run your original program, and give "mynum" as your second guess.)
So use yournum = int(raw_input("I am thinking...

you could use one while loop instead of two functions and one global
variable.

while (yournum != mynum):
if yournum < mynum:
print "Too low."
else:
print "Too high."
yournum = int(raw_input("Guess again:"))
print "You got it!"
 
P

Petr Jakes

It smells like many student are trying to do their homework last few
days here ... Can we now the name of your school? :)

AFAIK this group does not solve homeworks here :)

Just few points:

at the beginning try to test the input value
you can use "in range" or using "0 < yournum < 101"

you should test if the input is an integer as well..
http://tinyurl.com/j468c

Other suggested here which way to go.

Good luck :)

Petr Jakes
 
A

Alex Pavluck

Peter, Why do you make such claims without any reason to do so? This
is my own thing. So, I guess I am a student but just a student of my
own accord. So, please don't reply if you don't have anything to
contribute.

As for the - mynum I guess that could happen but I am just doing this
so that they will never match on first try. I guess I should just
hardcode it.

This is what happens if I step though:

mynum = 93 ***1***
yournum = input("I am thinking of a number between 1 and 100.\n Guess
which number: ") ***2***

def strt(): ***3*** ***6***
if yournum == mynum: ***7***
print "Wow! You got it!"
elif yournum < mynum: ***8***
print "Nope. Too low" ***9***
again() ***10*** ***15***
elif yournum > mynum:
print "Oh, your too high"

def again(): ***4*** ***11***
global yournum ***12***
yournum = input("guess again: ") ***13***
strt() ***14***

strt() ***5***


***15*** is the problem. It doesn't start at the top but rather where
is leaves the loop.
 
P

Petr Jakes

Alex said:
Peter, Why do you make such claims without any reason to do so? This
is my own thing. So, I guess I am a student but just a student of my
own accord. So, please don't reply if you don't have anything to
contribute.
First:
This is not "YOUR OWN THING". This group really can help, but this
group AFAIK does not write homeworks. It is nothing wrong you are just
a student, we are all students somehow :)

Second:
Few other posters were posting similar requests to this group last few
days (maybe your classmates :)) so the chance it just a coincidence is
very low. If I am wrong, sorry about that.

Third:
This group can help, but some effort (study, googling this group etc.)
on your side is necessary as well. Other posters suggested you the way,
where to go, but you didn't try to change your code a bit. Please read
the following as well:
http://www.albion.com/netiquette/corerules.html

To your code:
As for the - mynum I guess that could happen but I am just doing this
so that they will never match on first try. I guess I should just
hardcode it.

This is what happens if I step though:

mynum = 93 ***1***

use # sign for your comments in the code followed by the comment text.
All the stuff after the # is ignored when the code is executed. Nobody
can execute (examine) your code whit your comments like ***1***

To hardcode "mynum" value is IMHO very bad idea and your code will be
useless if the value will be "discovered". Other suggested how to
generate random number at the beginning of the code.
yournum = input("I am thinking of a number between 1 and 100.\n Guess
which number: ") ***2***

def strt(): ***3*** ***6***
if yournum == mynum: ***7***
print "Wow! You got it!"
elif yournum < mynum: ***8***
print "Nope. Too low" ***9***
again() ***10*** ***15***
elif yournum > mynum:
print "Oh, your too high"

def again(): ***4*** ***11***
global yournum ***12***
yournum = input("guess again: ") ***13***
strt() ***14***

strt() ***5***


***15*** is the problem. It doesn't start at the top but rather where
is leaves the loop.

print statement can help to solve your problem, put it (with some text
that will navigate you) on the rows where you are not sure the program
is not running properly. Print out the values of the mynum and yournum
as well. This will be just for your "debugging" purposes. Finally you
will remove it.

For example you can put:
print "starting the strt function", "mynum = ", mynum, "yournum =",
yournum

on the first row of your strt function, so you will see the code is
going through there.

Finally:
Try to thing what will happen if the person will input character rather
than integer.

Again: good luck in your effort

Petr Jakes
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top