0

i've a working function which stops when the user writes exit, but i also have another function which doesn't stop so cmd just keeps staying open and won't close like it should as the other function keeps changing the cmd text color

import os,webbrowser,time,random,sys
from threading import Thread 

def fun1():
    Q=1
    while Q==1:
        print ('would you like to:')
        print('1.launch an applcation')
        print('2.browse the internet')
        print('========================================')
        bruv=input('your answer: ')
        if bruv in ('1','launch','launch app','launch an application'):
            print('========================================')
            print('what app would you like to open?')
            print('========================================')
            x=input('your answer: ')
            if x not in ('word'):
                y=x+'.exe'
                os.startfile(y)
            else:
                z='win'+x
                os.startfile(z)
        if bruv in ('2','browse the internet','browse','browse internet'):
            print('========================================')
            print ('where would you like to go?')
            print('========================================')
            p=input('your answer: ')
            p='www.'+p+'.com'
            webbrowser.open(p)
            print('========================================')
            print ('ok')
        print('========================================')
        print ('wanna continue?')
        if input() in ('exit','no','quit','nope','close'):
            print('========================================')
            print ('ok! thanks for using my bot!')
            print('========================================')
            time.sleep(1)
            Q+=1
            countdown=3
            while countdown>=0:
                if countdown!=0:
                    print (countdown)
                    time.sleep(1)
                    countdown-=1
                else:
                    sys.exit()

def fun2():
    what=1
    while what==1:
        os.system('color E')
        time.sleep(0.2)
        os.system('color A')
        time.sleep(0.2)
        os.system('color C')
        time.sleep(0.2)
        os.system('color B')
        time.sleep(0.2)
        os.system('color D')
        time.sleep(0.2)
    else:
        sys.exit()

t1=Thread(target=fun1)
t2=Thread(target=fun2)

t1.start()
t2.start()

In this code fun2 keeps rolling changing the color and wont close CMD also i know this code is pretty bad i'm just starting python.

Omkar C.
  • 755
  • 8
  • 21
  • 1
    `sys.exit()` is rarely a clean way to exit code, especially multithreaded. change `sys.exit()` call to just `return`. Also, in your loops you can also use `while True:` for infinite loops instead of constant variables. For thread checking, one approach is to pass `t1` to `fun2()` when you call it, and instead of `while what==1` for loop control you could use `while t1.is_alive()`. It will exit after changing the color to color D. – RufusVS Nov 24 '19 at 21:41
  • @Itai bartoov: Hi, I see you're new to SO. If you feel an answer solved the problem, please mark it as 'accepted' by clicking the green check mark. This helps keep the focus on older SO which still don't have answers. – BStadlbauer Nov 26 '19 at 17:20

1 Answers1

0

So the problem here is that your whole script (which starts both of your functions/threads) will on default only end when all the threads it started have stopped. So all your functions need to return at some point, but your fun2() will never finish as the endless loop while what==1 will always run as what is always equal to 1. (As a side note, the convention for endless loops in Python is to use while True: ...).

To tell a Thread that it should finish when all other threads have, you have to make it a Daemon. You might want to look here, where the documentation says:

A thread can be flagged as a “daemon thread”. The significance of this flag is that the entire Python program exits when only daemon threads are left. The initial value is inherited from the creating thread. The flag can be set through the daemon property or the daemon constructor argument.

So in your example above, making t2 a deamon should work.

... 
t1 = Thread(target=fun1)
t2 = Thread(target=fun2)
t2.deamon = True

t1.start()
t2.start()

P.S.: Also be aware that color does not work in all terminals and operating systems (e.g. MacOS)

BStadlbauer
  • 1,287
  • 6
  • 18