1. 程式人生 > >python學習11——if 語句。

python學習11——if 語句。

python學習 wake 17. dream people som bubuko ant tab

people=int(input("How many people are there in this city?"))
cats = int(input("How many cats are there in this city?"))
dogs = int(input("How many dogs are there in this city?"))
if people < cats:
 print("Too many cats! The world is doomed!")
elif people > cats:
 print("Not many cats! The world is saved!
") else: print("We can‘t decide.") dogs += 10 if people >= dogs: print ("People are greater than or equal to dogs.") else: print("People are less than dogs")

輸出結果:

技術分享圖片

註意:

if 、elif、else 後需要加冒號。

數值型數據需要使用int()

測試小遊戲:

print("Now you are lost in the forest, you can see a house in your left and a light comes from right, where do you want to go?
") print("1. To the house in the left.") print("2. To the light in the right.") choose_1=input("*") if choose_1 == "1": print("There are some food in the table, will you eat it?") print("1. Yes, I\‘m so hungry.") print("2. No, I will leave tomorrow.") choose_2 =input("*") if choose_2 =="
1": print("You ate poison and died, good job!") else: print("You wake up the other day, and find the way back home.") elif choose_1=="2": print("You find the thing that shines.\n1. You pick it up and hope it can help you find the way.\n2.You throw it away.") choose_3=input("*") if choose_3=="1": print("It was a shining snake and you were biten then die. Good job!") elif choose_3=="2": print("You were lost in the forest forever.") else: print("You wake up and it was just a bad dream.") else: print("You stood here until the wolf came and ate you.")

python學習11——if 語句。