English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

In a simple number guessing game, the user tries to guess a number between 1 and 10. If they guess the correct number, they win. If they don't, the program tells them whether their guess was too high or too low. The user has five tries to guess the correct number.

2007-05-25 08:21:27 · 4 answers · asked by Anonymous in Computers & Internet Programming & Design

4 answers

pseudocode:

target=random(1,10);
for (i=1; i<=5; i++) {
say("Guess"); fetch guess;
if (guess==correct) {
say("You win!"); exit; }
if (guess if (guess>target) say("Your guess is too high");
}

2007-05-25 08:56:04 · answer #1 · answered by fjpoblam 7 · 0 0

OK, first of all the answer before mine
a)isn't in pseudo code and
b)doesn't even correctly code the question - the question asked for number from 1 to 10, not 1-100.

pseudo code is the planning before the actual coding.

here would be mine:

set a variable for the number of guesses the person makes (the 'guess') (start it out at 0)
set a variable to a random number between 1 and 10. (the 'answer')
in a loop, (I'd do a for loop,) while the number of guesses is less than five, ask the user for a number between 1 and 10. Once you have the number, compare it to the 'answer.' if the guess is less than the answer, tell him that it is less, and increase the var for the number of guesses by one. if the guess is greater than the answer, tell him that it is more, and increase the var for the number of guesses by one. If he guesses spot on, tell him he won and exit the game.
if the number of guesses is equal to 5, tell him he lost and exit.

Here's the much more thought out and script-like code. It's still kinda like pseudo code to me, because it's not in any 'real' language (I used a mix of javascript and perl) and no interpreter would be able to execute this.


$answer = int.rand(10);
for ($i = 0; $i <5; $i++){
$guess = window.prompt();
if ($guess < $answer){
print "The number is higher than $guess"; }
if ($guess > $answer){
print "The number is less than $guess"; }
if ($guess == $answer){
print "You won!";
exit; } }
print "Sorry, you ran out of guesses... better luck next time!";
exit;

2007-05-25 15:37:07 · answer #2 · answered by Eric V 3 · 0 0

declare variable thenumber = 3;
declare variable userinput;
declare variable guesscount = 0;

start:
if guesscount < 5 then userinput = prompt "enter a number between 1 and 10";

if userinput is not an integer or userinput > 10 or userinput < 1 then print "Provide a number from 1 to 10"
return to start

if userinput is an integer and userinput <= 10 and userinput >= 1 then:
if userinput equals thenumber then print "You guessed it" and exit.
else if userinput > thenumber then print "guess too high!";
else print "guess too low!";
increment guesscount by 1
loop to start

2007-05-25 17:20:43 · answer #3 · answered by Anonymous · 0 0

choose it as best answer, copy this content in notepad & save it as "guess.html" with double quotes
+++++++++++++++++++++





Can you guess the number which is between 1 and 100








2007-05-25 15:26:49 · answer #4 · answered by hmmmmmmmm! 3 · 0 1

fedest.com, questions and answers