如果循环得到结果,我希望标题打印一次,如果没有,就不打印。我从这里得到了将标题设置为True或False的提示,但我现在尝试在循环中使用它,这似乎不起作用。你知道吗
header = False
if header==True:
print('{:10} {:10}'.format('Room#','Fee'))
match_count = 0
for apt in aptList:
if userInput >= htl.room and userInput <= htl.fee:
header=True ## shouldnt this statement get it to print?
print(htl.getApt())
match_count += 1
if match_count == 0:
header=False
此代码不打印标题,只打印结果。你不能像我那样在循环中设置一个不同的变量吗?你知道吗
我相信这是行不通的,因为您当前在开头有一个语句,将头定义为
False
,然后代码的下一部分是一个if语句,声明do following if header为True
,但您只是在前面的语句中定义了它False
。你知道吗所以if语句永远不会执行,因为它永远不会返回检查头变量是否已更改。您在for循环中将头变量声明为
True
,但是程序从不返回以查看if语句现在将在第一个if语句中打印该语句。你知道吗我会把if语句放在for循环中,这样它就可以执行它了。你知道吗
您需要在将其设置为True之后打印(标题),或者如果您只想在那里打印它,则需要将其替换为True。你知道吗
相关问题 更多 >
编程相关推荐