2017-09-07 1 views
1

저는 파이썬에 대해 상당히 새로운 초보자입니다. 서브 루틴으로 코드를 작성하여 가장 큰 수와 가장 작은 수 및 그 수 사이의 범위를 찾습니다. 그러나 테스트 데이터 x = 12, y = 6, z = 2는 가장 큰 숫자가 y임을 보여 주지만 테스트 데이터에 대한 대답은 잘못된 것입니다.Python. 잘못된 결과

노트 : 입력() 문자열을 반환하기 때문에 변수가 너무 코드에서

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

최저 변수/최고 변수/범위와 같은 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 :에 무엇을 선택 하나 인쇄 및 위해 출력 1하는 것은 문자열이 아닌 숫자를 비교합니다. 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가 파이썬 3을 사용하기를 희망하지만, 그들이 관찰하고있는 행동을 고려하면 가능성이 높습니다. –

+0

python3 왜냐하면 print()가 – ingvar

+0

일 뿐이라고 생각했는데, 단 하나의 인자로 print()를 사용했기 때문에 파이썬 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

링크

아래에 작업 코드를 확인 니펫을. 단계를 이해하려면 앞으로 버튼을 클릭하십시오.