Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ## A. Primes
- Write a program that takes in an integer N and output the first N prime numbers. For example, if N=5, output 2 3 5 7 11.
- Hint: Use the prime checking subroutine from our last class. For the outer loop, create a variable to run through the numbers and another one to count how many primes you have printed so far. The latter is used for the termination condition.
- ## B. Guess The Number
- Write the following number guessing game: Computer picks a random number from 1 to 100 without telling the user. Computer asks human for their guess. Human inputs. Computer says if the value is too low, too high, or correct. The program should stop when human gives correct answer.
- ### Random number generation
- In the first line of your code, write (excluding the `s):
- import random
- Anywhere in your code you want to generate a random number, write:
- variable_name = random.randint(a, b)
- where a and b are the boundary values. For example,
- num = random.randint(1, 5)
- generates a value of 1,2,3,4 or 5 for num.
- ## C. Also Guess The Number
- We will swap roles this time. First, human thinks of a number between 1 to 100 (doesn't have to input). The computer will make a guess. Then, human simply needs to type 'too low', 'too high', or 'correct' (or any other strings you think might be a lot more convenient to code). Computer will repeatedly make a guess until you type 'correct', in which case the program should end.
- You should try a simple strategy first where the computer starts guessing from 1, 2, 3, ...
- If you manage to get that working, try the most sophisticated approach you can come up with.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement