Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.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_Python 3.x - Fatal编程技术网

减少“的价值”;我";用Python

减少“的价值”;我";用Python,python,python-3.x,Python,Python 3.x,当num不在10-20之间时,我需要减小I在else部分中的值。我该怎么做 这是我的密码: arr = [] for i in range(10,20): num = int(input("Enter Number: ")) if num > 10 and num <= 20: arr.append(num) else: i = i - 1 print(arr) arr=[] 对于范围(10,20)内的i

num
不在
10-20之间时,我需要减小
I
else
部分中的值。我该怎么做

这是我的密码:

arr = []

for i in range(10,20):
    num = int(input("Enter Number: "))
    if num > 10 and num <= 20:
        arr.append(num)
    else:
        i = i - 1
print(arr)
arr=[]
对于范围(10,20)内的i:
num=int(输入(“输入数字:”)

如果num>10且num您的循环在整数范围内循环。在循环结束时减少int
i
没有帮助,因为在下一个循环周期中,超出范围的下一个实例被视为
i

你可以用不同的方法来解决这个问题。一个是@Tomerikoo在评论中提到的

如果要使循环构造与尝试的类似,可以执行以下操作:

arr = []
i = 0
while i < 10:
    num = int(input("Enter Number: "))
    i += 1
    if num > 10 and num <= 20:
        arr.append(num)
    else:
        i = i - 1
print(arr)
arr=[]
i=0
当我<10时:
num=int(输入(“输入数字:”)
i+=1

如果num>10和num,欢迎使用StackOverflow。你的问题我不清楚。你最终想要达到什么目标?数组大小为10,数字介于10和20之间,是否手动填充?你确定你需要减少i,而不是num吗?不要忘记将更改后的数字添加到数组中。这是否回答了您的问题。您应该保持循环的原样,不要试图更改
i
。在每次迭代中,您独立地请求输入,直到得到所需的数字entered@YashShah嗨,Yash,我需要得到10到20之间的计数,可能是0到10。但答案是一样的,不是吗?你好,卢卡奇,谢谢。我对我的英语感到抱歉。如果数字不在10和20之间,我需要减少I count并在该I空间中保存下一个值。同样,正如我所说,您甚至不使用
I
。列表方法
append
总是在末尾添加项目,因此您不必担心
i
arr = []
while len(arr)<10:
    num = int(input("Enter Number: "))
    if num > 10 and num <= 20:
        arr.append(num)
print(arr)