locked
Problem with exercise in Small Basic Curriculum (1.5) RRS feed

  • Question

  • Hey everyone. Before making this topic I searched for a similar one but the most similar I got to wasn't quite what i was looking for.

    This is the exercise:

    Write a program that opens a text window and then performs the following steps: 
    
    Asks the user for the name, temperature, rain status, and wind status of 10 cities. 
    Uses branching and subroutines to determine and display the total number of:
    Cold Cities
    Cool Cities
    Warm Cities
    Hot Cities
    Rainy Cities
    Windy Cities
    


    
    
    
    

    This is my code done so far:

    Sub Invalid
        TextWindow.ForegroundColor = "Red"
        TextWindow.WriteLine("Please enter 'Y' or 'N'")
      EndSub
      
      Sub Correct
        TextWindow.ForegroundColor = "White"
      EndSub
    
    TextWindow.Title = "Distinguishment of cities to 'Cold', 'Cool', 'Warm', 'Hot', 'Rainy' and 'Windy'"
    TextWindow.ForegroundColor = "White"
    While i <2
      TextWindow.Write("Enter the name of the city: ")
      City = TextWindow.Read()
      TextWindow.Write("Enter the current temperature of " + City + " (In Celsius): ")
      Temperature = TextWindow.ReadNumber()
      ReturnRain:
      TextWindow.ForegroundColor = "White"
      TextWindow.Write("Is it rainy in " + City + " (Y/N)? ")
      RainStatus = TextWindow.Read()
      If RainStatus = "Y" Or RainStatus = "N" Or RainStatus = "y" Or RainStatus = "n" Then
        Correct()
      Else 
        Invalid() 
        Goto ReturnRain
      EndIf
      ReturnWind:
      TextWindow.ForegroundColor = "White"
      TextWindow.Write("Is it windy in " + City + " (Y/N)? ")
      WindStatus = TextWindow.Read()
      If WindStatus = "Y" Or WindStatus = "y" Or WindStatus = "N" Or WindStatus = "n" Then
        Correct()
      Else 
        Invalid()
        Goto ReturnWind
      EndIf
      ColdCity()
        i = i + 1      
    Endwhile
    
      
      Sub ColdCity
        If Temperature >= -15 And Temperature <=0 Then
          TextWindow.WriteLine("Cold Cities: " + City)
        EndIf
        EndSub


    

    I know how to display the total count of cold cities, cool cities and all of that but I always like to tweak the exercise to make it harder and more challenging so that I can put more pressure on myself to learn more. My goal is for the program to actually display what are the cold cities, cool cities etc and not just the total count.

    For example it should look like this after all the cities are entered:

    Cold Cities: New York

    Cool Cities: Brazil

    Can you please tell me how to do this?

    Friday, January 4, 2013 6:41 PM

Answers

  • First we need to check all the temperature ranges, and then store the results in an array which we can use at the end.

    Are you familiar with arrays in Small Basic yet?

    Friday, January 4, 2013 7:12 PM
  • Perhaps this kind of thing, where it loops inside a While until a valid entry is given.  Its not any shorter and its not to everyone's taste that this is better, but you are right to avoid GoTo's if you can - it isn't so bad when the GoTo is very near where it is called and clear what is happening - much worse when the Goto is somewhere completely different in a different bit of logic.

    TextWindow.ForegroundColor = "White"
    TextWindow.Write("Is it rainy in " + City + " (Y/N)? ")
    RainStatus = TextWindow.Read()
    RainStatus = Text.ConvertToLowerCase(RainStatus)
    While (RainStatus <> "y" And RainStatus <> "n")
      TextWindow.ForegroundColor = "Red"
      TextWindow.WriteLine("Please enter 'Y' or 'N'")
      TextWindow.ForegroundColor = "White"
      TextWindow.Write("Is it rainy in " + City + " (Y/N)? ")
      RainStatus = TextWindow.Read()
      RainStatus = Text.ConvertToLowerCase(RainStatus)
    EndWhile
    

    Monday, January 7, 2013 8:13 PM
  • It means 'Not equal to' so we check that it is not equal to y and not equal to n, i.e. anything other than y or n.
    Monday, January 7, 2013 8:23 PM

