1

How do you write a Python 3 version of a program that denies negative integer input with a warning and does not allow it to enter ? for example,

print (' Hypothenuse ')

print ('______________________________________________')

while True:
    L1=int(input('Value of L1:'))
    L2=int(input('Value of L2:'))

    if L1 >= 0:
        if L1 ==0:
            print("L1 Zero")
        else:
            print("L1 Positive Number")
    else:
        print("L1 Negative Number, Please Recheck Input")

    if L2 >= 0:
        if L2 ==0:
            print("L2 Zero")
        else:
            print("L2 Positive Number")
    else:
        print("L2 Negative Number, Please Recheck Input")

    h= pow(L1,2) + pow(L2,2)
    print('Value of Hypot',h)

    print('____________________________________________')

My Code executes after the input of L1 and L2 but does not deny the negative input. Help Please?

tripleee
  • 175,061
  • 34
  • 275
  • 318
S.Universe
  • 15
  • 2
  • 3
  • 3
    Possible duplicate of [Asking the user for input until they give a valid response](http://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – lmiguelvargasf Feb 15 '17 at 20:53

2 Answers2

1

You can use this to get a positive number

while True:
    L1 = int(input('Value of L1:'))
    if not L1 < 0:
       break

Basically, you are constantly asking the user for an input unless he provides a non-negative number. However, keep in mind that you can get an exception if the user enters a string that is not a number as 'fksjfjdskl'.

lmiguelvargasf
  • 63,191
  • 45
  • 217
  • 228
0

How do you write a Python 3 version of a program that denies negative integer input with a warning and does not allow it to enter ?

You can simply give if L1 >= 0 and L2 >= 0: soon after the while loop and thus no negative number would be taken into consideration for calculation.

Hope this serves you right!

print(' Hypothenuse ')

print('______________________________________________')

while True:
    L1 = int(input('Value of L1:'))
    L2 = int(input('Value of L2:'))

    if L1 >= 0 and L2 >= 0:
        if L1 >= 0:
            if L1 == 0:
                print("L1 Zero")
            else:
                print("L1 Positive Number")

        if L2 >= 0:
            if L2 == 0:
                print("L2 Zero")
            else:
                print("L2 Positive Number")

        h = pow(L1, 2) + pow(L2, 2)
        print('Value of Hypot', h)

        print('____________________________________________')
    elif L1 < 0:
        print("L1 Negative Number, Please Recheck Input")
    elif L2 < 0:
        print("L2 Negative Number, Please Recheck Input")
Md Sifatul Islam
  • 846
  • 10
  • 28