I. Abbas
I. Abbas

Reputation: 1

Script runs and stops immediately

I ran this program on Repl.it for forking purposes - Peculiar enough, it does not seem to work, as Repl.it automatically runs then stops the program.

found = False
i = 0 

capital = ["London","New Dehli","Jerusalem","Paris","Washington DC","Riyadh","Kaula Lumpur"]

user_search = ("Which capital do you wish to search for \n - ")
while found == False and i < len(capital):
  if user_search == capital[i]:
    print ("Capital found")
    found = True
  i= i+1

Upvotes: 0

Views: 69

Answers (2)

neehari
neehari

Reputation: 2612

You are not getting the input, so change it like so:

user_search = input("Which capital do you wish to search for \n - ")

With proper indentation, spacing and correct spelling, it should be:

found = False
i = 0 
capital = ["London", "New Delhi", "Jerusalem", "Paris", "Washington DC", "Riyadh", "Kuala Lumpur"]

user_search = input("Which capital do you wish to search for \n - ")

while found == False and i < len(capital):
    if user_search == capital[i]:
        print("Capital found")
        found = True
    i = i + 1

If you could do without the while loop, like others said, you could just use an if statement with the membership operator in.

capital = ["London", "New Delhi", "Jerusalem", "Paris", "Washington DC", "Riyadh", "Kuala Lumpur"]
user_search = input("Which capital do you wish to search for \n - ")

if user_search in capital:
    print("Capital found")

Upvotes: 3

sniperd
sniperd

Reputation: 5274

If you want it to output anything you'll need to ask for input, and I would suggest not doing the while loop, you can look for a string in a list as follows:

capital = ["London","New Dehli","Jerusalem","Paris","Washington DC","Riyadh","Kaula Lumpur"]
user_search = input("Which capital do you wish to search for \n - ")
if user_search in capital:
    print ("found it")

Upvotes: 1

Related Questions