简体   繁体   中英

Python - Threading and Multiple Looping Problems

So i created a game, where you purchase balloons, and the more you buy, the more you earn. To do this i need help adding the money on, the problem is that i need 2 loops, 1 on an infinite loop, and the other on a timer loop. I did both of the loops through threading. And i printed some text to test if they worked or not. And if they had worked like i had wanted them to,it should have done an infinite loop, of both of them going Loop1 Loop2 Loop1 Loop2 Loop1 Loop2 Forever

But instead it just does Loop1 Loop2 once and that is it, which shows that it isn't infinitely looping. This is my source code, its a bit messy, but that's because i'm new

###Game###

#Imports#
import time
import threading

##Setting Variables##
#Money Variables
money=1
money_ps=0
#Number of Balloons#
red_balloon_num=0
blue_balloon_num=0
yellow_balloon_num=0
green_balloon_num=0
pink_balloon_num=0
total_num=0

#Defining
def moneyps():
        global money_ps
        money_ps=(red_balloon_num*0.1)+(blue_balloon_num*1)+(yellow_balloon_num*10)+(green_balloon_num*100)+(pink_balloon_num*1000)

def loop1():    
        #Loop#
        while True:
                print("loop1")
                time.sleep(1)
                #Input#
                inp=input().lower()
                word1=inp.split()[:1]              

                #Buying Balloon#
                if inp=="buy" or inp=="purchase":
                        print("What would you like to buy")
                        print("•Red Balloon (£1)")
                        print("•Blue Balloon (£100)")
                        print("•Yellow Balloon (£10000)")   
                        print("•Green Balloon (£1000000)")
                        print("•Pink Balloon (£100000000)")
                        inp=input().lower()

                        #Red Balloon#
                        if inp=="red balloon":
                                if money >= 1:
                                        money-=1
                                        red_balloon_num+=1
                                        print("You successfully bought a Red Balloon")
                                else:
                                        print("You are unable to afford this")

                        #Blue Balloon#
                        elif inp=="blue balloon":
                                if money >= 100:
                                        money-=100
                                        blue_balloon_num==1
                                        print("You successfully bought a Blue Balloon")
                                else:
                                        print("You are unable to afford this")

                        #Yellow Balloon#
                        elif inp=="yellow balloon":
                                if money >= 10000:
                                        money-=10000
                                        yellow_balloon_num==1
                                        print("You successfully bought a Yellow Balloon")
                                else:
                                        print("You are unable to afford this")

                        #Green Balloon#
                        elif inp=="green balloon":
                                if money >= 1000000:
                                        money-=1000000
                                        green_balloon_num==1
                                        print("You successfully bought a Green Balloon")
                                else:
                                        print("You are unable to afford this")

                        #Pink Balloon
                        elif inp=="pink balloon":
                                if money >= 100000000:
                                        money-=100000000
                                        pink_balloon_num==1
                                        print("You successfully bought a Pink Balloon")
                                else:
                                        print("You are unable to afford this")

                #Check Amount of Money In Bank#
                elif inp=="bank":
                        print("You have £",money)

                #Checks How Much They Earn Per Second#
                elif inp=="money per second":
                        print("You are earning £",money_ps,"Per Second")


                ##Checks Amount of Balloons
                #Number of Red Balloons
                elif inp=="red balloon":
                        if red_balloon_num==1:
                                print("You have 1 Red Balloon")
                        else:
                                print("You have",red_balloon_num,"Red Balloons")

                #Number of Blue Balloons
                elif inp=="blue balloon":
                        if blue_balloon_num==1:
                                print("You have 1 Blue Balloon")
                        else:
                                print("You have",blue_balloon_num,"Blue Balloons")

                #Number of Yellow Balloons#
                elif inp=="yellow balloon":
                        if yellow_balloon_num==1:
                                print("You have 1 Yellow Balloon")
                        else:
                                print("You have",yellow_balloon_num,"Yellow Balloons")

                #Number of Green Balloons#
                elif inp=="green balloon":
                        if green_balloon_num==1:
                                print("You have 1 Green Balloon")
                        else:
                                print("You have",green_balloon_num,"Green Balloons")

                 #Number of Pink Balloons#
                elif inp=="pink balloon":
                        if pink_balloon_num==1:
                                print("You have 1 Pink Balloon")
                        else:
                                print("You have",pink_balloon_num,"Pink Balloons")

                #Quit#
                elif inp=="quit":
                        break

                else:
                        print("Sorry this is not an available command")

def loop2():
        print("Loop2")
        time.sleep(1)

thread1 = threading.Thread(target=loop1)
thread1.start()

thread2 = threading.Thread(target=loop2)
thread2.start()

Thanks for any help it is much appreciated

Threads stop running when your main thread exits. In your program, you start two new threads but don't keep the main thread waiting for them to complete their work.

try to add .join() calls to your main script:

thread1.join()
thread2.join()

also, if you want "Loop1 Loop2 Loop1 Loop2...", your loop2 function needs an infinite loop too.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM