Python新手:设置多个整数的限制作为输入(Python novice: Setting limits for multiple integers taken as input)

编程入门 行业动态 更新时间:2024-10-25 08:27:07
Python新手:设置多个整数的限制作为输入(Python novice: Setting limits for multiple integers taken as input)

我正在尝试编写一个程序,它可以在一行中用空格分隔输入3个数字(i,j和k),每个数字的限制从1到1000(含)。 我需要一些关于如何一次设置所有三个数字的限制的建议。 提前致谢! 我的错误代码如下:

i, j, k = input("Please input 3 numbers between 1 and 1000:").split() i, j, k = int(i), int(j), int(k) if i, j, k >1000 or i, j, k <1: print ("Please try again") else: print ("Thank you!")

I'm trying to write a program that would take an input of 3 numbers (i, j & k) in a single line separated by spaces, with limits for each number being from 1 to 1000 (inclusive). I need some advice regarding how to set the limit for all three numbers at once. Thanks in advance! My buggy code is below:

i, j, k = input("Please input 3 numbers between 1 and 1000:").split() i, j, k = int(i), int(j), int(k) if i, j, k >1000 or i, j, k <1: print ("Please try again") else: print ("Thank you!")

最满意答案

使用Python的any()和all()函数来测试大量值的条件。 在你的情况下它应该看起来像

if all([1<=x<=1000 for x in [i,j,k]]): print ("Thank you!") else: print ("Please try again")

Use Python's any() and all() functions for testing conditions on large number of values. In your case it should look like

if all([1<=x<=1000 for x in [i,j,k]]): print ("Thank you!") else: print ("Please try again")

更多推荐

本文发布于:2023-07-29 20:41:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1319585.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   整数   新手   novice   Python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!