N
N
ngapp2020-05-06 17:00:46
Python
ngapp, 2020-05-06 17:00:46

How to check if all values ​​are positive and all input values ​​inside a for loop?

Hello everyone, I have a for loop to populate list data.
Inside the loop, you need to check whether these are numbers and whether they are positive, how can I do it?

val = 3
    for i in range(val):
      x.append(int(input("Input BBU " + str(i+1) + ": ")))
    print(x)


Tried like this, but the value is NONE.
val = 3
for i in range(val):
      ab = x.append(input("Input BBU " + str(i+1) + ": "))
      while True:
        print(ab)
        try:
          val = int(ab[i])
          if val <=0:
            print("not")
            continue
          break;
        except ValueError:
            print("notnot")
    print(x)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Miit, 2020-05-06
@hypnogaja

Maybe this will help

x = []
val = 3
i = 0
while i < val:
    value = input(f'Input BBU {i + 1}: ')
    try:
        value = int(value)
        if value > 0:
            x.append(value)
            i += 1
            print('added')
        else:
            print('write int > 0')
    except ValueError:
        print('write int')

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question