建立一个数组,实现数组的遍历,基于for和if嵌套实现,需要注意的是,for、if、else语句后一定要有“:”
\n为空一行
print("一个数,除以三余二、除以五余三、除以七余二")
number=[12,34,23,5,9,7]
for shu in number:
if shu%3==2 and shu%5==3 and shu%7==2:
print("当前遍历数值为:",shu,"满足除以三余二、除以五余三、除以七余二")
else:
print("当前遍历数值为:",shu,"数值不满足")
用户输入一个任意数,可直接调用input函数
print("一个数,除以三余二、除以五余三、除以七余二")
shu=int(input("请输入一个数字:"))
if shu%3==2 and shu%5==3 and shu%7==2:
print("当前遍历数值为:",shu,"满足除以三余二、除以五余三、除以七余二")
else:
print("当前遍历数值为:",shu,"数值不满足")
多个if和else嵌套
shu=int(input("请输入一个数字:"))
if shu >= 0:
if shu > 0:
print("当前数值大于0")
else:
print("当前数值为0")
else:
print("当前为负数")
关于if……elif……else的使用:
shu=int(input("请输入一个数字:"))
if shu == 1:
print("当前选项为1")
elif shu == 2:
print("当前选项为2")
elif shu == 3:
print("当前选项为3")
else:
print("当前输入无法识别")
当使用布尔进行判断时,正确的语法如下,假设变量为a:
if a:
if not a: