1
4
16
25
4 perfect squares found
For math heavy problems like this, I'll make sure that there's a reasonable brute force (a simple, usually iterative and not necessarily efficient) solution. What are some ways that we can solve this problem? →
The utility in this case may be up for debate, but for more complex programs, flow charts are indispensable! Let's try it! →
Let's also try writing some pseudocode →
"""
ask for input
start counting from 1...
while the count squared is less than the input
print out the count squared
print out the count
"""
Let's get our first version done. →
count = 1
num = int(input("Enter a number\n>"))
while count ** 2 < num:
print(count ** 2)
count += 1
print(str(count) + " squares found")
Our count was wrong, let's fix it. →
count = 1
num = int(input("Enter a number\n>"))
while count ** 2 < num:
print(count ** 2)
count += 1
print(str(count - 1) + " squares found")
Let's try a more complicated example. Write a game:
First… are these requirements sufficient? What additional questions might you ask? →
"""
Enter a command: roll or quit
>roll
Player rolled: 4
Computer rolled: 2
Player won!
Player: 1 Computer: 0
Enter a command: roll or quit
>quit
Bye! The final score was...
Player: 1 Computer: 0
"""
"""
* It's a text game - print out ASCII art!
* Ask for a command (there are only two possible commands: roll or quit)
* If command is roll, roll random dice for computer and player
* Print out the rolls
* Determine who wins (3 states: computer wins, player wins or tie)
* For each state add scores appropriately,
* Print out who won and the resulting scores (keep track of scores)
* After each roll, go back to #2 to ask for a command again
* (keep doing this until the user types in quit)
* If command is quit, print out the score and then exit the game
* If the command is not quit or roll,
* say "I don't know that command" and print the score
* ...continue asking for a command
"""
Write some pseudocode.
keep track of scores
ask the user for input
while the user hasn't quit
if the user's command was roll
roll dice for both the computer and the user
figure out who one and add scores
otherwise, if the command was quit, quit the game
otherwise, if it's an unknown command, print unknown command
print scores
import random
player_score, computer_score, play_game = 0, 0, True
while play_game:
command = input("Enter a command: roll or quit\n>")
if command == 'roll':
player_roll = random.randint(1, 6)
computer_roll = random.randint(1, 6)
print("Player rolled: " + str(player_roll))
print("Computer rolled: " + str(computer_roll))
if player_roll > computer_roll:
print("Player won!")
player_score += 1
elif player_roll < computer_roll:
print("Computer won!")
computer_score += 1
elif player_roll == computer_roll:
print("Tie!")
elif command == 'quit':
play_game = False
print("Bye! The final score was...")
else:
print("I don't know that command")
print("Player: " + str(player_score) + " Computer: " + str(computer_score) + "\n")
We're probably going to have to stop putting code in slides from here on in. I skipped on the banner, and it still didn't fit!
banner = """ ------
/ /| ~Dice Warz~
------| |
| | |
| |/
------
"""
print(banner)