Introduction to Python

davidbpython.com




Project Warmup Exercise Solutions, Session 2



EXERCISES RELATED TO Reverse Number Guessor

 
Ex. 2.1 Write a program that takes user input and prints whatever the user typed.
Suggested Solution:
user_input = input('please enter some text:  ')

text = f'you just wrote:  "{user_input}"'

print(text)

The exercise simply demonstrates the use of input(). You should be able to use this function from memory as it's extremely common.

 
Ex. 2.2 Write a program that takes user input and compares it to the word 'quit'. If input is equal to 'quit', the program prints quitting..., otherwise it prints not quitting....
Suggested Solution:
text = input("please enter some text ('quit' to quit):  ")

if text == 'quit':
    print('quitting...')
else:
    print('not quitting...')

This exercise shows you a standard approach to deciding whether user input is correct. We're using the == string comparison operator to see if the user's input (from input(), a string) is equal to the word 'quit'.

 
Ex. 2.3 Extending the above program, put the input() call inside a while True: loop. If the user asks to quit, break out of the loop. If not, allow the loop to return to the top (remember that all while loops automatically stop looping when the test b False; while True loops only stop looping when they hit break).
Suggested Solution:
while True:
    text = input("please enter some text ('quit' to quit):  ")

    if text == 'quit':
        print('quitting...')
        break
    else:
        print('not quitting...')
 
Ex. 2.4 Write a program that asks user to enter a number and sees if it is greater than 0, less than 0 or equal to 0. (Remember that if can be used with else to have Python do one thing or another depending on the result of the test.)
Suggested Solution:
ui = input('please enter a number:  ')

iui = int(ui)

if iui < 0:
    print('your value is less than 0.')
elif iui > 0:
    print('your value is greater than 0.')
else:
    print('your value is equal to 0.')
 
Ex. 2.5 Use input() to request the user to enter a number, and then test to see whether it is all digits (hint: use the str .isdigit() method in an if test). Simply print a success message if all digits, a failure message if no.
Suggested Solution:
user_input = input('please enter an integer:  ')

if user_input.isdigit():
    print('THANKS FOR THE INTEGER!')

else:
    print('that was NOT an integer!')
 

EXERCISES RELATED TO TEXT REPLACEMENT HOMEWORK

 
Ex. 2.6 Use the string .count() method to count the number of times 'or' appears in the following text: I am happy or sad or angry or mad or generous or stingy.
Suggested Solution:
msg = 'I am happy or sad or angry or mad or generous or stingy.'

search_string = 'or'

count = msg.count(search_string)

print(count)
 
Ex. 2.7 Modifying the above program, take user input of a string to count, and then report the number of occurrences in the sentence.
Suggested Solution:
msg = 'I am happy or sad or angry or mad or generous or stingy.'

search_string = input('please enter a string to search:  ')

count = msg.count(search_string)

print(count)
 
Ex. 2.8 Taking a user input "substring", replace the substring in the string with 'x's.
Suggested Solution:
msg = 'I am happy or sad or angry or mad or generous or stingy.'

print(msg)

search_string = input('please enter a character to replace: ')

newstring = msg.replace(search_string, 'x')

print(newstring)
 

EXERCISES RELATED TO EXTRA CREDIT / SUPPLEMENTARY while LOOP HOMEWORK (you can safely skip these until after the homework)

 
Ex. 2.9 Based on the earlier exercise in which we said THANKS FOR THE INTEGER!, place your whole program inside a while True: block. If the input is all digits (i.e., an integer), include the print statement indicating success, and follow it with a break statement to leave the loop. As you know, if you do not break, at the end of the block while's "automatic return" will return to the top of the block automatically and your block will take input() again.
Suggested Solution:
while True:

    user_input = input('please enter an integer:  ')

    if user_input.isdigit():
        print('THANKS FOR THE INTEGER!')
        break
    else:
        print('that was NOT an integer!')
 
Ex. 2.10 Use a while loop to count from 1 to 10. Use an integer counter which you "increment" upon each iteration of the loop. Print each new value of the integer.
Suggested Solution:
count = 1
while count <= 10:
    print(count)
    count = count + 1
 
Ex. 2.11 Similar to the above program, use a while loop and a counter to print "Happy birthday to you!" 10 times (do not print the integer count in this version).
Suggested Solution:
count = 1
while count <= 10:
    print('Happy birthday to you!')
    count = count + 1
 
Ex. 2.12 Modify the above program to take user input with input() and print the message that many times.
Suggested Solution:
user_input = input('how many times should I greet you?  ')

count = 1
while count <= int(user_input):
    print('Happy birthday to you!')
    count = count + 1
 
Ex. 2.13 Modify the above program to test the user input to make sure it is a usable integer. (Hint: use str.isdigit()).
Suggested Solution:
user_input = input('how many times should I greet you?  ')

if not user_input.isdigit():
    exit('error:  please enter an integer')

user_input = int(user_input)
count = 1
while count <= user_input:
    print('Happy birthday to you!')
    count = count + 1
 
[pr]