break the loop in one object and then return
Simon Forman
rogue_pedro at yahoo.com
Mon Jun 26 15:48:32 EDT 2006
Alex Pavluck wrote:
> 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
More information about the Python-list
mailing list