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

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

我將如何循環(huán) python 函數(shù)的特定部分?

我將如何循環(huán) python 函數(shù)的特定部分?

至尊寶的傳說(shuō) 2023-11-09 21:36:51
所以我的問(wèn)題是如何縮短這段代碼并仍然允許 3 次嘗試?def numberguess(answernumber):  guess=input('guess a number between 1-10: ')  if guess.isnumeric():    if guess==answernumber:      print('Correct')    else:      print('Incorrect')      userguess=input('Two more attempts: ')      if userguess.isalpha():        if userguess==answerletter:          print('Correct')        else:          print('Incorrect')          userguess=input('one more attempt: ')          if guess.isalpha():            if userguess==answerletter:              print('Correct')            else:              print('Incorrect, No more attempts remaining')          else:            print('Invalid')      else:        print('Invalid')  else:    print('invalid')我有一組較短的代碼,但我不知道如何允許多次嘗試而不使其變成以前的混亂代碼,我想知道是否有任何方法可以像您那樣進(jìn)行循環(huán)在 python(turtle) 中使用“for i in range:”循環(huán)def letterguess(answerletter,userguess):  answerletter=answerletter.lower()  userguess=userguess.lower()  if userguess.isalpha()==False:    print('Invalid')    return False  elif userguess==answerletter:    print('Correct')    return True  elif userguess>answerletter:    print('guess is too high')    return False  else:    print('guess is too low')    return False如果您想查看差異,這是縮短版本,但此版本僅允許嘗試一次
查看完整描述

3 回答

?
皈依舞

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

您在問(wèn)題標(biāo)題中使用了“循環(huán)”一詞,您是否嘗試過(guò)谷歌搜索并閱讀有關(guān) Python 中可用的循環(huán)結(jié)構(gòu)類型的信息?在您的情況下,您知道您希望循環(huán)運(yùn)行三次,因此您可以使用循環(huán)for。


所以基本結(jié)構(gòu)將如下所示:


def number_guess(answer: int, num_attempts: int = 3) -> None:  # The : int and -> None are call type hints or type annotations. I highly highly recommend getting into the habit of using them in Python. It makes your code easier to read and later on, you can use tools like mypy to catch errors before running your code.

    for attempt in range(num_attempts): # Loop a defined number of times

        guess = input("Guess a number between 1 and 10:")

        if validate_guess(guess, answer):  # Wrap up your conditions in a function for readability

            print("Correct")

            break # Exit the loop early because the condition has been met

        else:

            print("Incorrect")

    else:  # This part is a weird python thing, it only runs if the loop completes without reaching a break statement. What does it mean if your loop completed without hitting break in this case? It means that the condition never evaluated to true, hence the correct guess wasn't found and the user ran out of tries.

        print("Sorry, you're out of tries")

現(xiàn)在您需要定義validate_guess:


def validate_guess(guess: str, answer) -> bool:

    return guess.isnumeric() and int(guess) == answer


查看完整回答
反對(duì) 回復(fù) 2023-11-09
?
千萬(wàn)里不及你

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

緊湊版本可能是:


def numberguess(answerletter):

    for attempt in range(3):

        user_guess = input('Enter Guess:')

        if user_guess.isnumeric() and int(user_guess)==answerletter:

            print('Correct')

            return True

        else:

            print(f'incorrect, {2-attempt} attempts left')

    print('failed')

    return False

你需要做的工作:

  1. 循環(huán)。當(dāng)需要重復(fù)某些操作所需的次數(shù)時(shí)使用它們

  2. 結(jié)合條件。你不能只是將它們列在一個(gè)長(zhǎng)長(zhǎng)的 if-else 梯子中。相反,請(qǐng)使用邏輯來(lái)確定在何處以及如何更有效地使用這些條件。結(jié)果將是更高效、更簡(jiǎn)潔的代碼。


查看完整回答
反對(duì) 回復(fù) 2023-11-09
?
揚(yáng)帆大魚(yú)

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

好吧,呃,出于某種原因,我有一個(gè)奇怪的想法,僅僅因?yàn)樗鼈冇糜?python(turtle) 就意味著我不能對(duì)普通 python 使用 while 循環(huán),這里是使用臨時(shí) while 循環(huán)的修訂后的代碼,它不會(huì)改變太多原始代碼


def letterguess(answerletter):

? answerletter=answerletter.lower()

? i=0

? while i<3:

? ? userguess=input('guess a letter A-Z: ')

? ? userguess=userguess.lower()

? ? if userguess.isalpha()==False:

? ? ? print('Invalid')

? ? ? return False

? ? elif userguess==answerletter:

? ? ? print('Correct')

? ? ? i=4

? ? ? print('You guessed correctly')

? ? elif userguess>answerletter:

? ? ? print('guess is too high')

? ? ? i+=1

? ? else:

? ? ? print('guess is too low')

? ? ? i+=1

? else:

? ? print('you failed to guess')

print(letterguess('L'))

查看完整回答
反對(duì) 回復(fù) 2023-11-09
  • 3 回答
  • 0 關(guān)注
  • 193 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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