Computer Science Canada Problem with Restarting Loop, and Counter |
Author: | tristanbryce7 [ Thu Oct 11, 2012 4:28 pm ] | ||
Post subject: | Problem with Restarting Loop, and Counter | ||
Ok, so I have to make this game for class, where the computer makes a random int. from 1- 20, and you guess what it is, and it tells you if youre too high or too low, but problem is, after you finally guess the right amount, the program needs to tell you how many tries it took you to find the right amount, and i came in to the class 3 weeks late, so I didnt learn counters, counted loops and etc. So how do i put a counter in here that would tell you how much tries it took you to do it ? Also, once they get it right, i want the program, to re-start, but this time with a different number .
|
Author: | Clayton [ Thu Oct 11, 2012 5:09 pm ] | ||||||
Post subject: | Re: Problem with Restarting Loop, and Counter | ||||||
First of all, you have a whole lot of redundant code here:
Any time you feel like you're copy-pasting code is usually a time to step back and re-think how you're doing things. So for example, that ugly if statement can all be pushed up to your exit condition in the loop above like this:
Now then, onto your actual question, order to keep track of the number guesses, you'll have to have a counter that increments every time that a valid guess is input. Counters are nothing more than an integer variable that increment at some fixed rate when the conditions for it to do so are met. In this case, we just want it to increment by one every time a valid guess occurs, and then resets at the beginning of each game. For reference:
As far as re-running the game each time a successful guess occurs, simply wrap the whole game in a loop that re-initializes everything relevant at the start. Good luck! |
Author: | tristanbryce7 [ Thu Oct 11, 2012 7:36 pm ] | ||
Post subject: | Re: Problem with Restarting Loop, and Counter | ||
Thanks! But my teacher said to do it for practice and dummyproofing, and btw , I have another problem . My code at the moment is
the problem is that no matter what i do , the counter always says it took you only 1 try? so how do i change / fix it. And also, when it ends, the number is still the same from last time, so if it was 13 the first time, it stays that way through out the program after the first time ![]() |
Author: | Tony [ Thu Oct 11, 2012 7:58 pm ] |
Post subject: | Re: Problem with Restarting Loop, and Counter |
tristanbryce7 @ Thu Oct 11, 2012 7:36 pm wrote: the counter always says it took you only 1 try? Where in the code do you increment the counter? tristanbryce7 @ Thu Oct 11, 2012 7:36 pm wrote: And also, when it ends, the number is still the same from last time Presumably you mean the random number you are trying to guess? Where in the code do you pick that random number? How many times does that line get called? |
Author: | tristanbryce7 [ Thu Oct 11, 2012 8:03 pm ] |
Post subject: | Re: Problem with Restarting Loop, and Counter |
Tony @ Thu Oct 11, 2012 7:58 pm wrote: tristanbryce7 @ Thu Oct 11, 2012 7:36 pm wrote: the counter always says it took you only 1 try? Where in the code do you increment the counter? tristanbryce7 @ Thu Oct 11, 2012 7:36 pm wrote: And also, when it ends, the number is still the same from last time Presumably you mean the random number you are trying to guess? Where in the code do you pick that random number? How many times does that line get called? umm, im really new to turing lol , so i have no clue wat you're saying sorry man . im new to comp. programming in general, if by increment you mean where did i put it ,i put it near the end lol and the random number is put at the really beggining . OHHH, which is outside the loop , so i should prb put it inside lol . thanks , ill try it right noe, if i do that tho, it changes it every time , so eevery time i guess, its a different number , so ill guess 1, and it says too low, and then ill say 2, than itll say too high, meaning it wasnt the same number as last time i guessed, so im totally confused |
Author: | Tony [ Thu Oct 11, 2012 9:01 pm ] | ||||
Post subject: | RE:Problem with Restarting Loop, and Counter | ||||
I'm just trying to get you to look at the right parts of your code. You already correctly figured out what the problem with the random number generation is -- now the question is: when do you want to pick a new random number? (that is, what should happen right before you'd need to pick a new number; place the code there). As for the increment, you just need to look closely at
hint: consider this other piece of code
|
Author: | Clayton [ Fri Oct 12, 2012 7:13 am ] |
Post subject: | RE:Problem with Restarting Loop, and Counter |
Another thing to note is that you only ever modify the value of myCounter when you successfully guess the right number, so naturally it's only going to be one when you increment it. Every guess you make, regardless of the outcome, needs to increment myCounter. |
Author: | tristanbryce7 [ Fri Oct 12, 2012 10:28 am ] |
Post subject: | Re: Problem with Restarting Loop, and Counter |
I tried all of it, I just cant get it , I tried changing the myCounter statement but nothing works , probably just my nubby programming knowledge Thanks anyways lol |
Author: | Tony [ Fri Oct 12, 2012 11:20 am ] |
Post subject: | RE:Problem with Restarting Loop, and Counter |
How are the following three values different (if at all)? +1, 1, -1 |
Author: | Clayton [ Fri Oct 12, 2012 1:56 pm ] | ||
Post subject: | RE:Problem with Restarting Loop, and Counter | ||
On top of that, carefully look at this code again, and try and spot what you did differently from me when incrementing myCounter. Keeping these things in mind, you should be able to spot why it's not working properly.
|
Author: | tristanbryce7 [ Mon Oct 15, 2012 6:48 am ] |
Post subject: | Re: Problem with Restarting Loop, and Counter |
i tried putting the counter in its own loop, both before and after the big loop, and -1,1,+1 are different, as first goes decresing, other is nuetral, and other increases |
Author: | Insectoid [ Mon Oct 15, 2012 7:41 am ] |
Post subject: | RE:Problem with Restarting Loop, and Counter |
Quote: i tried putting the counter in its own loop, both before and after the big loop
Read over that a few times and think about where you *haven't* tried it yet. Quote: -1,1,+1 are different, as first goes decresing, other is nuetral, and other increases
No, they're all just numbers. What's the difference between +1 and 1? |
Author: | tristanbryce7 [ Mon Oct 15, 2012 12:05 pm ] | ||||
Post subject: | Re: Problem with Restarting Loop, and Counter | ||||
Yeah haha, i got it lol , problem with my old one was it was supposed to be
instead of what i had previously
|
Author: | Insectoid [ Mon Oct 15, 2012 5:03 pm ] |
Post subject: | RE:Problem with Restarting Loop, and Counter |
You could find a better spot, where you don't have to type it three times. |
Author: | Clayton [ Mon Oct 15, 2012 8:50 pm ] |
Post subject: | RE:Problem with Restarting Loop, and Counter |
Think about it this way, if you're going to increment it no matter what the result of the if statement is, what better option do you have? |
Author: | QuantumPhysics [ Mon Oct 15, 2012 9:23 pm ] |
Post subject: | RE:Problem with Restarting Loop, and Counter |
instead of myCounter = myCounter + 1; use myCounter += 1; it does the same thing, its more shorter and efficient in the same manner. ![]() |
Author: | tristanbryce7 [ Tue Oct 16, 2012 4:28 pm ] |
Post subject: | Re: Problem with Restarting Loop, and Counter |
To be honest, I dont know what increment means lol. I dont know where else to put the myCounter += 1 statement to make it much more efficient. I'll ponder over it thought |
Author: | tristanbryce7 [ Tue Oct 16, 2012 4:34 pm ] | ||
Post subject: | Re: Problem with Restarting Loop, and Counter | ||
and taking Claytons and QuantamPhysics advice i changed it to
and it worked either way ![]() |
Author: | Insectoid [ Tue Oct 16, 2012 5:26 pm ] |
Post subject: | RE:Problem with Restarting Loop, and Counter |
Learn to use Google. Or a dictionary. Solving your own problems is the first and most important step in learning to program. |
Author: | tristanbryce7 [ Tue Oct 16, 2012 5:38 pm ] |
Post subject: | Re: Problem with Restarting Loop, and Counter |
Definitely, I just needed some help getting started . |