1.
# 单分支结构#1
# 输入两个整数存放于变量a和b中,并使得a中存放的数据小于b中存放的数据
a = int(input('Please input 1st integer: '))
b = int(input('Please input 2nd integer: '))
print('Before processing: ')
print('a=%d,b=%d'%(a,b))
if a>b:t=aa=bb=t
print('After processing: ')
print('a=%d,b=%d'%(a,b))
C:\Users\Amber\AppData\Local\Programs\Python\Python36\python.exe C:/Users/Amber/Desktop/单分支结构——001.py
Please input 1st integer: 3
Please input 2nd integer: 2
Before processing:
a=3,b=2
After processing:
a=2,b=3
Process finished with exit code 0
2. 双分支结构
# 双分支结构#1
# 比较两个数的大小,输入两个整数,输出较大的数
a = float(input('Please input 1st number: '))
b = float(input('Please input 2nd number: '))
if a>b:print(a)
else:print(b)
C:\Users\Amber\AppData\Local\Programs\Python\Python36\python.exe C:/Users/Amber/Desktop/双分支结构#1.py
Please input 1st integer: 3.6
Please input 2nd integer: 7.3
7.3
# 双分支结构#2
# 输入三条边的值,看是否组成三角形
a = float(input('Please input 1st number: '))
b = float(input('Please input 2nd number: '))
c = float(input('Please input 3rd number: '))
if a+b>c and b+c>a and a+c>b:print("可以组成三角形")
else:print("不可组成三角形")
C:\Users\Amber\AppData\Local\Programs\Python\Python36\python.exe C:/Users/Amber/Desktop/双分支结构#2.py
Please input 1st number: 2
Please input 2nd number: 3
Please input 3rd number: 4
可以组成三角形
# 双分支结构#3
# 划船问题,一个教师带着x个学生去划船,每条船最多装4人,问需要几条船
a = int(input('Please input 学生数目: '))
total = a+1 #总人数将老师也算在内
if total % 4 == 0:n = total // 4
else:n = total // 4 + 1
print("Total required %d boats" %(n))
Connected to pydev debugger (build 181.2784.25)
Please input 学生数目: 5
Total required 2 boats
3. 多分支结构
# 多分支结构#1
# 计算分段函数的值。当x大于1时,y=x;当x小于-1时,y=-x;当x介于-1与1之间时,y=1
x = float(input('Please input a number: '))
if x > 1:y=x
elif x <-1:y&#61;-x
else:y&#61;1
print(y)
# 多分支结构#2
# 某校学生成绩采用百分制mark&#xff0c;将其该为五级制度
x &#61; float(input(&#39;Please input a number: &#39;))
if x >100:print(&#39;Error, Please input correct number:&#39;)
elif x >&#61; 90:print("A")
elif x >&#61; 80:print (&#39;B&#39;)
elif x >&#61; 70:print (&#39;B-&#39;)
elif x >&#61; 60:print(&#39;C&#39;)
else:print(&#39;D&#39;)
# 多分支结构#3
# 已知坐标点&#xff08;x,y&#xff09;&#xff0c;判断所在象限
x &#61; float(input(&#39;Please input x: &#39;))
y &#61; float(input(&#39;Please input y: &#39;))
if x&#61;&#61;0 and y&#61;&#61;0:print(&#39;点(%d,%d)在原点:&#39; %(x,y))
elif x&#61;&#61;0 and y!&#61;0:print(&#39;点(%d,%d)y-axis:&#39;%(x,y))
elif x!&#61;0 and y&#61;&#61;0:print(&#39;点(%d,%d)x-axis:&#39;%(x,y))
elif x >0 and y>0:print(&#39;点(%d,%d)第一象限:&#39;%(x,y))
elif x>0 and y<0:print(&#39;点(%d,%d)第4象限:&#39; %(x,y))
elif x <0 and y <0:print(&#39;点(%d,%d)第3象限:&#39; %(x,y))
elif x<0 and y>0:print(&#39;点(%d,%d)第2象限:&#39; %(x,y))
#else:# print(&#39;点(%d,%d)坐标轴上:&#39; %(x,y))
4. 嵌套if语句与逻辑运算
# 判断一年是否为闰年&#xff0c;闰年条件为&#xff1a;年份能被4整除但不能被100整除&#xff0c;或者能被400整除
year &#61; int(input(&#39;Please input a year&#39;))
if year%4 &#61;&#61;0 and year % 100 !&#61;0:print("%d year in run nian"%(year))
elif year%400 &#61;&#61;0:print("%d year in run nian" %(year))
else:print("%d year is not run nian" %(year))
# 判断一年是否为闰年&#xff0c;闰年条件为&#xff1a;年份能被4整除但不能被100整除&#xff0c;或者能被400整除
# 使用逻辑判断
year &#61; int(input(&#39;Please input a year&#39;))
if (year%4 &#61;&#61;0 and year % 100 !&#61;0) or (year % 400 &#61;&#61; 0):print("%d year in run nian"%(year))
else:print("%d year is not run nian" %(year))
等效形式&#xff1a;
if (year%4 &#61;&#61;0 and year % 100 !&#61;0) or (year % 400 &#61;&#61; 0):
if (year%4 &#61;&#61;0 and year % 100) or (year % 400 &#61;&#61; 0):
if ((not(year%4) and year % 100) or year % 400 &#61;&#61; 0):
5. 编写程序
#输出三个数中最大的那个数
a &#61; float(input("Please input 1st number"))
b &#61; float(input("Please input 2nd number"))
c &#61; float(input("Please input 3rd number"))
if (a >&#61; b) and (a >&#61; c):max &#61; aprint(&#39;max number is %s&#39;%(max))
elif (b >&#61; a) and (b >&#61; c):max &#61; bprint(&#39;max number is %s&#39;%(max))
else:max &#61; cprint(&#39;max number is %s&#39; % (max))
#输出三个数中最大的那个数/改进方法2
a &#61; float(input("Please input 1st number"))
b &#61; float(input("Please input 2nd number"))
c &#61; float(input("Please input 3rd number"))
max &#61; a
if (max if (max print(&#39;max number is %s&#39;%(max))