Udostępnij za pośrednictwem


Small Basic Example - Wood Chuck: How much wood could a woodchuck chuck?

How much wood could a woodchuck chuck if a woodchuck could chuck wood? Try out the program below to figure out the answer. Then find some ways to improve the program!

 

' Woodchuck.sb

TextWindow.Write("How much wood could a woodchuck chuck ")

TextWindow.WriteLine("if a woodchuck could chuck wood?")

TextWindow.WriteLine("")

TextWindow.WriteLine("You have 4 chances to guess how many logs.")

TextWindow.WriteLine("")

 

ans   = Math.GetRandomNumber(20)

guess = 0

trial = 0

While ( (trial < 4) And (guess <> ans) )

  trial = trial + 1

  TextWindow.Write("Guess #" + trial + " [1-20]: ")

  guess = TextWindow.ReadNumber()

  If (guess = ans) Then

    TextWindow.Write("Correct! You guessed it in try #" + trial)

    TextWindow.WriteLine(". Wait, are you a woodchuck?")

  ElseIf(guess > ans) Then

    TextWindow.Write("That’s too much wood. ")

    TextWindow.WriteLine("The woodchuck would lose its teeth!")

  Else

    TextWindow.Write("Too little. The woodchuck would starve ")

    TextWindow.WriteLine("without more wood!")

  EndIf

  TextWindow.WriteLine("")

EndWhile

 

'If your user guesses incorrectly, show her the answer

If ((guess <> ans) And (trial = 4)) Then

  msg = "You lost. The correct answer is " + ans + " logs. Duh."

  TextWindow.WriteLine(msg)

  TextWindow.WriteLine("")

EndIf

 

One might argue that a woodchuck wouldn't starve, as a woodchuck is just a common groundhog and doesn't actually chuck wood. However, the original question offers the presupposition that all the following statements acquire... What if a woodchuck could chuck wood?

 

Can you improve this program? Reply with your Small Basic Import ID to share your improvements!

 

Head to the Small Basic forum if you have any questions:

https://social.msdn.microsoft.com/Forums/en-US/smallbasic/threads/ 

 

And go to https://blogs.msdn.com/SmallBasic to download Small Basic and learn all about it!

Small and Basically yours,

   - Ninja Ed & Majed Marji

Comments