2016-10-12 3 views
0

저는 파이썬을 처음 사용하고 'n'또는 'N'을 입력하면 테이블에 정보를 인쇄하는 데 문제가 있습니다. 코드는 다음과 같습니다. 나는 아마 간단한 것을 놓치고 있지만 그것을 알아낼 수는 없습니다. 어떤 도움을 주셔서 감사합니다.Python 3 초보자 용 - 테이블에 인쇄 할 결과 얻기

결과는 다음과 같습니다. 보시다시피 N이 인쇄 된 후에도 속도 입력을 계속 요청합니다 :

속도를 MPH.50으로 입력하십시오. 시간을 입력하십시오 .3 입력 할 계산이 더 있습니까? (yes는 y를 입력하고 no는 n을 입력하십시오.) y 속도를 MPH.60으로 입력하십시오. 시간을 입력하십시오 .4 입력 할 계산이 더 있습니까? y를 yes로 입력하거나 no를 no로 입력하십시오. y MPH.75로 속도 입력 시간 이동 시간을 입력하십시오 .3 입력 할 계산이 더 있습니까? (yes는 y를 입력하고 no는 N을 입력하십시오.) n MPH로 속도를 입력하십시오.

'#이 프로그램은 차량이 이동 한 거리를 속도 (mph)와 주행 한 시간을 사용하여 마일로 계산합니다 (#).

# Create a variable to represent the maxium travel time in hours. 
max_travel = 9 
min_travel = 1 
# Create a variable to represent the maximum speed. 
max_speed = 120 
# Create a variable to represent the minimum speed. 
min_speed = 1 
#Define a variable to represent continuation of the program 
another = ["y", "Y"] 
# Create a variable for saved results to be printed in table 
results = [] 

# main module 
def main(): 

    # Get the speed in MPH. 
    speed = int(input('Enter the speed in MPH.')) 

    # Validate that the speed entered is not out of range. 
    while speed > max_speed or speed < min_speed: 
     if speed > max_speed: 
      print('ERROR: the speed entered must be a lower number between 1 and 120.') 
      speed = int(input('Enter a lower speed in MPH.')) 

     if speed < min_speed: 
      print('ERROR: the speed entered must be a higher number between 1 and 120.') 
      speed = int(input('Enter a higher speed in MPH.')) 

    # Ask user to input travel time in hours 
    travel_time = int(input('Enter the time travelled in hours.')) 

    # Validate that the time travelled is within the range of 1 to 9 hours. 
    while travel_time > max_travel or travel_time < min_travel: 
     if travel_time > max_travel: 
      print('ERROR: the time must be a lower number between 1 and 9.') 
      travel_time = int(input('Enter a different time travelled in hours.')) 

    # Validate that the time travelled is within the range of 1 to 9 hours. 
     if travel_time < min_travel: 
      print('ERROR: the time must be a higher number between 1 and 9.') 
      travel_time = int(input('Enter a different time travelled in hours.')) 

    # This will cause the loop, with the exception of the first loop, 
    # to depend on a 'y' or 'Y' entry to enter any additional entries. 
    first = False 

    # Calculate again? 
    another = input('Do you have another calculation to enter? ' + \ 
      '(Enter y for yes or N for no:)') 

    # This loop will continue until something other 
    # than 'y' or 'Y' is entered when prompted. 
    while another == 'y' or 'Y': 
     main() 

    # Calculations saved for table 
    input_tuple =speed, travel_time 

    # Print the column headings in a table. 
    # Print the time travelled and the 
    # result of the distance calculation 
    print() 
    print('Hours\t\tDistance') 
    print('---------------------') 

    # Print saved entries and calculations in table 
    if another !='y' or 'Y': 
     for input_tuple in results: 

    # Calculate the distance travelled 
    distance = speed * travel_time 
    print("'%s'\t\t'%s'" %(travel_time, distance)) 

# Call the main function. 
def main(): 
""" 
"""' 
+0

들여 쓰기로 평가해야한다고 말했다 가졌어요. 더 이상은 없어. 아니야. 탭을 사용하지 마십시오. – MattDMo

답변

0

코드에는 많은 들여 쓰기 오류가 있습니다. 당신이 적어도

while another == 'y' or 'Y' 

올바른

while another == 'y' or another == 'Y' 

에 실제로 or 'Y' 항상 단지 4 공백 True

+0

정말 고마워요! 나는 모든 것을 던질 수있는 작은 뉘앙스를 모두 잡으려고 아직도 노력하고 있습니다. 나는 그것이 연습으로 더 좋아 지리라고 상상한다! –