Common Computer Science References
At the end of this lesson, you will be able to:
understand and use a try catch statement
select statements
go over Try Catch , Computer Based Problem Solving
nil
re-create the "Number Guessing Game" program from before, again
use an If ... Then ... Else, so the user is told if the number is correct or wrong
when they get the number wrong, ensure you tell them what the correct number was
use the random number generator to generate a new random number every time the program is started over
this time ENSURE that what the user is entering is actually an integer
if it is not an integer, then let the user know this and DO NOT let the program crash
for your test cases, now you have 3 possible outcomes
from now on, 1 of your test cases MUST be if someone enters "invalid" data
more info about try ... catch for python
recreate the same above program in C
remember C does not have a try ... catch or a good way to get strings or numbers nicely; so we will use the CS50 C library
// Copyright (c) 2020 Mr. Coxall All rights reserved.
//
// Created by: Mr. Coxall
// Created on: Sep 2020
// This program gets an integer, using the CS50 library
#include <stdio.h>
#include <cs50.h>
int main() {
// this function gets an integer
int someInt;
// input
someInt = get_int("Enter and Int: ");
printf("Your last string was %d\n", someInt);
printf("\nDone.\n");
return 0;
}
NOTE: You can turn on "Closed Captions" to see a printout of what is being said by selecting the "CC" button.
You can also have it translate the closed captions by going to "Settings, Subtitles/CC, Auto-translate" and then pick your prefered language.