![](/img/trans.png)
[英]How can I get this program to trigger cv.waitKey() on its own in order to exit this loop?
[英]How can I exit the loop in this program
我最近開始了 python 並且在這個基於文本的游戲中我似乎無法退出 while 循環。我嘗試將循環從 while 更改為 for 循環和 If-Else 循環,但我無法解決退出它的方法。 當用戶輸入的數字與系統的隨機數匹配時,我希望它退出循環並打印下面寫的文本
import random
def batting ():
print("You can enter a number between 1 and 10")
bat = input()
sys_ball = random.randint(1,10)
print (sys_ball)
score_board = (bat + bat)
batting ()
while bat != sys_ball:
batting ()
def balling ():
print ("Ok! Start balling with a number between 1 and 10!")
user_ball = input()
sys_bat = random.randint(1,2)
print (sys_bat)
balling ()
while user_ball != sys_bat:
balling ()
def oddeve ():
print("Welcome to finger cricket! Type 1 to start batting and 2 to start balling!")
odd_eve = input()
if odd_eve == ('1'):
batting ()
print ("You win! You scored".join(score_board))
else:
balling()
print ("You win! You defeated me")
oddeve ()
仔細看,你的 function 遞歸調用自己。
def batting (): # <- 1
print("You can enter a number between 1 and 10")
bat = input()
sys_ball = random.randint(1,10)
print (sys_ball)
score_board = (bat + bat)
batting () # <- 2
while bat != sys_ball:
batting ()
def balling (): # <- 1
print ("Ok! Start balling with a number between 1 and 10!")
user_ball = input()
sys_bat = random.randint(1,2)
print (sys_bat)
balling () # <- 2
while user_ball != sys_bat:
balling ()
因此,請停止遞歸調用 function。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.