Python 3 非程序员教程/决策
像往常一样,我认为我应该用热身打字练习开始每一章,所以这里有一个简短的程序来计算一个整数的绝对值
n = int(input("Number? "))
if n < 0:
print("The absolute value of", n, "is", -n)
else:
print("The absolute value of", n, "is", n)
以下是程序运行两次的输出结果
Number? -34 The absolute value of -34 is 34
Number? 1 The absolute value of 1 is 1
那么,当计算机看到这段代码时它会做什么呢?首先,它使用语句“n = int(input("Number? "))
”提示用户输入一个数字。然后,它读取行“if n < 0:
”。如果n
小于零,Python 将运行行“print("The absolute value of", n, "is", -n)
”。否则,它将运行行“print("The absolute value of", n, "is", n)
”。
更正式地说,Python 会查看表达式n < 0
是真还是假。一个if
语句后面跟着一个缩进的代码块,当表达式为真时,这些语句就会被执行。可选地,if
语句后面可以跟着一个else
语句和另一个缩进的代码块。如果表达式为假,这个第二个代码块将被执行。
表达式可以进行许多不同的测试。这里是一个所有测试的表格
运算符 | 函数 |
---|---|
<
|
小于 |
<=
|
小于或等于 |
>
|
大于 |
>=
|
大于或等于 |
==
|
等于 |
!=
|
不等于 |
if
命令的另一个特点是elif
语句。它代表else if,意思是如果原始的if
语句为假,但elif
部分为真,则执行elif
部分。如果if
或elif
表达式都不为真,则执行else
块中的内容。这里有一个例子
a = 0
while a < 10:
a = a + 1
if a > 5:
print(a, ">", 5)
elif a <= 3:
print(a, "<=", 3)
else:
print("Neither test was true")
以及输出结果
1 <= 3 2 <= 3 3 <= 3 Neither test was true Neither test was true 6 > 5 7 > 5 8 > 5 9 > 5 10 > 5
注意,elif a <= 3
只有在if
语句不为真时才会被测试。可以有多个elif
表达式,允许在单个if
语句中进行多个测试。
# This Program Demonstrates the use of the == operator
# using numbers
print(5 == 6)
# Using variables
x = 5
y = 8
print(x == y)
以及输出结果
False False
high_low.py
# Plays the guessing game higher or lower
# This should actually be something that is semi random like the
# last digits of the time or something else, but that will have to
# wait till a later chapter. (Extra Credit, modify it to be random
# after the Modules chapter)
number = 7
guess = -1
print("Guess the number!")
while guess != number:
guess = int(input("Is it... "))
if guess == number:
print("Hooray! You guessed it right!")
elif guess < number:
print("It's bigger...")
elif guess > number:
print("It's not so big.")
示例运行
Guess the number! Is it... 2 It's bigger... Is it... 5 It's bigger... Is it... 10 It's not so big. Is it... 7 Hooray! You guessed it right!
even.py
# Asks for a number.
# Prints if it is even or odd
number = float(input("Tell me a number: "))
if number % 2 == 0:
print(int(number), "is even.")
elif number % 2 == 1:
print(int(number), "is odd.")
else:
print(number, "is very strange.")
示例运行
Tell me a number: 3 3 is odd.
Tell me a number: 2 2 is even.
Tell me a number: 3.4895 3.4895 is very strange.
average1.py
# keeps asking for numbers until 0 is entered.
# Prints the average value.
count = 0
sum = 0.0
number = 1 # set to something that will not exit the while loop immediately.
print("Enter 0 to exit the loop")
while number != 0:
number = float(input("Enter a number: "))
if number != 0:
count = count + 1
sum = sum + number
if number == 0:
print("The average was:", sum / count)
示例运行
Enter 0 to exit the loop Enter a number: 3 Enter a number: 5 Enter a number: 0 The average was: 4.0
Enter 0 to exit the loop Enter a number: 1 Enter a number: 4 Enter a number: 3 Enter a number: 0 The average was: 2.66666666667
average2.py
# keeps asking for numbers until count numbers have been entered.
# Prints the average value.
#Notice that we use an integer to keep track of how many numbers,
# but floating point numbers for the input of each number
sum = 0.0
print("This program will take several numbers then average them")
count = int(input("How many numbers would you like to average: "))
current_count = 0
while current_count < count:
current_count = current_count + 1
print("Number", current_count)
number = float(input("Enter a number: "))
sum = sum + number
print("The average was:", sum / count)
示例运行
This program will take several numbers then average them How many numbers would you like to average: 2 Number 1 Enter a number: 3 Number 2 Enter a number: 5 The average was: 4.0
This program will take several numbers then average them How many numbers would you like to average: 3 Number 1 Enter a number: 1 Number 2 Enter a number: 4 Number 3 Enter a number: 3 The average was: 2.66666666667
编写一个程序,提示用户输入他们的姓名,如果他们输入你的姓名,则显示“That is a nice name”,如果他们输入“John Cleese”或“Michael Palin”,则告诉他们你对他们的感受;),否则告诉他们“You have a nice name”。
name = input('Your name: ')
if name == 'Bryn':
print('That is a nice name.')
elif name == 'John Cleese':
print('... some funny text.')
elif name == 'Michael Palin':
print('... some funny text.')
else:
print('You have a nice name.')
修改本节中更高或更低的程序,以跟踪用户输入错误数字的次数。如果超过3次,在最后打印“That must have been complicated”,否则打印“Good job!”。
number = 7
guess = -1
count = 0
print("Guess the number!")
while guess != number:
guess = int(input("Is it... "))
count = count + 1
if guess == number:
print("Hooray! You guessed it right!")
elif guess < number:
print("It's bigger...")
elif guess > number:
print("It's not so big.")
if count > 3:
print("That must have been complicated.")
else:
print("Good job!")
编写一个程序,要求输入两个数字。如果两个数字的和大于100,则打印“That is a big number”。
number1 = float(input('1st number: '))
number2 = float(input('2nd number: '))
if number1 + number2 > 100:
print('That is a big number.')