Computer Science

# Lab 21 - Input Validation

Before doing this lab, make sure you have byu-pytest-utils version 0.3.6 or later.

## Activities

For each of these activities, write a Python program that meets the requirements.

This means that each solution should be callable from the command line. You should use if __name__ == '__main__': as part of your solution.

When testing your programs, you need to be sure your program prints lines exactly as they are shown in the assignment. If you miss a space or misspell a word, the test will fail. Check the output of the test and click on <Click to see difference> to see how your program differs from what we are expecting.

### sum_twenty.py

sum_twenty.py is a program that requires the user to input a comma-delimited list of positive integers that sum up to twenty.

### Requirements

• Prompt the user for input in the format Enter Response:
• If the response is not a comma-delimited series of positive integers, tell the user Response Invalid. Please respond with a comma-delimited series of positive integers. and prompt the user for a new response. This excludes responses with spaces between the numbers or responses with letters.
• If the integers in the response don’t add up to twenty, tell the user Response Invalid. Integers do not add to 20. and prompt the user for a new response.
• If the response is a comma-delimited series of positive integers that add up to twenty, end the program.

### Example

python sum_twenty.py

Enter Response: not numbers
Response Invalid. Please respond with a comma-delimited series of positive integers.
Enter Response: -1,10,11
Response Invalid. Please respond with a comma-delimited series of positive integers.
Enter Response: 5,7,9
Response Invalid. Integers do not add to 20.
Enter Response: 1,45,9
Response Invalid. Integers do not add to 20.
Enter Response: 5,6,9

### valid_sentence.py

valid_sentence.py is a program that requires the user to input a “valid” sentence. A valid sentence is a string that starts with a capital, ends with punctuation (non alphanum/space), and contains all 5 vowels (aeiou). (eg. "This apple is out.")

### Requirements

• Prompt the user for a sentence using the format Enter Sentence:
• If the input doesn’t start with a capital letter, tell the user Sentence must start with a capital.
• If the input doesn’t end with punctuation (non alphanum/space), tell the user Sentence must end with punctuation.
• If the input doesn’t contain all 5 vowels (aeiou; ignore case), tell the user Sentence must include all 5 vowels (aeiou).
• If the sentence is invalid, only notify the user of the first mistake encountered. Then ask them to Please try again. and prompt them for a new sentence.
• If the sentence is valid, end the program.

### Example

python valid_sentence.py

Enter Sentence: i dont start with an uppercase letter.
Enter Sentence: I dont contain all vowels.
Sentence must include all 5 vowels (aeiou).
Enter Sentence: I dont end with punctuation
Sentence must end with punctuation.
Enter Sentence: i have lots of problems
Enter Sentence: I am a fabulous sentence!

### tic_tac_toe.py

tic_tac_toe.py is the beloved game that we are all familiar with. Tic-tac-toe is perfect for this lab because it is played on a 3x3 grid. To play the game, two players take turns marking the grid and the first player to get 3 of their marks in a row (up, down, or diagonally) is the winner. When all 9 squares are full, the game is over.

### Requirements

• Before each turn, display the grid to the players.
• X starts the game. Prompt player X for a coordinate in the format X's Turn:
• The input should be in the format x,y where x is the x coordinate and y is the y coordinate.
• If the coordinate isn’t valid, tell the player Invalid coordinate. Please enter a coordinate of the format '1,1'. and prompt the same player for a new coordinate.
• If the coordinate is already occupied, tell the player Coordinate already occupied. Please enter a new coordinate. and prompt the same player for a new coordinate.
• Then it is O’s turn. Prompt player O for a coordinate in the format O's Turn:  and ensure that the input is valid.
• The players will continue take turns making marks until someone wins the game or all 9 squares are full.
• After a players turn, check to see if that player has won the game with three in a row (down, across, or diagonally).
• If the player won, display the grid and declare the winner in the format X Wins! or O Wins! depending on the winner. Then end the program.
• If the player didn’t win, it is the next players turn.
• If all 9 squares are full, that is called a cat’s game. If this happens, show the grid and tell the players Cats Game! and end the program.

### Example

python tic_tac_toe.py

. . .
. . .
. . .
X's Turn: 1,1
. . .
. X .
. . .
O's Turn: 0,0
O . .
. X .
. . .
X's Turn: 1,0
O X .
. X .
. . .
O's Turn: 0,0
O's Turn: 1,2
O X .
. X .
. O .
X's Turn: 0,3
Invalid coordinate. Please enter a coordinate of the format '1,1'.
X's Turn: 0,2
O X .
. X .
X O .
O's Turn: words
Invalid coordinate. Please enter a coordinate of the format '1,1'.
O's Turn: 37
Invalid coordinate. Please enter a coordinate of the format '1,1'.
O's Turn: 42
Invalid coordinate. Please enter a coordinate of the format '1,1'.
O's Turn: 2,0
O X O
. X .
X O .
X's Turn: 0,1
O X O
X X .
X O .
O's Turn: 2,2
O X O
X X .
X O O
X's Turn: 2,1
O X O
X X X
X O O
X Wins!

sum_twenty.py5 points
valid_sentence.py5 points
tic_tac_toe.py20 points