# Program Arcade GamesWith Python And Pygame

 < Previous Home Next >

# Chapter 3: 问答游戏和if语句

## 3.1 基本的比较

 # 在例子中使用的变量 a = 4 b = 5 # 基本的比较 if a < b: print("a is less than b") if a > b: print("a is greater than b") print("Done")  Variables: a=4 b=5 Output: a is less than b Done 

http://en.wikipedia.org/wiki/Flowchart

if a <= b:
print("a is less than or equal to b")

if a >= b:
print("a is greater than or equal to b")


<=>=符号必须按这个次序来使用，且中间不能有空格。比如=<就不工作，< =也不行。

if a ≤ b:

# 相等
if a == b:
print("a is equal to b")

# 不等
if a != b:
print("a and b are not equal")


# 这是错的
a == 1

# 这也是错的
if a = 1:
print("A is one")


## 3.2 缩进

if a == 1:
print("If a is one, this will print.")
print("So will this.")
print("And this.")

print("This will always print because it is not indented.")


if a == 1:
print("Indented two spaces.")
print("Indented four. This will generate an error.")
print("The computer will want you to make up your mind.")


if a == 1:
print("If a is one, this will print.")
print("So will this.")

print("This will always print because it is not indented.")
print("This will generate an error. Why it is indented?")


## 3.3 使用And/Or

# And
if a < b and a < c:
print("a is less than b and c")

# Non-exclusive or
if a < b or a < c:
print("a is less than either b or c (or both)")


# 这是不正确的
if a < b or < c:
print("a is less than b and c")


## 3.4 布尔变量

Python支持布尔型变量。什么是布尔型变量？布尔型变量能够存储一个True或者一个False布尔代数是由George Boole在1854年发明的。 多希望他能预见他的成果对奠定现代的计算机逻辑的基础是何等的重要啊！

# 布尔型数据。这是合法的！
a = True
if a:
print("a is true")


#如何使用not
if not(a):
print("a is false")


# 如何使用not
if not a:
print("a is false")


a = True
b = False

if a and b:
print("a and b are both true")


a = 3
b = 3
# 下面这行代码看起来很奇怪，但是是合法的。看起来c或者是true或者是false, 取决与a和b是否相当。
c = a == b
# 打印出c的值，在当前情形下是True
print(c)

0表示False。剩下一切都是True.

if 1:
print("1")
if "A":
print("A")


if 0:
print("Zero")


a = "c"
if a == "B" or "b":
print("a is equal to b. Maybe.")

# 这样正确的写法。
if a == "B" or a == "b":
print("a is equal to b.")


## 3.5 Else和Else If语句

temperature = int(input("What is the temperature in Fahrenheit? "))
if temperature > 90:
print("It is hot outside")
print("Done")


temperature = int(input("What is the temperature in Fahrenheit? "))
if temperature > 90:
print("It is hot outside")
else:
print("It is not hot outside")
print("Done")


temperature = int(input("What is the temperature in Fahrenheit? "))
if temperature > 90:
print("It is hot outside")
elif temperature < 30:
print("It is cold outside")
else:
print("It is not hot outside")
print("Done")


temperature = int(input("What is the temperature in Fahrenheit? "))
if temperature > 90:
print("It is hot outside")
elif temperature > 110:
print("Oh man, you could fry eggs on the pavement!")
elif temperature < 30:
print("It is cold outside")
else:
print("It is ok outside")
print("Done")


## 3.6 文本的比较

user_name = input("What is your name? ")
if user_name == "Paul":
print("You have a nice name.")
else:


user_name = input("What is your name? ")
if user_name == Paul: # This does not work because quotes are missing
print("You have a nice name.")
else:


### 3.6.1 多个文本的可能性

# 这不可行！因为它永远是true
if user_name == "Paul" or "Mary":


# 这是可行的
if user_name == "Paul" or user_name == "Mary":


### 3.6.2 对大小写不敏感的比较

user_name = input("What is your name? ")
if user_name.lower() == "paul":
print("You have a nice name.")
else:


## 3.7 if语句的例子

http://pydev.org/

# Sample Python/Pygame Programs
# Simpson College Computer Science
# http://simpson.edu/computer-science/

# Explanation video: http://youtu.be/pDpNSck2aXQ

# Variables used in the example if statements
a = 4
b = 5
c = 6

# Basic comparisons
if a < b:
print("a is less than b")

if a > b:
print("a is greater than than b")

if a <= b:
print("a is less than or equal to b")

if a >= b:
print("a is greater than or equal to b")

# NOTE: It is very easy to mix when to use == and =.
# Use == if you are asking if they are equal, use =
# if you are assigning a value.
if a == b:
print("a is equal to b")

# Not equal
if a != b:
print("a and b are not equal")

# And
if a < b and a < c:
print("a is less than b and c")

# Non-exclusive or
if a < b or a < c:
print("a is less than either b or c (or both)")

# Boolean data type. This is legal!
a = True
if a:
print("a is true")

if not a:
print("a is false")

a = True
b = False

if a and b:
print("a and b are both true")

a = 3
b = 3
c = a == b
print(c)

# These are also legal and will trigger as being true because
# the values are not zero:
if 1:
print("1")
if "A":
print("A")

# This will not trigger as true because it is zero.
if 0:
print("Zero")

# Comparing variables to multiple values.
# The first if statement appears to work, but it will always
# trigger as true even if the variable a is not equal to b.
# This is because "b" by itself is considered true.
a = "c"
if a == "B" or "b":
print("a is equal to b. Maybe.")

# This is the proper way to do the if statement.
if a == "B" or a == "b":
print("a is equal to b.")

# Example 1: If statement
temperature = int(input("What is the temperature in Fahrenheit? "))
if temperature > 90:
print("It is hot outside")
print("Done")

# Example 2: Else statement
temperature = int(input("What is the temperature in Fahrenheit? "))
if temperature > 90:
print("It is hot outside")
else:
print("It is not hot outside")
print("Done")

# Example 3: Else if statement
temperature = int(input("What is the temperature in Fahrenheit? "))
if temperature > 90:
print("It is hot outside")
elif temperature < 30:
print("It is cold outside")
else:
print("It is not hot outside")
print("Done")

# Example 4: Ordering of statements
# Something with this is wrong. What?
temperature = int(input("What is the temperature in Fahrenheit? "))
if temperature > 90:
print("It is hot outside")
elif temperature > 110:
print("Oh man, you could fry eggs on the pavement!")
elif temperature < 30:
print("It is cold outside")
else:
print("It is ok outside")
print("Done")

# Comparisons using string/text
# Note, this example does not work when running under Eclipse
# because the input will contain an extra carriage return at the
# end. It works fine under IDLE.
print("You have a nice name.")
else:



## 3.8 复习

### 3.8.3 实验

 < Previous Home Next >