Python - 如果语句在Tkinter中不起作用

时间:2018-01-17 18:43:37

标签: python tkinter

我是新手,也是python ..以下代码中的if语句未运行。有谁知道为什么?这是我需要在周日提交的骆驼游戏。

.home-cateogry-box {
display: block;
float: left;
box-sizing: border-box;
margin-bottom: 15px;

.home-category-box:not(:first-child),
.home-category-box:not(:last-child) {
padding: 0 15px;
}

2 个答案:

答案 0 :(得分:1)

我将假设这些是你所说的if语句:

if thirst > 4 and thirst <= 6:
   print "You are thirsty."
   print ""
elif thirst > 6:
   print "You died of thirst!"
   done = True
if tiredness > 5 and tiredness <= 8 and done != True:
   print "Your camel is getting tired."
   print ""
elif tiredness > 8 and done == False:
   print "Your camel is dead."
   done = True

if distance <= 0 and done != True:
   print "The natives caught you."
   done = True
elif distance < 15 and done != True:
   print "The natives are getting close!"

if summ >= 200 and done == False:
  print "You won!"
  done = True

if random.randint(1, 100) <= 5 and done != True:
  print "You found an oasis!"
  print ""
  thirst = 0
  tiredness = 0
  drinks = 3

您的if语句正在运行,但它们仅在代码的开头运行,因为它们不在函数或循环中,它们仅在代码的最开始运行。

如果您正在使用if语句或任何循环等,则需要使用tab键缩进其中的内容。如果你想在if语句和循环或类似的东西中有东西,你可以做两次选项卡。这些是告诉python你的循环和比较结束的地方。如果你根本没有缩进,python会认为循环内部没有任何内容。你还需要缩进函数,如果你想把if语句放在循环中你需要同样缩进它。

这是例子:

while something1 == something2:
   if anotherthing1 == anotherthing2:
      print "something happened"
      somethinghappened = True


这只是一个例子,按照你需要的方式实施。

答案 1 :(得分:1)

把它放在循环中并正确缩进if语句