Introduction to Python
davidbpython.com
Projects, Session 2
PLEASE REMEMBER:
|
|||||||||||||||||||||||||||||||||||||||||||||
All requirements are detailed in the homework instructions document. |
|||||||||||||||||||||||||||||||||||||||||||||
2.1 | Notes typing assignment. Please write out this week's transcription notes. The notes are displayed as an image named transcription in each week's project files folder. | ||||||||||||||||||||||||||||||||||||||||||||
This does not need to be in a Python program - you can use a simple text file. |
|||||||||||||||||||||||||||||||||||||||||||||
2.2 | Reverse Number Guessor. | ||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||
(Why is this a "reverse" guessor? The classic Number Guessor reverses the roles of guessor and guessee - see extra credit / supplementary.) |
|||||||||||||||||||||||||||||||||||||||||||||
Starter Code:
import random
number_to_guess = random.randint(1,100) # int, 81 (put in a sample value here,
# then use for notations below)
|
|||||||||||||||||||||||||||||||||||||||||||||
Sample program runs:
I am thinking of a number from 1 to 100. Try to guess it and I'll give you hints about it. Your guess? (q to quit): 50 your guess is LOWER than the number I'm thinking of. Your guess? (q to quit): 75 your guess is LOWER than the number I'm thinking of. Your guess? (q to quit): 87 your guess is HIGHER than the number I'm thinking of. Your guess? (q to quit): 81 you got it! You guessed it in 4 tries. |
|||||||||||||||||||||||||||||||||||||||||||||
I am thinking of a number from 1 to 100. Try to guess it and I'll give you hints about it. Your guess? (q to quit): 40 your guess is HIGHER than the number I'm thinking of. Your guess? (q to quit): 20 your guess is HIGHER than the number I'm thinking of. Your guess? (q to quit): 10 you got it! You guessed it in 3 tries. |
|||||||||||||||||||||||||||||||||||||||||||||
I am thinking of a number from 1 to 100. Try to guess it and I'll give you hints about it. Your guess? (q to quit): 50 your guess is HIGHER than the number I'm thinking of. Your guess? (q to quit): q You chose to quit. Have a great day! |
|||||||||||||||||||||||||||||||||||||||||||||
NOTE 1: if your program loops endlessly ("infinite loop") then click the red square at the left of the run window in PyCharm to break out of it. NOTE 2: some designs immediately convert the user's guess to int() but then can't compare to 'q'. Resolve this by comparing to 'q' first, then converting to int(). NOTE 3: this is a potentially tricky assignment and some students spend too much time trying to get it to work. If you find yourself spending over an hour trying to work out the logic, take a look at the Project Discussion for commentary and an outline. BONUSES: for an added challenge (not required) you may consider these two goals:
HOMEWORK CHECKLIST: all points are required
|
|||||||||||||||||||||||||||||||||||||||||||||
2.3 | Text replacement utility. | ||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||
Starter Code:
sample_text = """And since you know you cannot see yourself,
so well as by reflection, I, your glass,
will modestly discover to yourself,
that of yourself which you yet know not of."""
# [YOUR CODE GOES HERE]
|
|||||||||||||||||||||||||||||||||||||||||||||
Sample program run:
please enter search text: yourself please enter replace text: your glass And since you know you cannot see your glass, so well as by reflection, I, your glass, will modestly discover to your glass, that of your glass which you yet know not of. 3 replacements made. |
|||||||||||||||||||||||||||||||||||||||||||||
Sample program run:
please enter search text: yourself please enter replace text: your Corgi And since you know you cannot see your Corgi, so well as by reflection, I, your glass, will modestly discover to your Corgi, that of your Corgi which you yet know not of. 3 replacements made. |
|||||||||||||||||||||||||||||||||||||||||||||
Please note that you do not need to notate the entire long string value. Just show the first 10-15 characters followed by an ellipsis (...).
|
|||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||
EXTRA CREDIT / SUPPLEMENTARY |
|||||||||||||||||||||||||||||||||||||||||||||
2.4 | (Extra credit / supplementary) Count from 0 up to 100 by 2's (or 3's or 4's, or any number...). Use input() to take one input from the user: an integer "step" value. Then use a while: loop to print out all the values from 0 up to 100 (or less than 100 if the step value doesn't divide evenly into 100). | ||||||||||||||||||||||||||||||||||||||||||||
Note that the while will not use True as a test, but instead the value of a counter that eventually reaches 100. Please see slides for examples of a while loop that does not use True as a test. Do not use 'break' for this solution. The 'while' test must control. |
|||||||||||||||||||||||||||||||||||||||||||||
Sample program runs:
please enter an integer: 3
counting from 0 to 100 by 3's
0
3
6
9
12
15
... (etc., up to 99) ...
92
96
99
|
|||||||||||||||||||||||||||||||||||||||||||||
please enter an integer: 25
counting from 0 to 100 by 25's
0
25
50
75
100
|
|||||||||||||||||||||||||||||||||||||||||||||
Note that if you're having troubling making the count start at 0 or stop counting below 100, consider the order of printing and increasing the value - which should come first?
|
|||||||||||||||||||||||||||||||||||||||||||||
2.5 | (Extra credit / supplementary) Sum a sequence of integers. Use input() to take a value, and convert to int. Use a while loop to sum up all integers between 0 and the submitted value. | ||||||||||||||||||||||||||||||||||||||||||||
Note that the while will not use True as a test, but instead the value of a counter that eventually reaches and the submitted value. Please see slides for examples of a while loop that does not use True as a test. Do not use 'break' for this solution. The 'while' test must control. |
|||||||||||||||||||||||||||||||||||||||||||||
please enter an int: 2
sum from 1 to 2 is 3
|
|||||||||||||||||||||||||||||||||||||||||||||
please enter an int: 3
sum from 1 to 3 is 6
|
|||||||||||||||||||||||||||||||||||||||||||||
please enter an int: 4
sum from 1 to 4 is 10
|
|||||||||||||||||||||||||||||||||||||||||||||
please enter an int: 5
sum from 1 to 5 is 15
|
|||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||
2.6 | (Extra credit / supplementary) Number Guessor. Write a program that attempts to guess a number from 1 to 100 that the user has chosen and is keeping secret. To guess the number, the program asks the user a series of guesses and questions, narrowing down the possibilities by half each time. The program will: | ||||||||||||||||||||||||||||||||||||||||||||
a. Ask the user to think of a number from 1 to 100 b. Ask the user if the number is 50 (i.e., halfway between 1 and 100 c. If the user answers "no", ask the user if the number is higher or lower than 50 d. If lower, ask the user if the number is 25 (i.e., halfway between 1 and 50) e. If higher, ask the user if the number is 75 (i.e., halfway between 50 and 100) f. Return to question c -- if the guess is incorrect, ask the user if the number is higher or lower than 25 (or 75) g. Continue halving the possibiltiies and guessing the number until the user reports that the guess is correct. |
|||||||||||||||||||||||||||||||||||||||||||||
Sample program run:
Think of a number between 100 and 0, and I will try to guess it. Hit [Enter] to start. is it 50 (yes/no/quit)? no is it higher or lower than 50? higher is it 76 (yes/no/quit)? no is it higher or lower than 76? lower is it 63 (yes/no/quit)? no is it higher or lower than 63? lower is it 57 (yes/no/quit)? no is it higher or lower than 57? lower is it 54 (yes/no/quit)? no is it higher or lower than 54? lower is it 52 (yes/no/quit)? no is it higher or lower than 52? lower is it 51 (yes/no/quit)? yes I knew I could do it! |
|||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||
2.7 | (Extra credit / supplementary) Use while to find prime numbers in a range of numbers. Ask the user for an integer, and display all prime numbers between 2 and the user's number. (Note this assignment must use while and not range(), to highlight this week's features.) | ||||||||||||||||||||||||||||||||||||||||||||
Sample program runs:
* Prime Numbers *
Please enter max integer: 5
2
3
5
|
|||||||||||||||||||||||||||||||||||||||||||||
* Prime Numbers *
Please enter max integer: 20
2
3
5
7
11
13
17
19
|
|||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||
2.8 | (Extra credit / supplementary) Chicken, Fox and Grain game. This is a rather lengthy assignment using while, break and continue, as well as numerous if tests comparing strings and numbers. (In fact, it requires a great deal more if/then/else than might be needed if we were to use some upcoming Python features. But, it provides an interesting challenge using this sessions's features.) | ||||||||||||||||||||||||||||||||||||||||||||
The program is an implementation of a well-known logic puzzle: A farmer has to transport a fox, a chicken and a bag of grain across a river using a rowboat, but the rowboat has room for only one of the items besides the farmer. Therefore when crossing the river, the farmer usually has to leave two of the items alone on one of the shores (the farmer can also cross alone in the boat). The problem is that if the fox is left alone with the chicken, the fox will eat the chicken, and if the chicken is left alone with the grain, the chicken will eat the grain. How can the farmer move all three items across the river without losing any of them? |
|||||||||||||||||||||||||||||||||||||||||||||
Sample program runs:
**THE FOX, THE CHICKEN AND THE GRAIN** You are on the east bank of a river. You must transport a fox, a chicken and a bag of grain to the west bank. However, you can only carry one item at a time. Good luck! you now are on the east bank. the chicken is on the east bank. the fox is on the east bank. the grain is on the east bank. What would you carry in the seat of the boat ([Enter] for nothing, q to quit)? xx sorry, I don't recognize 'xx'. Try again. you now are on the east bank. the chicken is on the east bank. the fox is on the east bank. the grain is on the east bank. What would you carry in the seat of the boat ([Enter] for nothing, q to quit)? [pressed Enter alone, meaning carry nothing] oops, the fox got the chicken! |
|||||||||||||||||||||||||||||||||||||||||||||
**THE FOX, THE CHICKEN AND THE GRAIN**
You are on the east bank of a river.
You must transport a fox, a chicken
and a bag of grain to the west bank.
However, you can only carry one item
at a time. Good luck!
you now are on the east bank.
the chicken is on the east bank.
the fox is on the east bank.
the grain is on the east bank.
What would you carry in the seat of the boat ([Enter] for nothing,
q to quit)? fox
oops, the chicken got the grain!
|
|||||||||||||||||||||||||||||||||||||||||||||
**THE FOX, THE CHICKEN AND THE GRAIN** You are on the east bank of a river. You must transport a fox, a chicken and a bag of grain to the west bank. However, you can only carry one item at a time. Good luck! you now are on the east bank. the chicken is on the east bank. the fox is on the east bank. the grain is on the east bank. What would you carry in the seat of the boat ([Enter] for nothing, q to quit)? chicken ...carrying the chicken to the west bank... you now are on the west bank. the chicken is on the west bank. the fox is on the east bank. the grain is on the east bank. What would you carry in the seat of the boat ([Enter] for nothing, q to quit)? [pressed Enter alone, meaning carry nothing] ...traveling to the east bank... you now are on the east bank. the chicken is on the west bank. the fox is on the east bank. the grain is on the east bank. What would you carry in the seat of the boat ([Enter] for nothing, q to quit)? fox ...carrying the fox to the west bank... you now are on the west bank. the chicken is on the west bank. the fox is the west bank. the grain is on the east bank. What would you carry in the seat of the boat ([Enter] for nothing, q to quit)? [pressed Enter alone, meaning carry nothing] oops, the fox got the chicken! |
|||||||||||||||||||||||||||||||||||||||||||||