0

how to put a loop that asks user again and again to input till it enters correct country

population ={
    'china': '143',
    'india': '136',
    'usa': '32',
    'pakistan': '21'
}
population['bhutan'] = 2

print(population['china'])


country = input("enter countri name-")
country.lower()
if country in population:
    print(f"population {country} is: {population[country]} crore")
else:
   print("input again")
   country = input("enter countri name-")

if country in population:
    print(f"population {country} is: {population[country]} crore")
else:
   print("input again")
doctorlove
  • 18,872
  • 2
  • 46
  • 62
  • 3
    try a while loop? while country not in population: ... – Docuemada Dec 07 '22 at 17:06
  • 1
    You do not have loop at all. It seems like you may not be familiar with for and while loops in Python? Then I'd suggest looking for python tutorials on loops or more general questions about loops on stackoverflow. – user9794 Dec 07 '22 at 17:06
  • Does this answer your question? [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – G. Anderson Dec 07 '22 at 17:08

1 Answers1

1

One way to do is with a while true that check if the input is correct using the break keyword that will stop the loop

while True:
   country = input("enter countri name-")
   if country in population:
        break;
    

Or even do something like this that is more pythonic

country = input("enter countri name-")
while country not in population:
   country = input("Enter valid country")
BDurand
  • 108
  • 1
  • 10