Need Help adding a counter
Author |
Message |
Murphman
|
Posted: Thu Oct 29, 2009 1:13 pm Post subject: Need Help adding a counter |
|
|
What is it you are trying to achieve?
I created a guessing game and i am having trouble adding a counter in. so that when you guess the right number it will show you how many guess's it took you to get that number
What is the problem you are having?
it puts 0 where i want it to put number of guess it took
Describe what you have tried to solve this problem
i tried adding a var outside of the for loop and declaring it as 0 and making it so it goes up by one everytime the loop is ran.
Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
Turing: |
color (yellow)
colorback (red)
cls
var guess : int
var number := Rand.Int (1, 100)
var num : int
num := 0
for i : 1 .. 100
put "Choose a number between 1 and 100"
get guess
cls
if guess < number then
put "Higher"
elsif guess > number then
put "lower"
end if
if guess = number then
exit
num := num + 1
end if
end for
put "Congratulations you got it right. It took you ", num, " tries."
|
Please specify what version of Turing you are using
turing 4.1 |
|
|
|
|
|
Sponsor Sponsor
|
|
|
DemonWasp
|
Posted: Thu Oct 29, 2009 1:39 pm Post subject: RE:Need Help adding a counter |
|
|
Your flow of control is very wrong. The code for incrementing your num variable will never be executed because it occurs immediately after an exit, which will take you out of the loop without executing anything further. It also never happens on incorrect guesses. |
|
|
|
|
|
Sited
|
Posted: Sat Oct 31, 2009 3:35 pm Post subject: RE:Need Help adding a counter |
|
|
All I can say is add more comments and make your variables more descriptive so whoever is reading your code will actually want to read it.. as for counter, you use variable += 1 or variable := variable + 1. And you have to put that inside the loop where you have to guess again. So basically what you can do is
code: |
loop
Please enter your guess: " ..
get guess
noOfGuesses += 1
|
|
|
|
|
|
|
B-Man 31
|
Posted: Sat Oct 31, 2009 7:10 pm Post subject: RE:Need Help adding a counter |
|
|
why is it in a for loop, it should be in a regular loop, that will also make it easier to add a counter
Turing: | color (yellow)
colorback (red)
cls
var guess : int
var number := Rand.Int (1, 100)
var num : int
num := 0
loop
put "Choose a number between 1 and 100"
get guess
cls
if guess < number then
put "Higher"
elsif guess > number then
put "lower"
end if
exit when guess = number
num + = 1
end loop
put "Congratulations you got it right. It took you ", num, " tries." |
|
|
|
|
|
|
|
|