2017-12-13 11 views
-4

私は負の数を与える場合、このコードはこれ以上機能しませんし、例えば文字列や意味のないものを置くと、それを止める方法がわかりません。お願い助けて!!私のコードを変更する方法

def computeHCF(x,y): 

     if x>y: 
      smaller = y 
     else: 
      smaller = x 
     for i in range(1, smaller+1): 
      if((x % i == 0) and (y % i == 0)): 
       hcf = i 
     return hcf 

    while True: 
     num1 = int(input("Enter the first number: ")) 
     num2 = int(input("Enter the second number: ")) 
     print("The H.C.F. of", num1,"and", num2,"is", computeHCF(num1, num2)) 
     continue; 
    else: 
     print("You write something that doesn't have any sense!") 
+0

入力の絶対値を最初のステップとして使用するだけではどうですか。また、なぜ 'small = min(x、y)'を 'if ... else'構造ではなく使うのでしょうか?さらに、あなたの押し込みは理にかなっていません。関数定義の一番下に 'while'ループがあるのは間違いです。最後に、 'math.gcd()'は、あなたが計算をより効率的にしようとしているものを計算します。 –

+0

num1とnum2のif条件を書いてください –

+4

"これはもう機能しません" –

答えて

0

入力の文字列を最初にチェックする必要があります。

def computeHCF(x,y): 

    if type(x) is str or type(y) is str: 
     print("You wrote something that doesn't have any sense!") 
     exit() 
    elif x < 0 or y < 0 : 
     print("You wrote something that doesn't have any sense!") 
     exit() 
    elif x>y: 
     smaller = y 
    elif y<x: 
     smaller = x 
    for i in range(1, smaller+1): 
     if((x % i == 0) and (y % i == 0)): 
      hcf = i 
    return hcf 

while True: 
    num1 = (input("Enter the first number: ")) 
    num2 = (input("Enter the second number: ")) 
    print("The H.C.F. of", num1,"and", num2,"is", computeHCF(num1, num2)) 
    continue; 
0

私たちは何をシミュレートすることができます。 は、次に、これらの2つのエラーのいずれも存在しない場合、それはあなたのコード を実行するために、先に行く。しかし、彼らがそのようなエラー を与える前に、それが終了させて​​いる場合には、負の数値のため をチェック結果が期待どおりでないときにコード内で発生します。 負の数を挿入すると、プログラムが停止することに気付きました。 はそうのはNUM1 = -4、あなたは多くの方法でこの問題を解決することができます= 2

def computeHCF(x,y): # x = -4, y = 2 
    if x>y: # we don't meet this condition 
     smaller = y 
    else: # we meet this condition 
     smaller = x # smaller value is -4 
    for i in range(1, smaller+1): # there is nothing in range(1, -3)!! 
     # we do not enter here 
     if((x % i == 0) and (y % i == 0)): 
      hcf = i 
    return hcf # hcf value has never been retrived, so an error will be raised here 

NUM2を想定してみましょう、そのうちの二人は、次のとおりです。ベース値と セットHFC、ループの中に条件があるので、もし満たされていない、ベース値が返される。

('The H.C.F. of', -4, 'and', 2, 'is', None) 

をまたはxとyの絶対値を有すること:

def computeHCF(x,y): 
    hcf = None 

このコードが返され

def computeHCF(x,y): 
    x = abs(x) 
    y = abs(y) 

このコードが返されます:

('The H.C.F. of', -4, 'and', 2, 'is', 2) 

我々はまた、我々はintとして解釈できない文字列か何かを挿入した場合、別のエラーが発生していることがわかります。この2行

num1 = int(input("Enter the first number: ")) 
num2 = int(input("Enter the second number: ")) 

、あなたはintに、そのユーザ入力何かを変換しますが、のような文字列: この時、エラーは、入力を読み込むときに発生の「Hello World!」 intに変換することはできません。 この問題を解決する多くの方法の1つは、try/exceptを使用することです。入力をintとして読み取ろうとしますが、エラーが発生した場合は別の処理を行います。

try: 
    num1 = int(input("Enter the first number: ")) 
    num2 = int(input("Enter the second number: ")) 
    print("The H.C.F. of", num1,"and", num2,"is", computeHCF(num1, num2)) 
    continue 
except: 
    print("You write something that doesn't have any sense!") 
    continue 

このコードでは、入力「Hello」と「World!」の結果が表示されます。次のようになります。あなたも最後には、x = 0またはy = 0 機能computeHCF(x、y)とによって生成されたエラーをキャッチ

"You write something that doesn't have any sense!" 

、あなたは「他に、あなたの最後の2行を消去することができます"声明。これは、whileループの条件がFalseのときにのみ実行されますが、Trueは常にTrueです。 最後に、コードは次のようになります。

def computeHCF(x,y): 
    x = abs(x) 
    y = abs(y) 
    # or hcf = None 
    if any([type(x)!=int,type(y)!=int]): 
     return hcf 
    if x>y: 
     smaller = y 
    else: 
     smaller = x 
    for i in range(1, smaller+1): 
     if((x % i == 0) and (y % i == 0)): 
      hcf = i 
    return hcf 

while True: 
    try: 
     num1 = int(input("Enter the first number: ")) 
     num2 = int(input("Enter the second number: ")) 
     print("The H.C.F. of", num1,"and", num2,"is", computeHCF(num1, num2)) 
    except: 
     print("You write something that doesn't have any sense!") 
    continue 
関連する問題