473,386 Members | 1,753 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,386 software developers and data experts.

break the loop in one object and then return

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()

Jun 26 '06 #1
5 1257
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

Jun 26 '06 #2
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.
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!"

Jun 26 '06 #3
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

Jun 26 '06 #4
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.

Petr Jakes wrote:
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


Jun 26 '06 #5
Alex Pavluck wrote:
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


Petr Jakes wrote:
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


Jun 27 '06 #6

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

Similar topics

5
by: Ann | last post by:
I have trouble sometimes figuring out where break and continue go to. Is there some easy way to figure it out, or a tool? TIA Ann
5
by: viza | last post by:
Hi! Suppose I have int i,j,k; for(i=0;i<I;++i){ /* loop 1 */ for(j=0;j<J;++j){ /* loop 2 */ for(k=0;k<K;++k){ /* loop 3 */ if(test){
5
by: tony collier | last post by:
To break out of a loop i have seen some people use RETURN instead of BREAK I have only seen RETURN used in functions. Does anyone know why RETURN is used instead of BREAK to kill loops?
3
by: Krish | last post by:
Hello, This is an issue that I have encountered several times but have just used a workaround to solve. What is the accepted practice for breaking out of nested loops? For example: while(...
14
by: serrand | last post by:
Could someone tell me a beautiful way to exit from a switch and a loop in one statement ... without using a goto... and if possible without using an auxiliary variable as i did... int res;...
26
by: Alexander Korsunsky | last post by:
Hi! I have some code that looks similar to this: -------------------------------------------- char array = "abcdefghij"; for (int i = 0; i < 10; i++) {
4
by: kenneth6 | last post by:
for(int i=0; i<100; i++) { if(mark==TMark) { return i; cout << i << endl; break; } else {
4
by: raylopez99 | last post by:
See comment below. This is a simple problem but I'm a little rusty. How to break out of a event loop (here _Paint)? I've tried if/else, case, etc but not quite what I want--I keep getting the JIT...
7
by: jeddiki | last post by:
Hi, I am using a function called htmlwrap() which states that it does NOT add a "<br>" to the 70 character line so that it forces a line wrap. ( the script safely wraps long words without...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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,...

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.