91.3k views
5 votes
Dr. Rafael's office regularly sees three patients a time. She wants to keep track of their medications and the total number of pill capsules they take when they are prescribed. The program should allow users to enter the three patient names and then keep track of the number of pills capsules taken per patient. If a user tries to enter a patient name that doesn't exist, the program should indicate that the patient is not found. When the user is done (indicated by the user typing exit), the program should ask Would you like to see the total pill amounts for each patient?

User Carin
by
8.6k points

1 Answer

4 votes

# Initialize an empty dictionary to store patient names and their pill amounts

patients = {}

# Loop until the user types "exit"

while True:

# Ask the user for a patient name or "exit"

name = input("Enter a patient name or type 'exit' to quit: ")

# If the user types "exit", break out of the loop

if name == "exit":

break

# If the patient name is not already in the dictionary, add it with a starting pill amount of 0

if name not in patients:

patients[name] = 0

# Ask the user for the number of pill capsules the patient takes

pills = input("Enter the number of pill capsules taken by {}: ".format(name))

# Try to convert the input to an integer, and update the patient's pill amount

try:

pills = int(pills)

patients[name] += pills

except ValueError:

print("Invalid input: please enter a whole number")

# Ask the user if they want to see the total pill amounts for each patient

response = input("Would you like to see the total pill amounts for each patient? (y/n) ")

if response.lower() == "y":

for name in patients:

print("{}: {} pill capsules".format(name, patients[name]))

User MikeKusold
by
8.0k points