1. "Rock-paper-scissors is a hand game that is played by two people. The players count to three in unison and simultaneously "throw" one of three hand signals that correspond to rock, paper or scissors. The winner is determined by the rules:

  • Rock smashes scissors
  • Scissors cuts paper
  • Paper covers rock

Rock-paper-scissors is a surprisingly popular game that many people play seriously (see the Wikipedia article for details).

Write a Python program to ask the user's choice of Rock paper scissors. The program then, randomly, chooses a choice for itself (the computer) and then compares it with the users choice. The final output should show, the users choice, the programs choice and the winner.

Sample runs are shown below:

Enter your choice: rock
Computer chooses scissors
You, the user, win!

Enter your choice: paper
Computer chooses rock
I, the computer, win!

Enter your choice: scissors
Computer chooses scissors
User and computer tie!

2. Write a program where the user and the program/computer trade places in the number guessing game. Prompt the user for a number (between 1 and 100, inclusive) that the program/computer has to guess. Keep track of the number of iterations it takes for the computer to guess the number. Sample runs are shown below:

Enter number to be guessed: 88
You guessed 88, and it took the program 3 iterations

Enter number to be guessed: 55
You guessed 55, and it took the program 19 iterations

3. Write a function that accepts a line of text and a single letter as input and returns the number of times the letter is the first character of a word Sample runs are given below:

Enter your line of text: Where the mind is without fear and the head is held high
Enter your letter to use: h
Your letter h occurs as the first letter: 3 times

4. A palindrome is string that reads the same forwards and backwards. Write a Python program that reads in a string and determines whether the string is a palindrome or not. Note that a sentence that reads the same forwards and backwards by resequencing the spaces is also a palindrome and your program should consider this.

Sample runs are shown below:

Enter a string: mom
"mom" is a palindrome

Enter a string: apple
"apple" is not a palindrome

Enter a string: A nut for a jar of tuna
"A nut for a jar of tuna" is a palindrome

Enter a string: A nut for a jar of salmon
"A nut for a jar of salmon" is not a palindrome

5. Write a program to request a file name from the user and calculate the following statistics of the contents of the file:

  • Number of lines
  • Number of words
  • Number of characters
  • Average length of a word

In this problem use the following definitions:

  • A line is a sequence of characters that end with a newline (\n) character
  • A word bounded by one or more spaces (or \n) on either side of it (or both sides)
  • A character is any single length string, e.g. 'a', -, etc. but not a space (or white space)

An example file called robertfrost.txt is included in the homework files

Sample Run

What is the filename: robertfrost.txt
Number of lines: 4
Number of words: 27
Number of characters: 132
Average length of a word: 3.8

Note: If your file statistics are different from the answer I have given above, please explain in your notes/markdown script how you arrived at your answers. For example if you use the readlines() function then it will count the last line which does not end with a newline (\n) as a line, that is fine as long as you understand it and are able to explain.

Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.