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")更多推荐
发布评论