All replies

  • First we need to check all the temperature ranges, and then store the results in an array which we can use at the end.

    Are you familiar with arrays in Small Basic yet?

    Friday, January 4, 2013 7:12 PM
  • If you want some pointers or further help with your code then please ask.

    I didn't want to 'just do it' when I wasn't sure if you had used arrays and what kind of answer would be most helpful since you clearly want to learn yourself.

    A small article on Small Basic arrays is here.

    Good luck.

    Saturday, January 5, 2013 2:10 PM
  • If you want some pointers or further help with your code then please ask.

    I didn't want to 'just do it' when I wasn't sure if you had used arrays and what kind of answer would be most helpful since you clearly want to learn yourself.

    A small article on Small Basic arrays is here.

    Good luck.

    Thanx. I was busy trying to reply with my phone but couldn't cause my browser isn't very good.

    Arrays only comes later in the curriculum so i haven't really done it yet (besides a small example with a true/false output). As I said I always adjust the exercise to make it harder but I will only do this exercise once arrays is introduced later in the curriculum.

    I got another question to ask you though. People say that using goto statements makes your code confusing and I actually realised this myself. SO what can I use to reduce the goto statements and make my code more simpler and easy to understand. I will take out an example out of the code above: 

    Sub Invalid
        TextWindow.ForegroundColor = "Red"
        TextWindow.WriteLine("Please enter 'Y' or 'N'")
      EndSub
      
      Sub Correct
        TextWindow.ForegroundColor = "White"
      EndSub
    
    TextWindow.Title = "Distinguishment of cities to 'Cold', 'Cool', 'Warm', 'Hot', 'Rainy' and 'Windy'"
    TextWindow.ForegroundColor = "White"
    While i <2
      TextWindow.Write("Enter the name of the city: ")
      City = TextWindow.Read()
      TextWindow.Write("Enter the current temperature of " + City + " (In Celsius): ")
      Temperature = TextWindow.ReadNumber()
      ReturnRain:
      TextWindow.ForegroundColor = "White"
      TextWindow.Write("Is it rainy in " + City + " (Y/N)? ")
      RainStatus = TextWindow.Read()
      If RainStatus = "Y" Or RainStatus = "N" Or RainStatus = "y" Or RainStatus = "n" Then
        Correct()
      Else 
        Invalid() 
        Goto ReturnRain
      EndIf
      ReturnWind:
      TextWindow.ForegroundColor = "White"
      TextWindow.Write("Is it windy in " + City + " (Y/N)? ")
      WindStatus = TextWindow.Read()
      If WindStatus = "Y" Or WindStatus = "y" Or WindStatus = "N" Or WindStatus = "n" Then
        Correct()
      Else 
        Invalid()
        Goto ReturnWind
      EndIf
      ColdCity()
        i = i + 1      
    Endwhile
    
      
      Sub ColdCity
        If Temperature >= -15 And Temperature <=0 Then
          TextWindow.WriteLine("Cold Cities: " + City)
        EndIf
        EndSub

    In this example I use Goto statements. The user has to type "Y" ,"y" ,"N" or "n" to the given question and I use the goto statement whether the user typed in the "correct" answer and whther they did not. How do I make it simpler and not use the goto statements?

    Thank You:)

    Monday, January 7, 2013 7:39 PM
  • Perhaps this kind of thing, where it loops inside a While until a valid entry is given.  Its not any shorter and its not to everyone's taste that this is better, but you are right to avoid GoTo's if you can - it isn't so bad when the GoTo is very near where it is called and clear what is happening - much worse when the Goto is somewhere completely different in a different bit of logic.

    TextWindow.ForegroundColor = "White"
    TextWindow.Write("Is it rainy in " + City + " (Y/N)? ")
    RainStatus = TextWindow.Read()
    RainStatus = Text.ConvertToLowerCase(RainStatus)
    While (RainStatus <> "y" And RainStatus <> "n")
      TextWindow.ForegroundColor = "Red"
      TextWindow.WriteLine("Please enter 'Y' or 'N'")
      TextWindow.ForegroundColor = "White"
      TextWindow.Write("Is it rainy in " + City + " (Y/N)? ")
      RainStatus = TextWindow.Read()
      RainStatus = Text.ConvertToLowerCase(RainStatus)
    EndWhile
    

    Monday, January 7, 2013 8:13 PM
  • Thank You. What does <> mean? Does it mean greater or smaller? If so then I didn't know that you can use those signs with letters.
    Monday, January 7, 2013 8:17 PM
  • It means 'Not equal to' so we check that it is not equal to y and not equal to n, i.e. anything other than y or n.
    Monday, January 7, 2013 8:23 PM
  • Aah Lol..Thank you for your help:)
    Monday, January 7, 2013 8:35 PM