2014-03-25 2 views
-2

저는 시간 1을 0.0으로 계산하는 대신 첫 번째 '카운터'를 읽으려고합니다. 처음 시간에 대신 속도를 계산해야합니다.논리 오류 - 1에서 시작하지 않고 2에서 시작하지 않으려면 계산이 필요합니다.

내가 의미하는 바를 설명 할 수 없다면 코드를 실행하십시오. 1에서 (긴 목록을 인쇄하므로) 두 번째 숫자는 0.0입니다. MPH를 20으로, 시간을 15로 입력하십시오. 그것이 인쇄 될 때 20.0은 숫자 2가 될 때까지 오지 않습니다. 숫자 1에서 팝업이 필요합니다. 이것이 가장 기본적인 질문이되어야한다는 것을 알고 있지만, 나는 알아 내지 못합니다 :/

OUTPUT :

1 0.0 

2 20.0 

3 40.0 

4 60.0 

5 80.0 

6 100.0 

7 120.0 

8 140.0 

9 160.0 

10 180.0 

11 200.0 

12 220.0 

13 240.0 

14 260.0 

15 280.0 

Distance traveled by Smith was 300 miles. 

MY CODE :

def main(): 

    greet() 

    name = input("Please enter your name: ") 
    print() 

    speed = float(input("Please enter, in miles per hour, your speed (Between 20-500 MPH): ")) 

    while speed < 20 or speed > 500: 
     print("You must enter a speed between 20-500 MPH") 
     print() 
     speed = float(input("Please try again: ")) 
     print() 

    print("That speed is accepted.") 
    print() 

    time = int(input("Please enter, in hours, your travel time (Between 2-15 hours): "))  

    while time < 2 or time > 15: 
     print("You must enter a time between 2-15 hours.") 
     print() 
     time = int(input("Please try again: ")) 
     print() 

    print("That time is accepted.") 
    print() 

    totaldistance = speed * time 
    totaldistance = int(totaldistance) 

    for count in range (time): 
     distance = speed * count 
     print_data(name, distance, count, time) 

    print() 
    print("Distance traveled by", name,"was", totaldistance,"miles.") 

def greet(): 
    print() 
    print("This program calculates distance traveled.") 
    print() 

def print_data (name, distance, count, time): 
    print() 
    print(format(count + 1), " ", format(distance)) 

main() 

답변

0

내 오프셋 print_datacount 내부에 1을 가산에서 발생하지만, 거리를 산출하지 않는 경우.

이 시도 :

for count in range (1, time): 
    distance = speed * count 
    print_data(distance, count) 

함께

def print_data (distance, count): 
    print() 
    print(count, distance) 
+0

print(format(1), " ", format(0)) 

있을 것이기 때문에 줄 (1 0.0) 그러나 문제를 해결하지 못했습니다. 2는 여전히 (2 20.0)이었다. 이것을 더 잘 설명하기 위해 나는 무언가를 0으로 곱하고 있다고 생각하지만, 내가 그랬던 것을 볼 수는 없다. – user3399963

+0

@ user3399963 그런 다음 루프 만 변경했으나 print_data 함수는 변경하지 않았다. 자기 자신을 은밀하게 증식시키는 것은 아무것도 없다. 이것은 정말로 101을 프로그래밍하고 있습니다 ... – Hyperboreus

0
for count in range (time): 
    distance = speed * count 
    print_data(name, distance, count, time) 

으로이 인쇄물 인 경우에, 당신은 0으로 계산의 첫 번째 값부터 시작되어, 그 범위를 시작하기 때문에 . 내가 '범위'내 첫 번째 탈락에 포함 1이 반응을했을 때 카운트 = 0, 다음 속도 * 0 = 0과 print_data은 항상 첫 번째 출력

관련 문제