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.\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-07-30 18:04:10

python学习11——if 语句。的相关文章

Python学习笔记8—语句

条件语句 有的程序里写的是 /usr/bin Python,表示 Python 解释器在/usr/bin 里面.但是,如果写成 /usr/bin/env,则表示要通过系统搜索路径寻找 Python 解释器.不同系统,可能解释器的位置不同,所以这种方式能够让代码更将拥有可移植性. #/usr/bin/env python #coding=utf-8 number = int(raw_input("请输入任意一个整数:")) if number == 10: print "您输入

Python学习:if语句

先举例: age = 20 if age >= 18 : print 'Adult' elif age < 18 : print 'Nonage' 计算机之所以能做很多自动化的任务,因为它可以自己做条件判断. 比如,输入用户年龄,根据年龄打印不同的内容,在Python程序中,用if语句实现: age = 20 if age >= 18: print 'your age is', age print 'adult' 根据Python的缩进规则,如果if语句判断是True,就把缩进的两行pr

Python学习 之 switch语句

1.python并没有提供switch语句,python可以通过字典实现switch语句的功能,实现方法分为两步 —首先,定义一个字典 —其次,调用字典的get()获取相应的表达式 通过字典调用函数 {1:case1,2:case2}.get(o)(x,y,*args,**kwargs) 2.示例 from __future__ import division def jia(x,y): return x+y def jian(x,y): return x-y def cheng(x,y): r

Python学习3——for语句

# python code by zzw # time:2015-7-27 # for语句注意与其他语言的区别,感觉跟shell有点像 words=['dog','windows','Linux']; for w in words:     print('the length of ',w,' is ',len(w)); 运行结果 the length of  dog  is  3 the length of  windows  is  7 the length of  Linux  is  5

Python学习笔记(语句)

print和import的更多信息 使用逗号输出 >>> print 'age:', 42age: 42 >>> 1,2,3(1, 2, 3)>>> print 1,2,31 2 3>>> print (1,2,3)(1, 2, 3) >>> name='Gumby'>>> salutation='Mr.'>>> greeting='Hello,' >>> pri

python学习之结构语句

一 循环语句: 1.1 for x in rang(n) :#rang(n)生成左闭右开区间的序列 1.2 while x 条件n: 二条件语句: if 条件表达式: elif 表达式: elif 表达式: ... else: 三 break continu 四:函数: 4.1 定义:def func (参数列表): if not isinstance(x, (int, float)):#参数检查相当于断言 raise TypeError('bad operand type') return 参

python学习11 -文件,流

打开文件 语法如下:open(name,[module[,buffering]]) ,模式 和缓冲参数都是可选的 f = open(r'C:\TEXT\somefile.txt')#如果文件不存在 Traceback (most recent call last): File "<pyshell#64>", line 1, in <module> f = open(r'C:\TEXT\somefile.txt')IOError: [Errno 2] No suc

Python学习-11.Python中的类定义

Python是一门面向对象语言,那么作为面向对象的特征——类也是有的.值得注意的是Python中一切皆对象,并不像C#中为了性能考虑,int这些在Python中也是对象.(C#中int是结构体) 如何定义一个类: 1 class Person: 2 pass 使用class关键字,上面定义了一个Person类.但是现在这个类是空白的. 接下来定义一个构造函数 1 class Person: 2 def __init__(self,name,age): 3 self.name=name 4 sel

Python学习笔记——基本语句

1.程序输出——print语句 >>> myString = 'Hello World!' >>> print myString Hello World! >>> myString 'Hello World!' 注意:在仅用变量名的时候,输出的字符串是用单引号括起来的 2.程序输入——raw_input()内建函数 >>> user = raw_input('Enter your name:') Enter your name:roo