亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

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

如何生成隨機數并將它們與令牌輸入進行比較?

如何生成隨機數并將它們與令牌輸入進行比較?

鳳凰求蠱 2022-12-14 21:14:33
所以我試圖創建一個函數 def number_guess(num): 以生成一些隨機整數并將它們與某些 inputs() 進行比較并打印一些語句。例如,如果我輸入:32 45 48 80我的目標輸出是32 is too low. Random number was 80.45 is too high. Random number was 30.48 is correct!80 is too low. Random number was 97.我們還使用了種子值 900,這將導致計算機每次運行程序時都選擇相同的隨機數。到目前為止我的代碼是:# TODO: Import the random moduleimport randomdef number_guess(num):    # TODO: Get a random number between 1-100    random.randint(1,100)    # TODO: Read numbers and compare to random number    for token in tokens:        if token < randint:            print('{} is too low. Random number was {}.'.format(user_input[0], randint))        elif token > randint:            print('{} is too high. Random number was {}.'.format(user_input[1], randint))        elif token == randint:            print('{} is correct!'.format(randint))if __name__ == "__main__":    # Use the seed 900 to get the same pseudo random numbers every time    random.seed(900)    # Convert the string tokens into integers    user_input = input()    tokens = user_input.split()    for token in tokens:        num = int(token)        number_guess(num)我試過: def number_guess(num): # TODO: 獲取 1-100 之間的隨機數    randint = ['']    random.randint(1,100)    # TODO: Read numbers and compare to random number    for num in tokens:        if token < randint:            print('{} is too low. Random number was {}.'.format(num[0], randint))        elif token > randint:            print('{} is too high. Random number was {}.'.format(num[1], randint))        elif token == randint:            print('{} is correct!'.format(randint))但我并不真正理解格式以及它的功能應該如何工作。任何幫助表示贊賞!
查看完整描述

4 回答

?
呼喚遠方

TA貢獻1856條經驗 獲得超11個贊

你應該替換這個:

randint = ['']
random.randint(1,100)

有了這個:

randint = random.randint(1,100)


查看完整回答
反對 回復 2022-12-14
?
大話西游666

TA貢獻1817條經驗 獲得超14個贊

您需要將random.randint()呼叫存儲到內存中。

使用類似 的變量randint = random.randint(),因為它現在可以在您的代碼中使用。


查看完整回答
反對 回復 2022-12-14
?
慕仙森

TA貢獻1827條經驗 獲得超8個贊

您的代碼有幾個問題,其中最主要的是您沒有對生成的隨機數做任何事情,并嘗試通過 name 訪問它randint,這會導致“未定義的名稱”異常。


你的代碼的一個稍微簡化的版本,也可以工作,看起來像這樣:


import random


tokens = [32, 45, 48, 80]


def number_guess():

    secret = random.randint(1,100)

    for token in tokens:

        if token < secret:

            print('{} is too low. Random number was {}.'.format(token, secret))

        elif token > secret:

            print('{} is too high. Random number was {}.'.format(token, secret))

        elif token == secret:

            print('{} is correct!'.format(secret))


if __name__ == "__main__":

    # Use the seed 900 to get the same pseudo random numbers every time

    random.seed(900)

    number_guess()

我刪除了用戶輸入部分、不相關的參數和一個無關的循環,現在你會得到所有測試令牌的反饋:


32 is too low. Random number was 80.

45 is too low. Random number was 80.

48 is too low. Random number was 80.

80 is correct!


查看完整回答
反對 回復 2022-12-14
?
慕桂英3389331

TA貢獻2036條經驗 獲得超8個贊

這是對我有用的代碼


# TODO: Import the random module

import random


def number_guess(num):

    # TODO: Get a random number between 1-100

    rand_num = random.randint(1,100)

    # TODO: Read numbers and compare to random number

    if num < rand_num:

        print('{} is too low. Random number was {}.'.format(num, rand_num))

    elif num > rand_num:

        print('{} is too high. Random number was {}.'.format(num, rand_num))

    else:

        print(rand_num,"is correct!")

        

if __name__ == "__main__":

    # Use the seed 900 to get the same pseudo random numbers every time

    random.seed(900)

    

    # Convert the string tokens into integers

    user_input = input()

    tokens = user_input.split()

    for token in tokens:

        num = int(token)

        number_guess(num)


查看完整回答
反對 回復 2022-12-14
  • 4 回答
  • 0 關注
  • 133 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號