Computer Science

Lab 16 - Input

Activities

anagrams.py

anagrams.py is a simple word game using anagrams. An anagram is a series of letters that can be used to form another word. For example, dgo is an anagram of dog. In this game, a word is chosen randomly from a word list, then the scrambled letters are shown to the user. The user supplies guesses of the original word until he or she gets it right.

Requirements

• The filename for the word list is provided as the first command-line argument (sys.argv[1])
• The program should select a word at random from the word list file
• The user should be prompted for a word guess with the text: Guess:  (note the trailing space)
• If the guess is incorrect, the program should print the text Incorrect and prompt the user for a new guess
• If the guess is correct, the program should print the text Correct! and then display the text Total Guesses:  followed by the number of guesses the user provided.
• To get a list of scrambled letters, use:
scrambled = random.sample(word, len(word))

Example

Because the program uses a random word and scrambles the letters randomly, what follows is an example of how the program could be played.

python anagrams.py test_files/words.txt

t o s c a
Guess: scato
Incorrect
Guess: stoca
Incorrect
Guess: tacos
Correct!
Total Guesses: 3

math_practice.py

math_practice.py is a math game that helps us practice addition. The program displays some randomly generated addition expressions (eg. 7 + 3) and the user tries to solve them. After solving all the expressions, the user gets a score and is shown the expressions they got right and wrong.

Requirements

• The number of expressions is provided as the first command-line argument (sys.argv[1])
• Before each expression is shown, display which question the user is on using the format Question 1:  (note the trailing space)
• Use random.randint(lower_bound, upper_bound) to generate two numbers to be used in the addition expression
• lower_bound is provided as the second command-line argument (sys.argv[2])
• upper_bound is provided as the third command-line argument (sys.argv[3])
• Present the expression in the format number1 + number2 where number1 and number2 are the randomly generated numbers.
• The user should be prompted for an answer with the text: Answer:  (note the trailing space)
• After answering all the questions, the user is presented how many points they scored (eg. You Scored 5 Points!), the expressions they calculated correctly and the expressions they calculated incorrectly.
• The expressions should be presented in a comma-delimited list (comma followed by space between each expression).

Important: the output of your program should exactly follow the format demonstrated in the example below.

Example

Because the program uses randomly generated numbers, what follows is an example of how the program could be played.

python math_practice.py 3 1 10

Question 1:
7 + 1
Question 2:
5 + 4
Question 3:
8 + 2
You Scored 2 Points!
Correct Answers: 7 + 1, 8 + 2
Incorrect Answers: 5 + 4

flashcards.py

flashcards.py is a program that helps you study. The user gives the program a text file containing flashcards and the program then presents the flashcards to the user in a random order. If the user answers wrong, then they are shown the correct answer.

Requirements

• The filename for the flashcards is provided as the first command-line argument (sys.argv[1])
• The flashcards are formatted in question/answer pairs. As an example, one line in the file might look like: this is a question?|answer
• The program presents the flashcards in a random order
• The user is prompted to answer each flashcard with the text: Guess:  (note the trailing space)
• If the user gets the answer correct, display Correct! and add one point to their score.
• If incorrect, display Incorrect. and show the correct answer in the format Correct answer: _answer here_
• After answering all the flashcards, their score is presented in the format of a ratio (eg. You got 3/5 correct.)
• In order to randomize the order the flashcards, use:
shuffled_flashcards = random.sample(flashcards, len(flashcards))

Example

Flashcard example text file:

flashcards.txt

What do you call a bagel that can fly?|A plain bagel
What are two things you can't eat for breakfast?|Lunch and Dinner
What word becomes shorter when you add two letters to it?|Short
Before Mt. Everest was discovered, what was the tallest mountain in the world?|Mt. Everest (it just wasn't discovered yet)

Because the program displays the flashcards in a random order, what follows is an example of how the program could be played.

python flashcards.py flashcards.txt

What are two things you can't eat for breakfast?
Guess: Lunch and Dinner
Correct!
What do you call a bagel that can fly?
Guess: I dont know
Incorrect.
Before Mt. Everest was discovered, what was the tallest mountain in the world?
Guess: Mt. Shasta
Incorrect.
Correct answer: Mt. Everest (it just wasn't discovered yet)
What word becomes shorter when you add two letters to it?
Guess: Short
Correct!
You got 2/4 correct.

anagrams.py10 points
math_practice.py10 points
flash_cards.py10 points