vendredi 24 mai 2019

for-loops doesn't work inside if-statement in python

I know this question was asked multiple times, I can actually check if a string is inside a list of strings but IF it is the if-statement doesn't run at all

The if-statement and the for-loops works fine singularly but it doen't work when the loop is inside the if-statement

keyword = input("search here: ")

with open("listaUni.txt") as lista_uni:
    if keyword in lista_uni.read():
        print("there is")
    else:
        print("there is not")
#this works fine



with open("listaUni.txt") as lista_uni:
  for uni in lista_uni.readlines():
    if keyword.lower() in uni.lower():
      print(uni)
# also this works fine, it prints out all the strings in the list 
#containing the keyword



keyword = input("search here: ")

with open("listaUni.txt") as lista_uni:
    if keyword in lista_uni.read():
      for uni in lista_uni.readlines():
        if keyword.lower() in uni.lower():
          print(uni)
    else:
          print("there is not")

lista_uni.close()

When the keyword is in the list, it should check every string of the list and print the ones with the keyword in it, if the keyword is not in the list it should print "there is no". It asked for the keyword and then do nothing. Since the 2 pieces of the code actually work separately, what I'm doing wrong? thanks to everybody.

Aucun commentaire:

Enregistrer un commentaire