【Python-2.7】if-elif-else
2017-09-27 01:11
417 查看
要求:
序数:序数表示位置,如 1st 和 2nd。大多数序数都以 th 结尾,只有 1、2 和 3
例外。
在一个列表中存储数字 1~9。
遍历这个列表。
在循环中使用一个 if-elif-else 结构,以打印每个数字对应的序数。输出内容
应为 1st、2nd、3rd、4th、5th、6th、7th、8th 和 9th,但每个序数都独占一行。
numbers = list (range (1,10))
for number in numbers:
if number == 1:
print ("1st")
elif number == 2:
print ("2nd")
else:
print (str(number) + 'th')
输出为:
>>> ================================ RESTART ================================
>>>
1st
2nd
3th
4th
5th
6th
7th
8th
9th
注意:最后一行number需要声明类型为‘str’。
序数:序数表示位置,如 1st 和 2nd。大多数序数都以 th 结尾,只有 1、2 和 3
例外。
在一个列表中存储数字 1~9。
遍历这个列表。
在循环中使用一个 if-elif-else 结构,以打印每个数字对应的序数。输出内容
应为 1st、2nd、3rd、4th、5th、6th、7th、8th 和 9th,但每个序数都独占一行。
numbers = list (range (1,10))
for number in numbers:
if number == 1:
print ("1st")
elif number == 2:
print ("2nd")
else:
print (str(number) + 'th')
输出为:
>>> ================================ RESTART ================================
>>>
1st
2nd
3th
4th
5th
6th
7th
8th
9th
注意:最后一行number需要声明类型为‘str’。
相关文章推荐
- Python-if-elif-else语句
- Python-if-elif-else语句
- Python轻松入门-07 if elif else
- Python 3从入门到精通6-if,elif,else语句
- Python if for else elif while break loop pass 控制流工具
- Python-if-elif-else语句
- Python第一弹 使用if-elif-else
- python 流控制 if else, elif
- python 笔记 if语句中的elif和else 《笨办法学Python》习题30 ——1.2
- Python 条件判断语句(if ,elif, else)
- Python的 if .else.elif语句详解
- 2.8 python中的if...elif...else语句 [python入门教程]
- PyThon---if-elif-else用法
- Python学习入门基础教程(learning Python)--3.3.4 Python的if-elif-else语句
- Python学习-if_elif_else
- Python 3基础教程8-if elif else语句
- 关于python中的if-elif-else格式的问题
- 【Python入门】9、if、elif、else,if的嵌套
- python基础教程共60课-第23课if、elif、else
- python3 if elif else 分支语句