Python: find the smallest and largest value
题目要求:
Write a program that repeatedly prompts a user for integer numbers until the user enters 'done'. Once 'done' is entered, print out the largest and smallest of the numbers. If the user enters anything other than a valid number catch it with a try/except and put out an appropriate message and ignore the number. Enter 7, 2, bob, 10, and 4 and match the output below. [ Programming for Everybody (Getting Started with Python) 5.2 assignment ]
largest = None
smallest = None
while True:
num = input("Enter a number: ")
if num == "done" : break
try:
value = int(num)
except:
print("Invalid input")
continue
if largest is None or smallest is None:
largest = value
smallest = value
elif smallest > value:
smallest = value
elif largest < value:
largest = value
print("Maximum is", largest)
print("Minimum is", smallest)
用到的知识点:
- try/except 抛出异常后,用continue 结束本次循环进入下次循环,阻止循环终止。
- while 是 indefinite iteration; for 是definite iteration。区别在于:for 在循环体之前就知道循环的次数。
- python 的数据格式:Int, Float, String, Boolean,None
- break: Exits the currently executing loop
- continue: Jumps to the "top" of the loop and starts the next iteration
- 'is' and 'is not' Operators:
- both use in logic expressions
- 'is': similar to, but stronger than ==, implies 'is the same as'
- you shouldn’t use 'is' when you should use '=='
- 'is' used for 'True', 'False', 'None'.
- don’t use 'is' frequently, cause it’s strong equality, stronger than ==
Python: find the smallest and largest value的更多相关文章
- [LeetCode&Python] Problem 908. Smallest Range I
Given an array A of integers, for each integer A[i] we may choose any x with -K <= x <= K, and ...
- 【python cookbook】【数据结构与算法】4.找到最大或最小的N个元素
问题:想在某个集合中找出最大或最小的N个元素 解决方案:heapq模块中的nlargest()和nsmallest()两个函数正是我们需要的. >>> import heapq &g ...
- Wing IDE 5 for Python 安装及破解方法
安装Wing IDE 官网下载deb安装文件 开始安装程序 dpkg -i 文件名.deb 安装完成后打开注册界面,输入下面的License ID 后得到RequestCode,将RequestCod ...
- python模块:random
"""Random variable generators. integers -------- uniform within range sequences ----- ...
- python tricks
1. cities = ['Marseille', 'Amsterdam', 'New York', 'Londom'] # the good way for i, city in enumerate ...
- kafka实战教程(python操作kafka),kafka配置文件详解
kafka实战教程(python操作kafka),kafka配置文件详解 应用往Kafka写数据的原因有很多:用户行为分析.日志存储.异步通信等.多样化的使用场景带来了多样化的需求:消息是否能丢失?是 ...
- python操作kafka
python操作kafka 一.什么是kafka kafka特性: (1) 通过磁盘数据结构提供消息的持久化,这种结构对于即使数以TB的消息存储也能够保持长时间的稳定性能. (2) 高吞吐量 :即使是 ...
- 一些实验中用过的python函数/方法(持续更新)
衡量运行时间 很多时候你需要计算某段代码执行所需的时间,可以使用 time 模块来实现这个功能. import time startTime = time.time() # write your co ...
- python ide ---wing 注册机
注册机脚本代码如下: import sha import string BASE2 = '01' BASE10 = '0123456789' BASE16 = '0123456789ABCDEF' B ...
随机推荐
- Hive和sparksql中的dayofweek
dayofweek在hive2.2.0开始支持 ,低版本的hive没有提供原生的dayofweek函数,有时需要用到的时候不甚方便.其实低版本的sparksql和hive中可用以下方式实现dayofw ...
- div居中与div内容居中,不一样
1.div自身居中 使用margin:0 auto上下为0,左右自适应的css样式. 要让div水平居中,那么除了设置css margin:0 auto外,还不能再设置float,不然将会导致div靠 ...
- POJ 1018 Communication System (动态规划)
We have received an order from Pizoor Communications Inc. for a special communication system. The sy ...
- Codeforce 733B - Parade (枚举)
Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldi ...
- linux 环境RPM 安装MYSQL5.6
linux 环境RPM 安装MYSQL5.6 系统环境 CentOS7.2 1.关闭selinux 服务[SELinux是一种基于域-类型 模型(domain-type)的强制访问控制(MAC)安全系 ...
- [转载]web安全之token
参考:http://blog.csdn.net/sum_rain/article/details/37085771 Token,就是令牌,最大的特点就是随机性,不可预测.一般黑客或软件无法猜测出来. ...
- Linux 进程管理 ps、top、pstree命令
ps命令:查看系统中正在运行的进程 ps 是用来静态地查看系统中正在运行的进程的命令.不过这个命令有些特殊,它的部分选项不能加入"-",比如命令"ps aux" ...
- Redis Windows 安装
摘自:https://www.cnblogs.com/M-LittleBird/p/5902850.html 一.下载windows版本的Redis 去官网找了很久,发现原来在官网上可以下载的wind ...
- Oracle误删除数据恢复
select * from tablename as of timestamp to_timestamp('2018-05-04 13:30:00','yyyy-MM-dd hh24:mi:ss') ...
- Docker学习笔记之使用 Docker Compose 管理容器
0x00 概述 通过之前的介绍,我们已经基本掌握了构建.运行容器的方法,但这还远远不够,由于 Docker 采用轻量级容器的设计,每个容器一般只运行一个软件,而目前绝大多数应用系统都绝不是一个软件所能 ...