If条件判断
Input()默认类型是字符串
Python可连续条件表达 4<a<49
三元运算符
条件为真 if(条件表达式)else条件为假
a=input("请输入数字:")
if int(a)<3:
print(a)
d=68
if 5<d<90:
print(d)
print("a是小于3" if(int(a)<3) else "昂菲亚特")
|
|
|
多分枝
If
Elif
Elif
score=int(input("请输入一个0到100的数字:"))
grade=""
if score<0 or score>100:
score = int(input("输入错误!请输入一个0到100的数字:"))
else:
if score>90:
grade="A"
elif score>80:
grade="B"
elif score > 70:
grade = "C"
elif score > 60:
grade = "D"
elif score < 60:
grade = "E"
print("成绩为{0},等级为{1}".format(score,grade))
|
|
|
循环变量for x in 可迭代对象(数字、字符串、列表、字典、元组、迭代器对象、迭代函数)
for x in range(5):
for y in range(5):
print(x,end="\t")
print("\n")
|
|
|
九九乘法表
for m in range(1,10):
for n in range(1,m+1):
print("{0}*{1}={2}".format(n,m,m*n),end="\t")
print("\n")
|
|
|
continue语句重头再来
录入大于0的员工工资,并计算平均工资mypython2
employ_num=0
salary=[]
salary_sum=0
while True :
s=input("请输入员工工资:")
if s.upper()=="Q":
print("已退出输入框")
break
if float(s)<0:
continue
employ_num+=1
salary_sum+=float(s)
salary.append(float(s))
print("员工人数为:{0}".format(employ_num))
print("员工工资为:",salary)
print("平均工资为:{0}".format(salary_sum/employ_num))
|
|
|