**floor=int(input(“Floor: ”))
if floor>13:** # if condition:
**actualFloor=floor-1**
# statement what you would like to do
#如果楼层数高于13层的话,实际层数是楼层数减1(传说中的13楼标14层)
**else:** # else:
**actualFloor=floor** # 12楼就是12楼~
**print(“The elevator will travel to the actual floor ”,actualFloor)**
# 特别提示!!!格式一定要写成这样!一般写完if条件回车之后,statement那行会自动缩进表示这句statement是在if条件下执行。如果编辑器没有缩进,自己一定要缩进!!!如果下一句不是if条件下执行的语句,取消缩进就可以了。
# 注意执行顺序和跳过语句!!!使用前可比较if…else…和if…if…if…和if…elif…else…等模式的结果决定用哪一个
# 一般程序执行条件是,如果符合if,会跳过else,和matlab下switch运行模式相同
# 所以不用找了,python没有switch语句
**RATE1=0.10
RATE2=0.25
RATE1_SINGLE_LIMIT=32000.0
RATE2_MARRIED_LIMIT=64000.0
income=float(input("Please enter you income: "))
maritalStatus=input("Please enter s for single, m for married: ")
tax1=0.0
tax2=0.0
if maritalStatus == "s" :
if income <= RATE1_SINGLE_LIMIT :
tax1 = RATE1 * income
else :
tax1 = RATE1 * RATE1_SINGLE_LIMIT
tax2 = RATE2 * (income - RATE1_SINGLE_LIMIT)
else :
if income <= RATE2_MARRIED_LIMIT :
tax1 = RATE1 * income
else :
tax1 = RATE1 * RATE2_MARRIED_LIMIT
tax2 = RATE2 * (income - RATE2_MARRIED_LIMIT)
totalTax = tax1 + tax2
print("The tax is $%.2f" % totalTax)
# **Output:**
# Please enter your income: 80000
# Please enter s for single, m for married: m
# The tax is $10400.00**
**richter = float(input("Enter a magnitude on the Richter scale: "))
if richter >= 8.0 :
print("Most structures fall")
elif richter >= 7.0 :**# elif == else if
**print("Many buildings destroyed")
elif richter >= 6.0 :
print("Many buildings considerably damaged, some collapse")
elif richter >= 4.5 :
print("Damage to poorly constructed buildings")
else :
print("No destruction of buildings")**
> , > = , < , < = , = = (equal), ! = (not equal)
完全遵照三次元的逻辑。但是!需要注意!Python中 **=** 的意思是赋值(等价于R里面的**->** 或者 <- ), 两个等号,也就是 **= =** 才是等于,用于逻辑判断.
一定要区分赋值和判断!!!
# 栗子 1
**c = 6 #** 给变量c赋值6;
**c == 6 #** 是判断c等于6.
# 栗子 2