写程序跟烹饪很像,
当然对门外汉的我们来说,“烹饪”是不准确用词,还是做饭炒菜更(tie)接(jin)地(shi)气(qing)一点。
从简单的食材基本的菜谱做起,把大米饭焖好先
在这个视频里我们两个程序实例继续不干涉程序执行顺序(美元换算成人民币、输入两个数值,输出和、差、积、商和余数),任由它们从上至下一条语句一条语句的执行
# ---------- MATH ON 2 VALUES ----------
# Ask the user to input 2 values and store them in variables num1 num2
# split() splits input using whitespace
num1, num2 = input('Enter 2 numbers separated by space: ').split()
# Convert the string into regular numbers Integer
num1 = int(num1)
num2 = int(num2)
# Add the values entered and store in sum
sum = num1 + num2
# Subtract values and store in difference
difference = num1 - num2
# Multiply the values and store in product
product = num1 * num2
# Divide the values and store in quotient
quotient = num1 / num2
# Use modulus on the values to find th remainder
remainder = num1 % num2
# Print the results
# format() loads the variable values in order into the {} placeholders
print("{} + {} = {}".format(num1,num2,sum))
print("{} - {} = {}".format(num1,num2,difference))
print("{} * {} = {}".format(num1,num2,product))
print("{} / {} = {}".format(num1,num2,quotient))
print("{} % {} = {}".format(num1,num2,remainder))
可谁让我们的现实生活充满了选择呢,
所以我们的程序语言必须加点选择分支处理的小料,
条件分支在这个视频里初试莺啼(“密码对否”和“你是吸血鬼不是爱丽丝”),
name = 'Bob'
age = 3000
if name == 'Alice':
print('Hi, Alice.')
elif age < 12:
print('You are not Alice, kiddo.')
elif age > 2000:
print('Unlike you, Alice is not an undead, immortal vampire.')
elif age > 100:
print('You are not Alice, grannie.')
else:
print('Hi, did you know where Alice is?')
在下个视频里我们继续条件分支,会给出三个实例~~~