Common Computer Science References
At the end of this lesson, you will be able to:
understand and use an if ... then ... else statement
if ... then statements
watch one way to truly generate random numbers
go over how to generate a random number in python
import random
some_variable = random.randint(1, 100) # a number between 1 and 100
note that in a flowchart, generating a random number is a process
go over If…Then…Else , Computer Based Problem Solving
step through the better "Too Many Students" program (see below)
notice it now gives an answer no matter what the number is
re-create the "Number Guessing Game" program from before
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
step through your code and see what the random number variable is when testing your program
for your test case, you will not know what will happen because of the random numbers
instead of having "mathematical" test cases, now you have "scenario" test cases
show 1 outcome where the number was guessed correctly
show 1 outcome where the number was not guessed correctly
in your top-down, flowchart and pseudocode you include a statement for actually generating the random number
Note: since you are generating a random number, there is no need for a constants.py file
recreate the same above program in C
in C you will have to explicitly set the seed (hopefully to the clock) or you will always get the same random number
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
int main() {
// this function allows the user to guess a number
// and the program decides if the user is correct
unsigned int seed = time(NULL);
int randomNumber = rand_r(&seed) % 10;
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.
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.