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.")
原文地址:https://www.cnblogs.com/shannon-V/p/9550102.html
时间: 2024-09-29 00:25:43