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

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

初學(xué)者python游戲:擲骰子,while循環(huán)問題

初學(xué)者python游戲:擲骰子,while循環(huán)問題

回首憶惘然 2022-10-06 19:12:23
我是編程新手,我的任務(wù)是制作一個(gè)擲骰子游戲。游戲繼續(xù)進(jìn)行,直到您在被要求再次玩時(shí)回答“否”。一旦你輸入“否”,它應(yīng)該顯示你贏了和輸了多少次。它工作得很好(不記分):import randomdef main():    playGame = input("Would you like to play Craps? (Enter yes or no): ")    while playGame == 'yes':        roll = input("Press Enter to roll the dice")        rollDice1 = random.randint(1, 6)        rollDice2 = random.randint(1, 6)        print("You got a", rollDice1, "and a", rollDice2)        rolledDice = rollDice1 + rollDice2        print("you rolled a", rolledDice)        if rolledDice == 7 or rolledDice == 11:            print("IT'S YOUR LUCKY DAY! YOU WIN!")        elif rolledDice == 2 or rolledDice == 3 or rolledDice == 12:            print("YOU LOSE! BETTER LUCK NEXT TIME!")        else:            print("YOU NEITHER WIN NOR LOSE!")        playGame = input("Try again? (Enter yes or no): ")        if playGame == "no":            print("Place holder")main()當(dāng)我試圖保持得分時(shí),當(dāng)你輸贏時(shí)它不會(huì)循環(huán)。(雖然當(dāng)你不贏也不輸時(shí)它仍然循環(huán)):import randomdef main():    wins = 0    losses = 0    playGame = input("Would you like to play Craps? (Enter yes or no): ")    while playGame == 'yes':        roll = input("Press Enter to roll the dice")        rollDice1 = random.randint(1, 6)        rollDice2 = random.randint(1, 6)        print("You got a", rollDice1, "and a", rollDice2)        rolledDice = rollDice1 + rollDice2        print("you rolled a", rolledDice)        if rolledDice == 7 or rolledDice == 11:            print("IT'S YOUR LUCKY DAY! YOU WIN!")            wins = wins + 1            return wins        elif rolledDice == 2 or rolledDice == 3 or rolledDice == 12:            print("YOU LOSE! BETTER LUCK NEXT TIME!")            losses = losses + 1            return losses        else:            print("YOU NEITHER WIN NOR LOSE!")        playGame = input("Try again? (Enter yes or no): ")        if playGame == "no":            print("Wins: ", wins)            print("Losses: ", losses)main()我感謝提供的任何幫助和建議。就像我說的我是新手,所以請(qǐng)嘗試以簡(jiǎn)單的方式解釋什么是錯(cuò)的以及我應(yīng)該做什么。
查看完整描述

1 回答

?
慕桂英3389331

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

如果您return從函數(shù)內(nèi)部獲得某些東西,則將其保留-這就是您的 while 循環(huán)不起作用的原因:

def main():

    wins = 0

    losses = 0


    playGame = input("Would you like to play Craps? (Enter yes or no): ")

    while playGame == 'yes':

        roll = input("Press Enter to roll the dice")

        rollDice1 = random.randint(1, 6)

        rollDice2 = random.randint(1, 6)

        print("You got a", rollDice1, "and a", rollDice2)

        rolledDice = rollDice1 + rollDice2

        print("you rolled a", rolledDice)

        if rolledDice == 7 or rolledDice == 11:

            print("IT'S YOUR LUCKY DAY! YOU WIN!")

            wins = wins + 1

            return wins   # EXITS main() - simply delete this row


        elif rolledDice == 2 or rolledDice == 3 or rolledDice == 12:

            print("YOU LOSE! BETTER LUCK NEXT TIME!")

            losses = losses + 1

            return losses # EXITS main() - simply delete this row


        else:

            print("YOU NEITHER WIN NOR LOSE!")


        playGame = input("Try again? (Enter yes or no): ")

        if playGame == "no":

            print("Wins: ", wins)

            print("Losses: ", losses)

             return  # add this to exit the function (could use break as well)

main()


查看完整回答
反對(duì) 回復(fù) 2022-10-06
  • 1 回答
  • 0 關(guān)注
  • 175 瀏覽
慕課專欄
更多

添加回答

舉報(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)