簡體   English   中英

在不使用列表的情況下計算最小值、最大值

[英]Calculating min, max without using a list

我正在嘗試解決 Python 教科書中的一個問題:

編寫一個程序,要求用戶輸入他們在賽道上跑的次數,然后使用循環提示他們輸入每圈的圈速。 當循環結束時,程序應該顯示他們最快圈的時間、最慢圈的時間和他們的平均圈時間。

到目前為止,還沒有介紹列表的概念,但我想不出一種方法來計算最小、最大單圈時間而不在​​列表上使用 min(),max()。

這是我的代碼:

num_laps = int(input('Enter number of laps you ran: '))


total_time = 0.0
lap_time_list = []

for lap in range(1,num_laps+1):
    lap_time = float(input('\nEnter the lap time in minutes for each lap from first to last: '))
    total_time += lap_time
    lap_time_list.append(lap_time)


avg_time = total_time / num_laps
fast_lap = min(lap_time_list)
slowest_lap = max(lap_time_list)

# Display the time of fastest lap, slowest lap and average lap time

print('\nAverage lap time:',avg_time,'mins')
print('\nFastest lap time:',fast_lap,'mins')
print('\nSlowest lap time:',slowest_lap,'mins')

好吧,我認為您實際上可以相當簡單地做到這一點,無需列表。 只需跟蹤迄今為止看到的最大和最小數字.. 對嗎? 不確定這是否有效,但一個簡單的例子是:

num_laps = int(input('Enter number of laps you ran: '))

slowest, fastest, avg = 0, 1000, 0 # fastest just needs to be a very large number usually like a int.big

for lap in range(num_laps):
    lap_time = int(input('\nEnter the lap time in minutes for each lap from first to last: ')) # assumes round numbers
    if lap_time > slowest:
        slowest = lap_time
    if lap_time < fastest:
        fastest = lap_time
    avg += lap_time

avg /= num_laps

某處可能存在錯誤,並且需要處理邊緣情況,但這個想法仍然存在,您應該能夠使用該概念使其正常工作。

您可以考慮使用“緩存和轉儲”方法,檢查當前條目是最快的還是最慢的。 如果是任何一種情況,請將跟蹤變量替換為當前條目值。

total_time = 0

for lap in range(1,num_laps+1):
    current_lap_time = eval(input("what's the lap time?"))

    # if it's the first lap, it will be our starting point
    if lap == 1:
        slowest_time = current_lap_time
        fastest_time = current_lap_time

    #check if the current lap time is the slowest
    if current_lap_time > slowest_time:
        slowest_time = current_lap_time

    # check if the current lap time is the fastest
    if current_lap_time < fastest_time:
        fastest_time = current_lap_time

    # calculate total time
    total_time += current_lap_time

# calculate average time
average_time = total_time / num_laps

這很簡單; 我們可以在不使用列表的情況下解決它; 這是代碼:

    enter Lap_count=int(input('How many laps you have taken'))
    slowest_lap=0.0
    Fastest_lap=0.0
    total=0.0
    for num in range(Lap_count) :
         print('This is the input for lap ', num+1,)
         lap_time = float(input('Enter the lap time:'))
         print('=======================================')
         if num == 0 :
             slowest_lap = lap_time
             Fastest_lap = lap_time
         total += lap_time
         if  slowest_lap > lap_time:
             slowest_lap = lap_time
         if Fastest_lap < lap_time:
             Fastest_lap = lap_time

        print('The fastest lap is:', Fastest_lap)
        print('The slowest lap is:', slowest_lap)
        print('The averages is:', format((total/Lap_count), '.2f'))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM