0

I’m having an issue with part of my code. I’ve created a dictionary below

names_meaning = {
  "Taiwo": "First of a twin",
  "Tariro": "Hope" ,
  "Joseph": "God will add",
  "Joel": "YHWH (or LORD) is God",
  "Jude": "Praised",
  "Hannah": "Favour or Grace"
     }

I created a new list from the keys in the dictionary above.

family_names = list(names_meaning.keys())

Then I’m asking the user for an input and I’ve called it below.

name = input("Please type a name to know the meaning: ").capitalize()

From the user’s input, I am checking to see if it is in a family_names.

if name not in family_names:
    print(f"You can only pick a name from this list of names {family_names}.")
    name = input("Please type a name to know the meaning: ").capitalize()
else:
    print(f"{name} means: {names_meaning[name]}\n")

The check works only once. I need the check to continue saying "Please type a name to know the meaning: " until the user types in the correct name. What am I doing wrong?

1 Answers1

0

use a while loop to continue asking for input until it is valid, then break out of the loop.

names_meaning = {
"Taiwo": "First of a twin",
"Tariro": "Hope",
"Joseph": "God will add",
"Joel": "YHWH (or LORD) is God",
"Jude": "Praised",
"Hannah": "Favour or Grace",
}

family_names = list(names_meaning.keys())

name = input("Please type a name to know the meaning: ").capitalize()

run = True
while run:
    if name not in family_names:
        print(f"You can only pick a name from this list of names 
{family_names}.")
        name = input("Please type a name to know the meaning: ").capitalize()
        if name in family_names:
            print(f"{name} means: {names_meaning[name]}\n")
            break
    else:
        print(f"{name} means: {names_meaning[name]}\n")
        break
Caleb Bunch
  • 109
  • 6