Advertisement
AquaBlitz11

Gunn coding homework for Jan 24, 2021

Jan 22nd, 2021
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.60 KB | None | 0 0
  1. ## A. Primes
  2.  
  3. 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.
  4.  
  5. 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.
  6.  
  7. ## B. Guess The Number
  8.  
  9. 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.
  10.  
  11. ### Random number generation
  12.  
  13. In the first line of your code, write (excluding the `s):
  14. import random
  15.  
  16. Anywhere in your code you want to generate a random number, write:
  17. variable_name = random.randint(a, b)
  18. where a and b are the boundary values. For example,
  19. num = random.randint(1, 5)
  20. generates a value of 1,2,3,4 or 5 for num.
  21.  
  22. ## C. Also Guess The Number
  23.  
  24. 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.
  25.  
  26. You should try a simple strategy first where the computer starts guessing from 1, 2, 3, ...
  27. 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