Python - 如果语句在 Tkinter 中不起作用 [英] Python - If statments not working in Tkinter

查看:19
本文介绍了Python - 如果语句在 Tkinter 中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的,也是 Python 的新手.. 下面代码中的 if 语句没有运行.有谁知道为什么?这是我需要在周日之前提交的骆驼游戏.

I'm new here, and to python also.. The if statments in the following code are not running. does anyone know why? This is a camel game I need to submit by sunday.

import random
from Tkinter import *

print "Welcome to Camel!"
print "You have stolen a camel to make your way across the great Mobi desert."
print "The natives want their camel back",
print "and are chasing you down! Survive your"
print "desert trek and out run the natives."
print ""

summ = 0
done = False
miles = 0
thirst = 0
tiredness = 0
distance = 20
drinks = 3

window = Tk()
window.title("Camel")

def A():
global drinks
if drinks > 0:
drinks -= 1
thirst = 0
else:
print "You ran out of drinks!"
done = True

def B():
global summ, thirst, tiredness, distance
miles = random.randint(5, 12)
summ += miles
print "miles traveled: ", miles
print "Total miles traveled: ", summ
print ""
thirst += 1
tiredness += 1
distance -= random.randint(7, 14)
distance += miles

def C():
global summ, thirst, tiredness, distance
miles = random.randint(10, 20)
summ += miles
print "Miles traveled: ", miles
print "Total miles traveled: ", summ
print ""
thirst += 1
tiredness += random.randint(1, 3)
distance -= random.randint(7, 14)
distance += miles

def D():
global distance
tiredness = 0
print "Your Camel is happy."
print ""
distance -= random.randint(7, 14)

def E():
print "Total miles traveled: ", summ
print "Drinks in canteen: ", drinks
print "The natives are %i miles behind you." % (distance)
print ""

def Q():
exit()


A = Button(window, text = "A. Drink from your canteen.", command = A)
B = Button(window, text = "B. Ahead moderate speed.", command = B)
C = Button(window, text = "C. Ahead full speed.", command = C)
D = Button(window, text = "D. Stop for the night.", command = D)
E = Button(window, text = "E. Status check.", command = E)
Q = Button(window, text = "Q. Quit.", command = Q)
A.pack()
B.pack()
C.pack()
D.pack()
E.pack()
Q.pack()

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

window.mainloop()

推荐答案

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

I am going to assume that these are the if statements that you speak of:

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 语句和循环或类似内容中包含某些内容,请执行两次 Tab 操作.这些是告诉python你的循环和比较在哪里结束.如果根本不缩进,python 会认为循环内部没有任何内容.您还需要对函数进行缩进,如果您想将 if 语句放在循环中,您也需要同样缩进.

示例如下:

Your if statements are running, but they are only running at the beginning of the code because they are not in a function or a loop, they are only being run at the very start of your code.

if you are using if statements or any loops and such, you need to indent what is inside them using the tab key. if you want to have something inside an if statement and a loop or something of that sort, you do tab twice. These are to tell python where your loops and comparisons end. If you do not indent at all, python will think that there is nothing inside the loop. You also need to indent with functions, if you want to put the if statements inside of the loop you need to indent it likewise.

Here is and example:

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



这只是一个例子,按照您需要的方式实现它.



That is just an example, implement it the way you need to.

这篇关于Python - 如果语句在 Tkinter 中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