因此,我创建了一个游戏,您在其中购买气球,购买的越多,您获得的收入就越多。为此,我需要增加资金的问题,问题是我需要2个循环,无限循环1个,计时器循环另一个。我通过线程完成了两个循环。我打印了一些文本来测试它们是否起作用。如果他们像我希望他们那样工作,那应该做一个无限循环,他们俩都将 循环1 Loop2 循环1 Loop2 循环1 Loop2 永远
但是相反 循环1 Loop2 一次,就是这样,这表明它不是无限循环的。 这是我的源代码,有点混乱,但这是因为我是新手
###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()
非常感谢您的帮助
答案 0 :(得分:0)
当主线程退出时,线程停止运行。在程序中,您启动了两个新线程,但是不要让主线程等待它们完成工作。
尝试向您的主脚本添加[CS_RES]
调用:
.join()
此外,如果您想要“ Loop1 Loop2 Loop1 Loop2 ...”,则您的thread1.join()
thread2.join()
函数也需要一个无限循环。