I need help with my Python Project

i have 3 functions.
i need to loop code but for some reason i can’t make it
im new to python, but i summed up java.
im making a whatsapp chatbot but i dont have any api access or anything so
i need to do this my own way. Im making a discord-like deal but i am using whatsapp
web instead… This way i don’t get limited to only sending mesasges by the api.
i need to call the code all over again but wont work.
every function needs the other.

my code:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time

driver = webdriver.Firefox(executable_path=r'C:\Python38\geckoonly\geckodriver.exe')
input("Press anything after QR scan")

def reply_message(botname,msg,classname):

 reply = driver.find_element_by_class_name(classname)
 reply.send_keys({botname} + "> " + {msg})

def search():

 names = ["WhatsBot Testing"]

 for name in names:
  person = driver.find_element_by_xpath('//span[@title = "{}"]'.format(name))

for i in range(1,3):

 driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")

 msg_got = driver.find_elements_by_css_selector("span.selectable-text.invisible-space.copyable-text")
 msg = [message.text for message in msg_got]  
#also this is kind of broken. i need to search for the msg but it is in the 
#function so i cant really do anything cuz it will get undefined.
` statement("!hello",reply_message("WhatsBot","Hello there!","_3FRCZ.copyable-text.selectable-text"))
 statement("!commands",reply_message("PurpBot","You can do: !hello and !link","_3FRCZ.copyable-text.selectable-text"), msg)

 statement("!link",reply_message("WhatsBot","google: https://google.com","_3FRCZ.copyable-text.selectable-text"), msg)`
#idk what to do with this
def statement(command,result,msg): 

 if msg[-1] == command:
 else :


Help me out.
Sincerly yours, Approximated!

tried using goto also wont work

To repeat actions you need a loop. Unless you already know how many times you want to repeat, you’ll probably want a while loop, so you can end execution of your code when a specific condition is met. Here is a very simple example:

from random import choice
user_input = 'hello'
responses = ["I'm sleepy", "Do you like bananas?", "How many is seven?", "I like bananas.", "Do you have any grapes?"]
print("Hello, my name is Bot. Type 'quit' any time to quit.")

while user_input != 'quit':
    user_input = input("Say something: ")
print("Well, okay then. Bye!")
Sample output:

Hello, my name is Bot. Type ‘quit’ any time to quit.
I like bananas.
Say something: Good for you.
I like bananas.
Say something: I know. You’ve mentioned it.
I’m sleepy
Say something: Take a nap.
How many is seven?
Say something: Umm. 7
Do you have any grapes?
Say something: No. Do you?
I like bananas.
Say something: Apparently.
I like bananas.
Say something: quit
Well, okay then. Bye!

does the while loop wait until the function is finished?? and can i run a function from another?

also anyway the code needs its itself, so i cant have any luck predefine-ing

The code inside of the while loop will repeat until the condition is met. You could have multiple function calls inside a while loop. Those functions can call other functions.

can i predefine functions from others
def a():
def b():

while true:

Yes. Expounding on your example a bit:

def a():
def b():

condition = ""

while condition != "quit":
    #need to make sure that condition gets updated inside the loop else you'll have an infinite loop
    #probably by asking for user input in your case
    condition = input("Type 'quit' to quit. ")
Sample output:

Type ‘quit’ to quit. nope
Type ‘quit’ to quit. no
Type ‘quit’ to quit. quit

THANK YOU SO MUCH <3. But isn’t there a way to continue the loop without continuously typing?
Isn’t while true is used for that??
isn’t there a way to type “q” and close? and not just type it every time? i guess I’ll have to stick to macro-ing if that isn’t the case

1 Like

Maybe i just while true inside

while condition != "quit":
while True: 
    condition = input("Type 'quit' to quit. ")

That was just an example. If you are making a chat bot, the user presumably will be typing responses continually. The line of code taking the user input should be inside the while loop, so that if they type ‘quit’ or whatever you want the trigger word to be to end, the loop will end. You wouldn’t have to specifically ask the user to type ‘quit’ to quit repeatedly. I wrote the following just for fun a few days ago after discussing this topic with you.

from random import choice
from random import choices
user_input = ''
statements = ["I'm sleepy", "I'm hungry.", "I like bananas.", "I don't think I like monkeys very much.", "Bananas are nutritious and delicious.", "I think it's time to eat.", "I don't like banana jokes. I think it's rude to make fun of bananas.", "Banana bread is yummy!", "I love banana pudding!", "I don't have any pets.", "If I had a pet, it wouldn't be a monkey. I know that much."]
questions = ["Do you like bananas?", "Do you have any grapes?", "Is it time to eat?", "Do you know any banana farmers?", "How do you feel about monkeys?", "If a monkey came here, do you think it would eat my bananas?", "Why do monkeys even exist?", "If you had two bananas, would you let me eat one?", "If you and I were to share three bananas, would we split the third one long ways or short ways?", "Do you know any banana jokes?"]
jokes = ["Why did the monkey cross the road?", "Why does it take three monkeys to change a light bulb?", "Why do monkeys swing in trees?"]
answers = ["To steal my bananas!", "to steal your bananas?", "to steal your bananas"]

def ask():
  return questions[choice(range(1000)) % len(questions)]

def say():
  return statements[choice(range(1000)) % len(statements)]

def joke():
  user_answer = input(f"\n{tell_joke()} ")
  return answers[0] if user_answer not in answers else "Exactly!!"

def tell_joke():
  return jokes[choice(range(1000)) % len(jokes)]

def burp():
  return "BUUUURRRRPPP!! Excuse me."

bot_actions = [ask, say, joke, burp]

print("Hello, my name is Bot. Type 'quit' to quit.")

while user_input != 'quit':
    print("\n" + choices(bot_actions, weights=[10, 10, 5, 1])[0]())
    user_input = input("\nYour response: ")
print("\nWell, okay then. Bye!")

# Using questions[choice(range(1000)) % len(questions)] seemed to increase the randomness as opposed to just using `choice(questions)` which seemed to yield many repeats.

I assume you are building something much more complex, but this may give you some ideas for the basic structure.

Something like this would work:

while True:
    x = input("say something: ")
    if x == 'quit': break

print("I quit!")

Thank you for your amazing help, but sadly, this is not how you thought it would work, the bot is reading messages left and right on a WhatsApp group chat, and responding to the typical, the user shouldn’t be able to quit the program, only i can. Because i would run the bot, and only i should quit the bot for any reason. The user isn’t communicating on the console, we are just checking for a sufficient question to respond to. Example !link will make the bot reply with, a link for a tutorial video, or a specific website.

Understood, it will always run, and i cant stop it