Home>

Good afternoon!

How can I make it so that I can check the entered data duringinput()(without helpIfandfor)

Data is numeric only (intandfloat)

For example, forjI need numbers between 2 and 100000...

In the worst case, an error should pop up (not print("Error"), but a python error)

import numpy as np
rez= list()
j= int(input(""))
for i in range (0, j):
    a, b, c= input("").split(" ")
    a= eval(a)
    b= eval(b)
    c= eval(c)
    v= abs(40 -a) + abs(30 -b) + abs(75 -c)
    rez append(v)
rez= np.array(rez)
print(res.sum())
  • Answer # 1

    You can still throw the desired exception like this:

    a= 100000
    assert 2< a< 10000, "Invalid value entered"
    '''
    --------------------------------------------------------------------------
    AssertionError Traceback (most recent call last)
    <ipython-input-863-dc06aae1cd56> in<module>      1 a= 100000
    ----> 2 assert 2< a< 10000, "Invalid value entered"
    AssertionError: Invalid value entered
    
  • Answer # 2

    You can still throw the desired exception like this:

    a= 100000
    assert 2< a< 10000, "Invalid value entered"
    '''
    --------------------------------------------------------------------------
    AssertionError Traceback (most recent call last)
    <ipython-input-863-dc06aae1cd56> in<module>      1 a= 100000
    ----> 2 assert 2< a< 10000, "Invalid value entered"
    AssertionError: Invalid value entered
    
  • Answer # 3
    x= -1
    try:
        x= int(input("Please enter a number: "))
        if 2 > x< 10000:
            raiseValueError()
    exceptValueError:
        print("Oops! That was no valid number. Try again...")
    print(x)
    

    https://docs.python.org/3/tutorial/errors.html

    But the condition is wrong.

    CrazyElf2022-01-29 13:55:01

    strange, the author of the question seemed to want a solution without the help of If and for, but this answer suited him))

    SergFSM2022-01-29 19:13:44
  • Answer # 4
    x= -1
    try:
        x= int(input("Please enter a number: "))
        if 2 > x< 10000:
            raiseValueError()
    exceptValueError:
        print("Oops! That was no valid number. Try again...")
    print(x)
    

    https://docs.python.org/3/tutorial/errors.html

    But the condition is wrong.

    CrazyElf2022-01-29 13:55:01

    strange, the author of the question seemed to want a solution without the help of If and for, but this answer suited him))

    SergFSM2022-01-29 19:13:44