python - 即使正在更新变量,循环也不会退出
问题描述
我正在尝试创建一个简单的猜谜游戏。一切正常,因为它应该。但是,它似乎无法退出检查变量“guessflag”是否小于 15 的第一个 while 循环。我还通过添加一些打印语句来检查它是否正在更新。请帮忙。
它甚至在满足用户猜测正确时退出程序的条件时退出。当我在 stackoverflow 上咨询有关相同问题的其他一些查询时,我试图确保这些错误不会发生在我身上。此外,我必须添加一些更多的收尾工作以减少程序运行所花费的时间,但目前这工作得很好。
import random
point = 101
randomint = random.randint(0,100)
hintflag = 0
hintmessage = "To claim a hint, enter 102 in the input below. ***THE HINT WILL COME AT A COST OF 20 POINTS***"
guessflag = 0
while guessflag <=15:
def init(param):
global guess, point, guessflag
if(param == 1):
global point,guessflag
point = point - 1
guessflag = guessflag + 1
print(hintmessage)
guess = int(input("Enter your guess. It should be between 1 and 100... "))
proceed()
def proceed():
global hintflag
global point
if (guess < 1 or (guess > 100 and guess != 102)):
print('')
print("Please enter a number between 1 and 100... ")
init(2)
elif ((guess == 102)and(hintflag < 2 and hintflag >= 0)):
if(hintflag == 0):
checkrandeven()
if(hintflag == 1):
checkrandmultiple3()
else:
match()
def checkrandmultiple3():
if (randomint % 3 == 0):
global israndmultiple3, point, hintmessage, hintflag
hintflag = hintflag + 1
israndmultiple3 = True
point = point-20
hintmessage = 'You have exhausted all your hints'
print("The number to be guessed is a multiple of 3.")
print("Points: "+str(point))
init(2)
else:
hintflag = hintflag+1
israndmultiple3 = False
point = point-20
hintmessage = 'You have exhausted all your hints'
print("The number to be guessed is not a multiple of 3.")
print("Points: "+str(point))
init(2)
def checkrandeven():
if (randomint % 2 == 0):
global israndeven, point, hintmessage, hintflag
hintflag = hintflag+1
israndeven = True
point = point-20
hintmessage = 'To claim your ***LAST*** hint, enter 102 in the input below. ***THE HINT WILL COME AT A COST OF 20 POINTS***'
print('')
print("Your hint is...")
print("The number to be guessed is even.")
print("***YOU HAVE SPENT 20 POINTS***")
print("Points: "+str(point))
init(2)
else:
israndeven = False
hintflag = hintflag+1
point = point-20
hintmessage = 'To claim your ***LAST*** hint, enter 102 in the input below. ***THE HINT WILL COME AT A COST OF 20 POINTS***'
print("The number to be guessed is odd.")
print("Points: "+str(point))
init(2)
def match():
global randomint, guess, point, guessflag, hintflag
if(randomint != guess):
if(randomint < guess):
print("Try Again! Your number was too high; Try a number lower than "+str(guess))
init(1)
if(randomint > guess):
print("Try Again! Your number was too low; Try a number higher than "+str(guess))
init(1)
if(randomint == guess):
print ("CONGRATULATIONS! You won!")
print ("It took you "+ str(guessflag) + " tries, "+ str(hintflag)+" hints to beat the game!")
print("The number of points you finished with are... "+ str(point))
exit()
init(1)
解决方案
您的 while 循环中的“init(1)”调用实际上只调用了一次,并且您的 4 个函数递归地相互调用。换句话说,你从来没有真正完成你的 while 循环的第一次迭代,你只是在一个链中越走越远:
init -> 继续 -> checkrandeven (或 checkrandmultiple3) -> init -> 继续 -> checkrandeven -> init -> 继续 ...
基本上,您需要考虑重组代码以避免递归。尝试在主循环中返回输出,然后从那里调用后续函数。
推荐阅读
- .net - 窗体打开点击事件
- arrays - 如何在Angular中对列表中的项目进行排序?
- sapui5 - 有没有办法以自定义方式使用 sap.m.PlanningCalendar?
- service - 如何将 keycloak 安装为服务,使其自动启动?
- python - 无法在散景上的瓷砖上绘制点
- python-3.x - 如何使用 Python 将数据从 Excel 文件(不同的工作表)复制到一个文本文件中
- kml - KML 和海拔高度
- c# - 在实体框架中使用 TransactionScope
- android - 未处理的异常:PlatformException(sign_in_failed, com.google.android.gms.common.api.ApiException: 12500: , null)
- c++ - 可变参数模板(具有类和功能)无法遵守