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

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

反向猜數(shù)游戲 Python

反向猜數(shù)游戲 Python

犯罪嫌疑人X 2023-12-29 16:58:35
所以,最近我一直在嘗試編寫一個反向猜數(shù)游戲,讓計算機嘗試猜測我想到的數(shù)字。我應該得到的輸出如下所示:Enter the range: 6Think of a random number between 1 and 6 and press enter once done!Is it smaller than 4, 'y' or 'n'?yIs it smaller than 2, 'y' or 'n'?nIs it smaller than 3, 'y' or 'n'?yWonderful it took me 3 questions to find out that you had the number 2 in mind!到目前為止,這些是我的代碼:import randommaxNum = int(input('Enter the range: '))input('Think of a random number between 1 and ' + str(maxNum) + ' and press enter once done!')lowBound = 1highBound = maxNumresponse = ''noOfGuesses = 0numberHasBeenGuessed = FalserandomNumber = random.randint(lowBound,highBound)while not numberHasBeenGuessed:    noOfGuesses += 1    response = input("Is it smaller than " + str(randomNumber) + ", 'y' or 'n'?")    if response == "n" or response == 'N':        lowBound = randomNumber + 1           randomNumber = random.randint(lowBound,highBound)    elif response == "y" or response == "Y":        highBound = randomNumber - 1        randomNumber = random.randint(lowBound,highBound)    else:        print ('Please only type y, Y, n or N as responses')        numberHasBeenGuessed = True        print('Wonderful it took me ' + str(noOfGuesses) + ' attempts to guess that you had the number ' + str(randomNumber) + ' in mind')主要算法正在工作,但不知怎的,當數(shù)字被“猜測”時它無法檢測到它......有人知道為什么嗎?我將非常感謝您的幫助:)
查看完整描述

2 回答

?
斯蒂芬大帝

TA貢獻1827條經(jīng)驗 獲得超8個贊

這是一個使用的版本try/except


import random


maxNum = int(input('Enter the range: '))

input('Think of a random number between 1 and ' + str(maxNum) + ' and press enter once done!')

lowBound = 1

highBound = maxNum

response = ''

noOfGuesses = 0

numberHasBeenGuessed = False

randomNumber = random.randint(lowBound,highBound)


while not numberHasBeenGuessed:

        noOfGuesses += 1

        response = input("Is it smaller than " + str(randomNumber) + ", 'y', 'n', or 'Thats it'?")

        if response == "n" or response == 'N':

            lowBound = randomNumber + 1

            

            try:

                randomNumber = random.randint(lowBound,highBound)

            except ValueError:

                numberHasBeenGuessed = True

        elif response == "y" or response == "Y":

            highBound = randomNumber - 1


            try:

                randomNumber = random.randint(lowBound,highBound)

            except ValueError:

                numberHasBeenGuessed = True

        else:

            print ('Please only type y, Y, n or N as responses')

        

           

print('Wonderful it took me ' + str(noOfGuesses) + ' attempts to guess that you had the number ' + str(randomNumber) + ' in mind')

當它遇到 a 時ValueError,它會認為它找到了您的號碼。請確保沒有其他情況可能導致此代碼給出錯誤答案,因為我顯然還沒有對其進行徹底測試。


您還可以將這些try/except子句放在 a 中def以使其更加緊湊,因為它們是相同的代碼片段,但我不確定您的項目是否允許這樣做,所以我保留了它。


查看完整回答
反對 回復 2023-12-29
?
拉莫斯之舞

TA貢獻1820條經(jīng)驗 獲得超10個贊

你在numberHasBeenGuessed = Truewhile 循環(huán)之外 - 這意味著在循環(huán)結束之前它不能被調(diào)用 - 所以它將永遠卡住。


當它達到你的猜測時 - 假設我有數(shù)字 7。你的程序會詢問“7 比 7 小還是大?” 顯然這是沒有意義的,7就是7。7 不小于也不大于 7:你的程序知道這一點并崩潰。所以你需要引入一個新的用戶輸入選項:


    import random

    

    maxNum = int(input('Enter the range: '))

    input('Think of a random number between 1 and ' + str(maxNum) + ' and press enter once done!')

    lowBound = 1

    highBound = maxNum

    response = ''

    noOfGuesses = 0

    numberHasBeenGuessed = False

    randomNumber = random.randint(lowBound,highBound)

    

    while not numberHasBeenGuessed:

        noOfGuesses += 1

        response = input("Is it smaller than " + str(randomNumber) + ", 'y', 'n', or 'Thats it'?")

        if response == "n" or response == 'N':

            lowBound = randomNumber + 1   

            randomNumber = random.randint(lowBound,highBound)

            print(randomNumber)

        elif response == "y" or response == "Y":

            highBound = randomNumber - 1

            randomNumber = random.randint(lowBound,highBound)

            print(randomNumber)

        elif response == "Thats it": #New input option 'Thats it'

            numberHasBeenGuessed = True #Stops the while loop

        else:

            print ('Please only type y, Y, n or N as responses')

               

    print('Wonderful it took me ' + str(noOfGuesses) + ' attempts to guess that you had the number ' + str(randomNumber) + ' in mind')

現(xiàn)在,當您的數(shù)字被猜到時,您可以輸入“就是這樣”,程序就會以您想要的輸出結束。(當然,將輸入和內(nèi)容更改為您想要的)


最后的程序員提示:用主題標簽評論你的代碼,這樣你就知道(以及其他人的幫助)每個部分的作用。


查看完整回答
反對 回復 2023-12-29
  • 2 回答
  • 0 關注
  • 269 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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