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的作用範圍

離誰近對誰起作用

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章