Study guide no 1 Flashcards
What does random.randint(low, high) do in the play() function?
Selects the number that the player has to guess
Suppose you change the value of high to 20 but made no other changes to the code? What error would this introduce to the code? How could you fix this error?
It would make the guess a number from one to ten text inaccurate and lead to people not guessing above 10. I would just change the 10 to 20.
How many chances does the player have to guess the number correctly? Where is this defined in the code?
- Defined under the play function(tries=5)
Why is the variable guess initially set to -1 in the play() function? What other values could also work here? What potential effect could setting the value to a positive number like 50 have on our code? That is, why did we decide a negative value was probably better than a positive value?
So it’s different from any other number. 1-10 could work here, 1-20 would be to high
Why is it a good idea to have get_guess() as a separate function rather than just have the input statement in the game loop within play()?
It makes things a whole lot easier.
What is the purpose of the guess = int(guess) statement in the get_guess() function?
To make the guess an integer
What error will occur if a player inputs a non-integer guess? Explain the error message in your own words.
It’s a type error. I think that it will say that the symbol other than the number has been defined.
What is the purpose of the tries -= 1 statement in the play() function?
It subtracts one from the tries
Where in the code does it check to see if the player’s guess is correct?
It’s under the play function(if guess == number:
print(“Got it!”))
Where in the code does it check to see if a player has run out of guesses?
It’s under the play function (else:
print(“You’re such a loser!!!!!!!!”))
What happens when the player runs out of tries without guessing correctly?
The game ends.
What is the purpose of the play_again() function?
To make sure that the code doesn’t end if the player doesn’t want it to and it gives the player the option to try again.
What type of loop does play_again() use? What is the purpose of the loop?
Its a while loop. It checks if the player wants to play again and if they say yes or no the computer either restarts the code or stops the code. If the player types anything other than that the computer says huh.
Which modification would be the BEST way to make the play_again() function case-insensitive so it accepts responses like “YES” or “No” in any letter case?
lower().
What is the purpose of the input() statement in the show_start_screen() function?
When the player inputs anything, the code starts.
What is the value of the running variable when the show_end_screen() function is called in the main() function?
Suppose you played the game 2 times and then said you didn’t want to play again. In what order would each function of the functions main(), show_start_screen(), show_end_screen(), play(), and play_again() get executed? (Some will get executed more than once.)
main(), show_start_screen(), play(), play again(), play(), play_again(), show_end_screen()
What are TWO reasons to include header comments at the beginning of a program?
It shows the date worked on and it shows who made the code.
How many blank lines should go between functions?
2 lines should go in between functions.
When should we put single blank lines in our code?
It’s between 2 separate codes in the same function that dont so the same thing.
What would happen if we never used blank lines in our code?
The code will get hard to read and the code won’t work.
How many spaces should all indents be?
It should be 4 spaces.
Why do we always put spaces around operators such as +, =, and ==? What would happen if we didn’t?
It makes the code easier to read and if we didn’t do this it would make code hard to read
What does import random do in the script?
It imports the random module so you could choose the random number
Where should all import statements be included in a script?
They should be at the very top
Why are modules like random imported? That is, why can’t we just use functions like randint directly? You should know TWO reasons imports are used.
People can’t use functions like randint because it isn’t built within python and you could use the random module so you don’t have to retype the entire function if its already given
What is the purpose of if __name__ == ‘__main__’: in the script? Give TWO reasons it is used.
The purpose of this line is to play the game and that main is only called if the script is running.
What is the maximum number of guesses a player might need to guarantee they find the correct number using an optimal guessing strategy? How can we determine this value? For example, what is the maximum number of guesses required if the game was played with a range of 1 to 200? (You should be able to determine this on your fingers with simple mental math.)
By using the power of 2, you should find the right amount of guesses. The best one would be 2^8 for a range from 1^200.