语法"/>
python基础05 if while for 语法
print 一次性输出多个变量的值
name="liuaoling"
age=19
gender="女"
print("姓名是:%s,年龄是:%d,性别:%s"%(name,age,gender))
if语句:
you = input("你去吗?")
youwife = input("你媳妇去吗?")
if you =="去" and youwife=="去":
print ("这件大事能办成!")
else:
print("失败!")
color = input ("你白吗?")
money = input ("你的财产总和:")
beautiful = input("你长得漂亮吗?")
if color=="白"and money>="100000"and beautiful=="美":
print("你是白富美!")
else:
print ("你是丑丑!")
if 语句的注意点:
1.记住是两个==
2.else后面的:
3.if else 要对齐
if elif 的使用:
where = input ("你最想去的地方是?")
if where=="香港":
print("是的,那是我最想去的地方")
elif where=="巴基斯坦":
print("那是我第二想去的地方")
elif where=="撒哈拉":
print("这个地方也想去")
else :
print("其实,那里都想去!")
while循环:
num=1
while num<=100:
num=num+2
print(num)
if嵌套循环:
tickt = input("你有车票吗?")
dangrous = input("你有危险品吗?")
if tickt == "有":
print("接下来要进行安检了!")
if dangrous =="有" :
print("对不起,安检不通过")
else:
print("赶快上车吧!")
else:
print ("车票都没有,别来坐车咯")
复合赋值运算符:
打印乘法表:
i = 1
while i<=9:
j=1
while j<=i:
print("%d*%d=%d\t" %(j,i,i*j ),end="")
j+=1
print("")
i+=1
石头剪刀布:
import random
player=int(input("请输入 0 石头 1 剪刀 2 布"))
computer=random.randint(0,2)
if (player==0 and computer==1)or (player==3 and computer==0)or (player==1 and computer==2):
print ("victor")
elif player==computer:
print("平局")
else :
print("defeat")
for循环:
break continue:
打印1-100前20个偶数
i =1
n=0
while i<=100:
if i%2==0:
print("--------")
print(i)
n+=1
if n==20:
break
i+=1
while循环嵌套中break 和continue的作用范围
离谁近对谁起作用
更多推荐
python基础05 if while for 语法
发布评论