题目要求:

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)

用到的知识点:

  1. try/except 抛出异常后,用continue 结束本次循环进入下次循环,阻止循环终止。
  2. while 是 indefinite iteration; for 是definite iteration。区别在于:for 在循环体之前就知道循环的次数。
  3. python 的数据格式:Int, Float, String, Boolean,None
  4. break: Exits the currently executing loop
  5. continue: Jumps to the "top" of the loop and starts the next iteration
  6. 'is' and 'is not' Operators: 
    1. both use in logic expressions
    2. 'is': similar to, but stronger than ==, implies 'is the same as'
    3. you shouldn’t use 'is' when you should use '=='
    4. 'is' used for 'True', 'False', 'None'.
    5. don’t use 'is' frequently, cause it’s strong equality, stronger than ==

Python: find the smallest and largest value的更多相关文章

  1. [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 ...

  2. 【python cookbook】【数据结构与算法】4.找到最大或最小的N个元素

    问题:想在某个集合中找出最大或最小的N个元素 解决方案:heapq模块中的nlargest()和nsmallest()两个函数正是我们需要的. >>> import heapq &g ...

  3. Wing IDE 5 for Python 安装及破解方法

    安装Wing IDE 官网下载deb安装文件 开始安装程序 dpkg -i 文件名.deb 安装完成后打开注册界面,输入下面的License ID 后得到RequestCode,将RequestCod ...

  4. python模块:random

    """Random variable generators. integers -------- uniform within range sequences ----- ...

  5. python tricks

    1. cities = ['Marseille', 'Amsterdam', 'New York', 'Londom'] # the good way for i, city in enumerate ...

  6. kafka实战教程(python操作kafka),kafka配置文件详解

    kafka实战教程(python操作kafka),kafka配置文件详解 应用往Kafka写数据的原因有很多:用户行为分析.日志存储.异步通信等.多样化的使用场景带来了多样化的需求:消息是否能丢失?是 ...

  7. python操作kafka

    python操作kafka 一.什么是kafka kafka特性: (1) 通过磁盘数据结构提供消息的持久化,这种结构对于即使数以TB的消息存储也能够保持长时间的稳定性能. (2) 高吞吐量 :即使是 ...

  8. 一些实验中用过的python函数/方法(持续更新)

    衡量运行时间 很多时候你需要计算某段代码执行所需的时间,可以使用 time 模块来实现这个功能. import time startTime = time.time() # write your co ...

  9. python ide ---wing 注册机

    注册机脚本代码如下: import sha import string BASE2 = '01' BASE10 = '0123456789' BASE16 = '0123456789ABCDEF' B ...

随机推荐

  1. 20165316 实验四 Android程序设计

    20165316 孙勖哲 第四次实验 Android 程序设计1 参考 http://www.cnblogs.com/rocedu/p/6371315.html#SECANDROID, 安装 Andr ...

  2. PHP $_SERVER 及用户真实IP

    $_SERVER 是一个包含了诸如头信息(header).路径(path).以及脚本位置(script locations)等等信息的数组.这个数组中的项目由 Web 服务器创建.不能保证每个服务器都 ...

  3. 自写Jquery插件 Datagrid

    原创文章,转载请注明出处,谢谢!https://www.cnblogs.com/GaoAnLee/p/9086582.html 废话不多说,先上个整体效果: html <div id='data ...

  4. The Little Prince-11/29

    The Little Prince-11/29 The wheat fields have nothing to say to me. And that is sad. But you have ha ...

  5. Jquery部分小结

    window.onload 必须等待网页中所有的内容加载完毕后(包括图片)才能执行,如果多个,只会执行最后一个;$(document).ready() 网页中所有DOM结构绘制完毕后就执行,可能DOM ...

  6. C++中虚函数的作用

    一, 什么是虚函数(如果不知道虚函数为何物,但有急切的想知道,那你就应该从这里开始) 简单地说,那些被virtual关键字修饰的成员函数,就是虚函数.虚函数的作用,用专业术语来解释就是实现多态性(Po ...

  7. java.lang.IllegalStateException: Failed to check the status of the service

    java.lang.IllegalStateException: Failed to check the status of the service com.pinyougou.sellergoods ...

  8. RedisLive安装

    环境安装 Python2.7 [root@ ~]# yum install -y readline readline-devel [root@ ~]# yum install sqlite-devel ...

  9. js 解密 16进制转10进制,再取ascii码的对应值

    如:\x64 对应 16进制 0x64 转10进制就是 0x64.toString(10) == 100, 查对应的ascii码表得到 ‘d' <div id=code style='displ ...

  10. P2233 [HNOI2002]公交车路线

    洛咕原题 dp->矩阵乘法 首先我们可以得出一个状态转移方程 f[i][j]=f[i-1][j-1]+f[i-1][j+1] 蓝后发现,我们可以把这转化为一个8*8的转移矩阵 然后跑一遍矩阵快速 ...