Approaching a New Programming Problem

Don't Just Jump Into Code!

Perfect Squares

1
4
16
25
4 perfect squares found

Potential Solutions

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? →

  1. we could start counting from 1… and square each number until the square is greater than the input
  2. count up to the number and check if the square root of that number is a whole number (this seems a little trickier)

Let's Write Up a Plan

A Flow Chart

The utility in this case may be up for debate, but for more complex programs, flow charts are indispensable! Let's try it! →

Perfect Squares

Pseudocode

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
"""

Our First Draft

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")

Another Version

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")

Dice Warz!

Let's try a more complicated example. Write a game:

First… are these requirements sufficient? What additional questions might you ask? →

Example Output

"""
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
"""

Let's flesh out those requirements:

"""
* 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
"""

Design Your Program

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")

Don't Forget the Banner!

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)