Computer Science Canada var topic |
Author: | nin [ Tue Nov 21, 2006 7:20 pm ] |
Post subject: | var topic |
im workin on a worksheet that my teacher gave me n its due tomorrow. I dont really know what to do on the last 3 assignments. and so far im having difficulties...n this is my code so far: loop %Declaration Section var number : int var answer : int := 28 %Program Title locate (1, 34) put "Guessing Game" %Program intorduction locate (3, 1) put "See if you can guess a number between ten and thirty!" %User Input locate (5, 1) put "Enter a number between 10 and 30:" .. get number %Possible guess results if number >= 29 then put "You need to come down a little!" .. elsif number = 28 then put "Good job!" .. elsif number >= 10 then put "Way down there?" .. end if end loop %End of Program the assignments are the following: a) add an if structure so that only an input between ten and thirty will be accepted. Test your program. Then input section will repeat until the user enters the correct value. Add an if structure so that only one of the possible guess will output. Test your program. Save it as IfStructure3.t Print source code and hand it in with this assignment. b) add a for loop structure so that the program will execute four times. Test your program. c)Add a for loop structure so that the program will execute four times and exit if the user guesses the correct answer before the fourth time. Test your program. so how can i do the following assignments... and am i on the right track so far? [/quote] |
Author: | Expirant [ Wed Nov 22, 2006 7:59 am ] | ||
Post subject: | |||
I think you're starting to get on the right track for question a). A couple things you may want to consider though is instead of declaring the answer right at the beginning of the code, use a variable to hold its value to make it easier for you or the teacher to read and possibly allow you to change it to a random number. And checking if a number is between two numbers should look something like:
Keep at it, Expirant |
Author: | rdrake [ Wed Nov 22, 2006 8:45 am ] |
Post subject: | Re: var topic |
nin wrote: the assignments are the following:
Long solution above, but it should work.
a) add an if structure so that only an input between ten and thirty will be accepted. Test your program. Then input section will repeat until the user enters the correct value. Add an if structure so that only one of the possible guess will output. Test your program. Save it as IfStructure3.t Print source code and hand it in with this assignment. nin wrote: b) add a for loop structure so that the program will execute four times. Test your program. Honestly not hard. Press F10 and look up the for statement.
nin wrote: c)Add a for loop structure so that the program will execute four times and exit if the user guesses the correct answer before the fourth time. Test your program. Use the exact same as above, then research the exit keyword.
nin wrote: and am i on the right track so far? You are. |
Author: | rdrake [ Wed Nov 22, 2006 11:47 am ] | ||||
Post subject: | Re: var topic | ||||
After rereading your post, I noticed you had a wee problem in your code. If you want between the values, then you want to adjust the first condition. Otherwise, adjust the last condition. Regardless, see two posts above for a fix. nin wrote:
Oh, and might I suggest you change the following line so you do not have to hardcode the number in twice.
![]() |