Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:简化计数程序_Python_Loops_Counting_Simplification - Fatal编程技术网

Python:简化计数程序

Python:简化计数程序,python,loops,counting,simplification,Python,Loops,Counting,Simplification,我对Python非常陌生,我制作了一个程序,要求用户选择2个数字。第一个必须介于1和10之间,第二个必须介于100和200之间。然后程序要求用户选择1到5之间的第三个数字。分别调用这3个数字X、Y和Z,然后程序按Z的步长从X到Y进行计数 预期行为:如果用户选择10、105和5,那么Python应该打印15、20、25等等,最多打印105。此外,如果用户尝试输入程序指定值之外的值,它将拒绝输入并要求用户重试 这个程序可以工作,但是,正如我所说,我对Python非常陌生,所以如果我以最理想的方式完成

我对Python非常陌生,我制作了一个程序,要求用户选择2个数字。第一个必须介于1和10之间,第二个必须介于100和200之间。然后程序要求用户选择1到5之间的第三个数字。分别调用这3个数字X、Y和Z,然后程序按Z的步长从X到Y进行计数

预期行为:如果用户选择10、105和5,那么Python应该打印15、20、25等等,最多打印105。此外,如果用户尝试输入程序指定值之外的值,它将拒绝输入并要求用户重试

这个程序可以工作,但是,正如我所说,我对Python非常陌生,所以如果我以最理想的方式完成它,我会感到非常惊讶。你认为有没有办法简化下面的代码以提高效率

代码如下:

x = int(input("Please enter any number between 1 and 10: ").strip())

while x > 10 or x < 1:
    print("No!  I need a number between 1 and 10.")
    x = int(input("Please enter any number between 1 and 10: ").strip())

y = int(input("Now enter a second number between 100 and 200: ").strip())

while y > 200 or y < 100:
    print("No!  I need a number between 100 and 200.")
    y = int(input("Please enter any number between 100 and 200: ").strip())

print("Now we're going to count up from the first to the second number.")
print("You can count every number, every second number, and so on. It's up to you.")
z = int(input("Enter a number between 1 and 5: ").strip())

while z > 5 or z < 1:
    print("No.  I need a number between 1 and 5!")
    z = int(input("Enter a number between 1 and 5: ").strip())

print("You have chosen to count from {} to {} in steps of {}.".format(x, y, z))

input("\nPress Enter to begin")

for q in range(x,y):
    if q == x + z:
    print(q)
    x = x + z
x=int(输入(“请输入介于1和10之间的任何数字:”).strip()
当x>10或x<1时:
打印(“不!我需要一个介于1和10之间的数字。”)
x=int(输入(“请输入介于1和10之间的任何数字:”).strip()
y=int(输入(“现在输入第二个介于100和200之间的数字:”).strip()
y>200或y<100时:
打印(“不!我需要一个介于100和200之间的数字。”)
y=int(输入(“请输入100到200之间的任何数字:”).strip()
print(“现在我们要从第一个数字开始数到第二个数字。”)
打印(“你可以数每一个数字,每一秒的数字,等等。这取决于你。”)
z=int(输入(“输入一个介于1和5之间的数字:”).strip()
当z>5或z<1时:
打印(“不,我需要一个介于1和5之间的数字!”)
z=int(输入(“输入一个介于1和5之间的数字:”).strip()
打印(“您选择了以{}的步长从{}到{}进行计数。”.format(x,y,z))
输入(“\n按Enter开始”)
对于范围(x,y)内的q:
如果q==x+z:
打印(q)
x=x+z
这个计划是可行的,但我的直觉告诉我必须有一个更干净的方法来做到这一点。如果您有任何建议,我们将不胜感激


干杯

您可以将循环替换为:

for q in range(x,y,z):
    print(q)

将第三个参数添加到范围表达式中会使其按步计数。

好的,我喜欢压缩代码,也喜欢挑战

砰 代码:

x,y,z=None,None,None
而x==None或x>10或x<1:x=int(输入(“请输入介于1和10之间的任何数字:”)
而y==None或y>200或y<100:y=int(输入(“请输入100到200之间的任何数字:”)
print(“现在我们要从第一个数字开始数到第二个数字。”+'\n'+“你可以数到每一个数字,每一秒的数字,依此类推。这取决于你。”)
而z==None或z>5或z<1:z=int(输入(“输入一个介于1和5之间的数字”))
tmp=raw_input(str(“您选择了以{}..format(x,y,z))的步骤从{}到{}计数)+'\n'+“按Enter键开始”)
x=x-1
对于范围(x、y、z)中的q:打印(q)
打印x+z

尽可能精简。

您希望评论的工作代码通常属于@TemporalWolf,因此我不必对此进行精确评论:)此外,代码和问题似乎适合CR:甚至会在那里获得投票。投票结束。我投票结束这个问题,因为它非常适合啊。谢谢各位。我将把这个主题转载到那里。前面的代码没有打印1-100乘5的100。而且,它从1开始,数到6,11,16等等,所以我修正了这个问题
x, y, z = None, None, None
while x == None or x > 10 or x < 1: x = int(input("Please enter any number between 1 and 10: "))
while y == None or y > 200 or y < 100: y = int(input("Please enter any number between 100 and 200: "))
print("Now we're going to count up from the first to the second number."+'\n'+"You can count every number, every second number, and so on. It's up to you.")
while z == None or z > 5 or z < 1: z = int(input("Enter a number between 1 and 5: "))
tmp=raw_input(str("You have chosen to count from {} to {} in steps of {}.".format(x, y, z))+'\n'+"Press Enter to begin")
x = x-1
for q in range(x,y,z): print(q)
print x+z