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?

1

2 Answers 2

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'.

Sign up to request clarification or add additional context in comments.

Comments

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") 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.