Conditional Execution
Read the first half of of "Think Python" chapter five on Conditionals and Recursion, 5.1-5.5.
PROGRAM TWO
Write a guess the number game. The program will pick a random number between 1 and 50. The user will try to guess the number. After they enter a guess the program will tell them if they are right, if their guess was too low, or if their guess was too high. When they guess the number the program will tell them how many guesses it took to get the number. For some extra fun have the program ask if they want to play again and repeat the game until they say no.
We haven't read about looping yet and you'll need a while loop or two in this program. A while loop will do the nested code in it until the statement that follows it is False. For example this code:
x = 0
while x < 10:
print(x)
x = x+1
will print the numbers from 0 to 9. To make sure you understand it, modify the code to print the numbers 1 to 10.
You will also need to import the random module, look up the randint function. We also need to covert the user input from a string to an integer so look up the int function. Most of the documentation for that function will be hard to understand but give it a try and we'll talk about it. What does this statement do: int("10").
Fun Stuff:
Human Resources Machine on the iPad years 6,7,8
- Write a statement in the interpreter that prints out the last three digits of the number 10245 using the modulus operator.
- In the interpretor set x=2, y=5 and evaluate the following: x==y, x!=y, x<y, x>y, x<=y, x>=y. Now in your head set x=5, y=5 and figure out how those six statements would evaluate. Run them through the interpreter and see if you were right.
- Come up with a value for n that would make the following statement True: (n%2==0 and n%3==0). Test this in the interpreter.
- Would the following statements be True or False for x=3 and y=4, test them after you get your answer to verify:
- not x>y
- x<y or x==y
- not x==y
- x<y and x<=y
- x<y or x<=y
PROGRAM TWO
Write a guess the number game. The program will pick a random number between 1 and 50. The user will try to guess the number. After they enter a guess the program will tell them if they are right, if their guess was too low, or if their guess was too high. When they guess the number the program will tell them how many guesses it took to get the number. For some extra fun have the program ask if they want to play again and repeat the game until they say no.
We haven't read about looping yet and you'll need a while loop or two in this program. A while loop will do the nested code in it until the statement that follows it is False. For example this code:
x = 0
while x < 10:
print(x)
x = x+1
will print the numbers from 0 to 9. To make sure you understand it, modify the code to print the numbers 1 to 10.
You will also need to import the random module, look up the randint function. We also need to covert the user input from a string to an integer so look up the int function. Most of the documentation for that function will be hard to understand but give it a try and we'll talk about it. What does this statement do: int("10").
Fun Stuff:
Human Resources Machine on the iPad years 6,7,8
Comments
Post a Comment