第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會有你想問的

While循環(huán)(Python)

While循環(huán)(Python)

富國滬深 2021-03-21 15:07:56
我有一個(gè)while循環(huán),該循環(huán)返回爬山所需的“奔波”次數(shù)。小山的大小是“斜坡高度”,爬升的高度是“ rush_height_gain”減去“ back_sliding”。以下代碼適用于:ans = num_rushes(15, 10, 5)print(ans)打印1和ans = num_rushes(100, 15,7)print(ans)打印2和ans = num_rushes(10, 10, 9)print(ans)打印12但是返回錯(cuò)誤的答案ans = num_rushes(100, 10, 0)print(ans)應(yīng)該打印10,但是打印9我不確定為什么會這樣,任何幫助將不勝感激def num_rushes(slope_height, rush_height_gain, back_sliding):    current_height = 0    rushes = 0    while current_height < slope_height:        if rush_height_gain == slope_height:            rushes+=1            return rushes        elif current_height < slope_height:            if current_height == slope_height:                return rushes            else:                a = rush_height_gain - back_sliding                current_height += a            if current_height == slope_height:                return rushes            elif current_height > slope_height:                return rushes            else:                rushes+=1    return (rushes)
查看完整描述

4 回答

?
慕斯709654

TA貢獻(xiàn)1840條經(jīng)驗(yàn) 獲得超5個(gè)贊

如果我正確地理解了這個(gè)問題,那么我認(rèn)為您正在尋找的是:


def num_rushes(slope_height, rush_height_gain, back_sliding):

    if rush_height_gain < slope_height and rush_height_gain - back_sliding < 1:

        raise Exception("this is not going to work very well")

    current_height = rushes = 0

    while current_height < slope_height:

        rushes += 1

        current_height += rush_height_gain

        if current_height >= slope_height:

            break

        current_height -= back_sliding

    return rushes

每次上坡“奔波”后,您都要檢查是否已經(jīng)到達(dá)山頂。如果是這樣,那么您就完成了;如果沒有,那么請向下滑動一下再走一次!正如@perreal在他對原始帖子的評論中的鏈接中所指出的那樣,如果您滑倒的次數(shù)多于滑倒的次數(shù),并且第一次沒有完全站起來,那么您將會遇到問題。在這些情況下,您可能想拋出一個(gè)異常。


查看完整回答
反對 回復(fù) 2021-03-30
?
犯罪嫌疑人X

TA貢獻(xiàn)2080條經(jīng)驗(yàn) 獲得超4個(gè)贊

我相信問題是這樣的:


        if current_height == slope_height:

            return rushes

如果back_sliding是0,那么在第十次迭代,current_height從去90到100。然后,支票返回true,并9在遞增之前返回。


查看完整回答
反對 回復(fù) 2021-03-30
?
呼如林

TA貢獻(xiàn)1798條經(jīng)驗(yàn) 獲得超3個(gè)贊

def num_rushes(slope_height, rush_height_gain, back_sliding):


    current_height = 0

    rushes = 0


    while current_height < slope_height:


        current_height += rush_height_gain

        rushes += 1


        if current_height < slope_height:

            current_height -= back_sliding


    return rushes


查看完整回答
反對 回復(fù) 2021-03-30
  • 4 回答
  • 0 關(guān)注
  • 223 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號