英文题目:
5.2 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.
中文翻译:
5.2编写程序,反复提示用户输入整数,直到用户输入“done”。一旦输入“完成”,打印出最大和最小的数字。如果用户输入的不是有效的数字,则使用try/except捕获该数字,并输出适当的消息,忽略该数字。输入7、2、bob、10和4并匹配下面的输出。
#求个神,大学题, 打印最大和最小的数字,如果用户输入的不是有效数字, 用try/except ,
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
2条回答 默认 最新
- m0_57901298 2021-09-18 19:19关注
5.2
def main(): arr = [] while True: num = input('Input a number:') if num == 'done': break else: try: arr.append(int(num)) except ValueError: print('Invalid input') print(f'maximum is {max(arr)}') print(f'mininum is {min(arr)}') if __name__ == '__main__': main()
result:
Input a number:7 Input a number:2 Input a number:bob Invalid input Input a number:10 Input a number:4 Input a number:done maximum is 10 mininum is 2
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 1无用
悬赏问题
- ¥30 关于用python写支付宝扫码付异步通知收不到的问题
- ¥50 vue组件中无法正确接收并处理axios请求
- ¥15 隐藏系统界面pdf的打印、下载按钮
- ¥15 MATLAB联合adams仿真卡死如何解决(代码模型无问题)
- ¥15 基于pso参数优化的LightGBM分类模型
- ¥15 安装Paddleocr时报错无法解决
- ¥15 python中transformers可以正常下载,但是没有办法使用pipeline
- ¥50 分布式追踪trace异常问题
- ¥15 人在外地出差,速帮一点点
- ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改