2017-02-15 81 views
1

如何编写一个程序的Python 3版本,该程序拒绝带有警告的负整数输入并且不允许它进入? 例如,Python 3检查输入的负数

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('____________________________________________') 

我的代码L1和L2的输入之后执行,但不否认的负输入端。请帮助?

+2

[询问用户进行输入,直到它们得到有效的响应]的可能的复制(http://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-给有效的答复) – lmiguelvargasf

+0

http://stackoverflow.com/a/42261523/6840615希望这可以解决您的问题 –

回答

0

您可以使用它来得到一个正数

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

基本上,你不断要求输入用户除非他提供了一个非负数。但是,请记住,如果用户输入的数字不是'fksjfjdskl',则可能会发生异常。

0

你怎么写一个Python 3版本,否认负整数输入提出警告,并不允许它进入一个程序?

你可以简单地给if L1 >= 0 and L2 >= 0:while环后不久,因此没有负数将考虑进行计算。

希望这能为您服务!

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