Home > Blockchain >  ELSE not executing in the program
ELSE not executing in the program

Time:11-01

fruits = ["mango", "banana", "apple", "yolk", "mulberry"]
letter = input("Enter the letter you want to search: ")
letter2 = input("Enter the second letter you want to search: ")
for word in fruits:
   if word[0].startswith(letter) and word[1].startswith(letter2):
      result = word
      if result == word:
         print(result)
      else:
         print("no result")

i get the result part working but the "no result" output is not showing whenever i enter in the wrong input

CodePudding user response:

the "no result" is not appearing because that never happens.

in the second if statement ( if word[0]......) you have set

result = word

so when the third if statement sees that result is equal to word, it prints the value stored in 'result'

CodePudding user response:

You can use your code like this too

fruits = ["mango", "banana", "apple", "yolk", "mulberry"]
letter = input("Enter the letter you want to search: ")
letter2 = input("Enter the second letter you want to search: ")
flag = False
result=""
for word in fruits:
   if word[0].startswith(letter) and word[1].startswith(letter2):
      flag = True
      result = word



if(flag == False):
   print("no result")
else:
   print(result)

CodePudding user response:

You don't need to use nested if and else conditions. Remember that this code will compare every item from the list and display each result whether they are a match or not.

fruits = ["mango", "banana", "apple", "yolk", "mulberry"]
letter = input("Enter the letter you want to search: ")
letter2 = input("Enter the second letter you want to search: ")
for word in fruits:
   if word[0].startswith(letter) and word[1].startswith(letter2):
         print(word)
   else:
      print("no result")

CodePudding user response:

Instead of assigning 2 variables from 2 inputs, you can concat 2 inputs and assign it to 1 var. From there, you only need to check if word start with letters when iterating the fruits list.

Using walrus operator (:=), you can assign a variable while printing and reuse it to print whether its value is None or False

fruits = ["mango", "banana", "apple", "yolk", "mulberry"]
letters = input("Enter the letter you want to search: ")   input("Enter the second letter you want to search: ")
for word in fruits:
    if word.startswith(letters): 
        print(result := word)
if not result: 
    print('Not Found')

# Enter the letter you want to search: m
# Enter the second letter you want to search: u
# mulberry

Or one liner:

fruits = ["mango", "banana", "apple", "yolk", "mulberry"]
letters = input("Enter the letter you want to search: ")   input("Enter the second letter you want to search: ")

print(res[0] if (res := [w for w in fruits if w.startswith(letters)]) else 'Not Found')

# Enter the letter you want to search: m
# Enter the second letter you want to search: b
# Not Found

CodePudding user response:

Why are you comparing with 'startwith'? you can compare it directly like this:

if word[0] == letter1 and word[1] == letter2:

Do something like this:

fruits = ["mango", "banana", "apple", "yolk", "mulberry"]
letter = input("Enter the letter you want to search: ")
letter2 = input("Enter the second letter you want to search: ")
result = ""

for word in fruits:
   if word[0] == letter and word[1] == letter2:
      result = word
      break
  
if len(result) > 0:
         print(result)
else:
         print("no result")
  • Related