Pet Name Generator
Write a program that randomly selects a pet name from an array. The program should ask the user what pet they need a name for (dog, cat, or fish) using input validation. The program will then ask how many name options they want to enter, which are then stored in a data structure. Once all names have been added, the program will then choose a name at random for the pet.
What type of animal? (dog, cat, fish): dog How many name options do you want? 3 Enter name option 1: Max Enter name option 2: Fido Enter name option 3: Caeser Your dog should be called Max!
DECLARE names[STRING] // array for storing name choices
DECLARE animalType STRING
DECLARE numberOfNames INTEGER
DECLARE chosenName STRING
SEND "What type of animal do you need a name for?"
RECEIVE animalType
SEND "How many name options do you want to enter?"
RECEIVE numberOfNames
SET names TO array of STRING with numberOfNames size
FOR i FROM 0 TO numberOfNames - 1 DO
SEND "Enter name option " + (i + 1)
RECEIVE names[i]
END FOR
SET chosenName TO RANDOM item from names
SEND "Your " + animalType + " should be called " + chosenName + "!"
import random
pet_type = input("What type of animal? (dog, cat, fish): ")
num_names = int(input("How many name options do you want? "))
names = [str] * num_names
for i in range(num_names):
names[i] = input("Enter name option " + str(i + 1) + ": ")
chosen_index = random.randint(0, num_names - 1)
print()
print("Your", pet_type, "should be called", names[chosen_index] + "!")
import random
# Ask user for pet type with input validation
pet_type = input("What type of animal? (dog, cat, fish): ")
# Ask how many name options they want
num_names = int(input("How many name options do you want? "))
# Create array and store names
names = [str] * num_names
for i in range(num_names):
names[i] = input("Enter name option " + str(i + 1) + ": ")
# Pick a random name
chosen_index = random.randint(0, num_names - 1)
# Display result
print()
print("Your", pet_type, "should be called", names[chosen_index] + "!")
Extension
Extend the program to include three more facts. Then implement input validation, which asks the user if they want another fun fact. If the user says yes, another fact should be picked at random and displayed to the user. This should be repeatable until the user says no.
What type of animal? (dog, cat, fish): dogg Your pet must be either a dog, cat or fish. Please enter dog, cat, or fish: dog How many name options do you want? 3 Enter name option 1: Max Enter name option 2: Fido Enter name option 3: Caeser Your dog should be called Fido!