2017-09-07 21 views
1

私はかなり新しい初心者です。私は、サブルーチンを使って最大数と最小数、そしてその数の範囲を見つけるコードを書いた。 しかし、テストデータx = 12、y = 6、z = 2は、最大の数字がyであることを示しています。Python。間違った結果

注:変数がどのような選択肢1枚のプリントのOUTPUT1であり、文字列を返す入力()ので、そう、あなたのコード内の

x = input("Enter x:") 
y = input("Enter y:") 
z = input("Enter z:") 
if x>y and x>z : 
    output1 = 'x is the largest' 
    large = x 
elif (y>x and y>z): 
    output1 = 'y is the largest' 
    large = y 
elif (z>x and z>y): 
    output1 = 'z is the largest' 
    large = z 
else : 
    output1 ='all numbers are equal' 
    large = 0 
if x<y and x<z : 
    output2 = 'x is the smallest' 
    small = x 
elif (y<x and y<z): 
    output2 = 'y is the smallest' 
    small = y 
elif (z<x and z<y): 
    output2 = 'z is the smallest' 
    small = z 
else : 
    output2 = 'all numbers are equal' 
    small = 0 
output3 = large-small 
outputq = "Bye" 
print("[1] Find the highest variable.") 
print("[2] Find the lowest variable.") 
print("[3] Find the range between the highest and the lowest variables.") 
print("[q] Quit.") 
while outputq == ('Bye'): 
    choice = input("Enter choice number:") 
    if choice == '1' :print (output1) 
    elif choice == '2' :print (output2) 
    elif choice == '3' :print (output3) 
    elif choice == 'q' : 
     print (outputq) 
     outputq="end" 
    input() 
+2

FYI:「サブルーチン」は一般にPythonでは_functions_と呼ばれます。しかし実際には、あなたのコードには何の関数もありません。 –

+0

私は最低変数/最高変数/ Rangeの3つの異なる目的を意味しました –

+0

https://stackoverflow.com/questions/20449427/how-can-i-read-inputs-as-integers https://stackoverflow.com/questions/4915361/whats-the-difference-raw-input-and-input-in-python3-x –

答えて

1

if x>y and x>z :に、文字列ではなく数値を比較します。 int型に変換します。私は

まず検索最大見つけ

def find_largest(data): 
    x, y, z = data 
    print('\nx = {}, y = {}, z = {}'.format(x,y,z)) 

    a='' 
    if x>y and x>z : 
     a = 'x is the largest' 
    elif (y>z): 
     a = 'y is the largest' 
    else: 
     a = 'z is the largest' 

    return a 

あなたのコードを再構築し、機能に分かれてい

x = int(input("Enter x:")) 
y = int(input("Enter y:")) 
z = int(input("Enter z:")) 
+0

OPがPython 3を使用していることを期待してみましょう。 –

+0

python3 print()のため – ingvar

+0

だと思いましたが、ただ一つの引数で 'print()'しかないので、Python 2でも同じことが起こります。 –

0

最低

def find_lowest(data): 
    x, y, z = data 
    print('\nx = {}, y = {}, z = {}'.format(x,y,z)) 

    b = '' 
    if x<y and x<z : 
     b = 'x is the smallest' 
    elif (y<z): 
     b = 'y is the smallest' 
    else: 
     b = 'z is the smallest' 

    return b 

検索ミッドレンジ

def find_mid(data): 
    return (max(data)-min(data)) 

決勝、コードスニペット

data = [12,6,2] # used list to pass data 
print("[1] Find the highest variable.") 
print("[2] Find the lowest variable.") 
print("[3] Find the range between the highest and the lowest variables.") 
print("[q] Quit.") 
d = '' 
while d != 'Bye': 
    choice = input("Enter choice number:") 
    if choice == '1' : 
     print (find_largest(data)) 
    elif choice == '2' : 
     print (find_lowest(data)) 
    elif choice == '3' : 
     print (find_mid(data)) 
    elif choice == 'q' : 
     d = 'Bye' 
     print(d) 

このリンクリンク

Find Largest/lowest and mid

以下の作業コードを確認してくださいあなたのコードの流れを理解するのに役立ちます。手順を理解するには、[進む]ボタンをクリックします。