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 the numbers 4,5,6,7 and Match the desired output as shown below:
Invalid input Maximum is 7 Minimum is 4
please build your program based on the sample code:
largest = None smallest = None while True: num = raw_input("Enter a number: ") if num == "done" : break print num
``` i = '' nums = [] while i != 'done': ....if i.isdigit(): ........nums.append(float(i)) ....i = input('Enter a number (end by "done"): ') print(max(nums), min(nums)) ```
necomancer
2016-09-21 23:23:24 +08:00
好吧,还有 invalid
i = input('Enter a number (end by "done"): ') nums = [] while i != 'done': ....if i.isdigit(): ........nums.append(float(i)) ....else: ........print('Invalid input!') ....i = input('Enter a number (end by "done"): ') if nums: ....print(max(nums), min(nums))
@necomancer 强行用 list 为啥不能 keep 一个 max 和一个 min 多了好多逻辑 而且还没有 try block 不符合题目要求
lz 这题这么简单还上来问 think first before posting 是最基本的好嘛 而且这不算作弊么
czk1997
2016-09-22 08:27:48 +08:00
1. 善用 google 搜索,网上第一条 github 就是答案 2.这题真的不难……用数组记录调用 max 或者 min 或者只记录当前最大最小值就好,方法很多。 3.这题用户不会有你们想的那么复杂吧,因为这题网上不是说了么: Programming For Everybody Coursera Course ,初学者作业题又不是面试题你们讨论的那么复杂干啥……面试题限定条件随便加啊……