python学习11——if 语句。
Posted shannon-v
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python学习11——if 语句。相关的知识,希望对你有一定的参考价值。
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. 1. You pick it up and hope it can help you find the way. 2.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 语句。的主要内容,如果未能解决你的问题,请参考以下文章