注释:单行注释在前边加#号,多行注释''' ''''
1.条件控制 if else:
(1)解决选择性问题
mood = True
if mood:
print('go to left')
else:
print('go to right')
根据 mood的值输出结果。
关键在于 if 后边的变量。
'''
一段小程序
'''
account = 'qiyue'
password = '123456'
print("please input account")
user_account = input()
print("please input password")
user_password = input()
if (account == user_account) and (password == user_password):
print('success')
else:
print('fail')
if可以单独使用,但else必须和if配对。if 里也可以嵌套if。
(2)根据某一个变量不同的取值执行不同的事情。(elif 表示 else+if使代码更简洁)
a = input() #接受的统一认为是字符串类型,下边比较的是数字类型,所以需要装换。
a = int(a)
if a == 1:
print('apple')
elif a == 2:
print('orange')
elif a == 3:
print('banana')
else:
print('shopping')