Home > Mobile >  How do I make a function that googles the input from the user, with while loop unless I type 'e
How do I make a function that googles the input from the user, with while loop unless I type 'e

Time:07-30

I tried making a while loop, however, I can't break it. I am new to programming and maybe I am bitting more than I can chew, but I would be grateful if someone could help me. I am using pywhatkit and I have a problem defining the searching variable.

import pywhatkit as pwt

def searching_mode():
    searching = (input(''))
    while True:
        print(f"Searching ...")
        if searching == pwt.search(input('')):
            continue
        else:
            searching == (input('exit'))
            break


searching_mode()

CodePudding user response:

Your input should be in while loop so it can continue execute the input when there's no input or break when the input is 'exit'. Try this:

import pywhatkit as pwt

def searching_mode():
    while True:
        searching = input('Search for? (type "exit" to exit) : ')
        if 'exit' in searching:
            break
        elif not searching:
            continue
        else:
            print(f'Searching for "{searching}"...')
            pwt.search(searching)

searching_mode()

Output:

Search for? (type "exit" to exit) : python course
Searching for "python course"...
Search for? (type "exit" to exit) : 
Search for? (type "exit" to exit) : stackoverflow python while loop
Searching for "stackoverflow python while loop"...
Search for? (type "exit" to exit) : exit

Process finished with exit code 0

CodePudding user response:

Hi if you can give more information about what you want to achiev and give full script can help better. But you can try setting up a switch to break a loop for example;

def searching_mode():
    switch = True:
    searching = (input(''))
    while switch:
        print(f"Searching ...")
        if searching == pwt.search(input('')):
            continue
        else:
            searching == (input('exit'))
            switch = False


 searching_mode()

While loop will continue to run if you construct like "while True", so you can setup a condition to break from it. by using

while switch:
   if condition1:
     #do something:
   else:
     #do this
     switch = False
     # so next time it 
  • Related