Ismaail Mousa
Homework 2 MATH 4332
Problems & Solutions
Problem 1
Write a program that asks the user to enter a word and then capitalizes every other letter of that
word. So if the user enters rhinoceros, the program should print rHiNoCeRoS.
Solution
word = input ( " Enter a word : " )
result = " "
for i in range ( len ( word ) ) :
if i % 2 == 0:
result = result + word [ i ]. lower ()
else :
result = result + word [ i ]. upper ()
print ( result )
Problem 2
Write a program that asks the user to enter their name in lowercase and then capitalizes the first
letter of each word of their name.
Solution
name = input ( " Enter your name in lowercase : " )
words = name . split ()
result = " "
for word in words :
result = result + word [0]. upper () + word [1:] + " "
print ( result )
Problem 3
Ask the user to enter 10 test scores. Write a program to do the following:
(a) Print out the smallest and the second smallest scores.
Ismaail Mousa
(b) If any of the scores is greater than 100, then after all the scores have been entered, print a
message warning the user that a value over 100 has been entered.
(c) Drop the two lowest scores and print out the average of the rest of them
Solution
scores = []
over_100 = False
for i in range (10) :
score = float ( input ( " Enter test score : " ) )
scores . append ( score )
if score > 100:
over_100 = True
if over_100 :
print ( " Warning : A score over 100 was entered . " )
scores . sort ()
smallest = scores [0]
second_smallest = scores [1]
print ( " Smallest score : " , smallest )
print ( " Second smallest score : " , second_smallest )
remaining_scores = scores [2:]
average = sum ( remaining_scores ) / len ( remaining_scores )
print ( " Average after dropping two lowest scores : " , average )
Problem 4
Write a program that asks the user to guess a random number between 1 and 10. If they guess
right, they get 10 points added to their score, and they lose 1 point for an incorrect guess. Give
the user five numbers to guess and print their score after all the guessing is done.
Solution
import random
score = 0
for i in range (5) :
number = random . randint (1 , 10)
guess = int ( input ( " Guess a number between 1 and 10: " ) )
if guess == number :
Ismaail Mousa
score = score + 10
print ( " Correct ! " )
else :
score = score - 1
print ( " Incorrect . The number was " , number )
print ( " Your final score is " , score )
Problem 5(a)
A simple way of encrypting a message is to rearrange its characters. One way to rearrange the
characters is to pick out the characters at even indices, put them first in the encrypted string, and
follow them by the odd characters. For example, the string message would be encrypted as msaeesg
because the even characters are m, s, a, e (at indices 0, 2, 4, and 6) and the odd characters are e,
s, g (at indices 1, 3, and 5).
(a) Write a program that asks the user for a string and uses this method to encrypt the string.
Solution
text = input ( " Enter a string to encrypt : " )
even_chars = " "
odd_chars = " "
for i in range ( len ( text ) ) :
if i % 2 == 0:
even_chars = even_chars + text [ i ]
else :
odd_chars = odd_chars + text [ i ]
encrypted = even_chars + odd_chars
print ( " Encrypted string : " , encrypted )
Problem 5(b)
(b) Write a program that decrypts a string that was encrypted with this method.
Solution
encrypted = input ( " Enter a string to decrypt : " )
n = len ( encrypted )
half = ( n + 1) // 2 # number of even - index characters
Ismaail Mousa
even_chars = encrypted [: half ]
odd_chars = encrypted [ half :]
decrypted = " "
even_index = 0
odd_index = 0
for i in range ( n ) :
if i % 2 == 0:
decrypted = decrypted + even_chars [ even_index ]
even_index = even_index + 1
else :
decrypted = decrypted + odd_chars [ odd_index ]
odd_index = odd_index + 1
print ( " Decrypted string : " , decrypted )