簡體   English   中英

如何修復python中的“類型錯誤十進制模塊”

[英]How to fix' type error decimal module' in python

我正在制作一個隨機數生成器,但遇到了編碼錯誤

我曾嘗試修改錯誤並檢查一些解決方案,但它們不起作用

from decimal import Decimal
import random
def Random(num1,num2):
random_list=[random.randint(num1,num2)for i in range(num2)]
random_number=(random.choice(random_list))
randomer_number=random.choice(random_list)
print("The two numbers are",str(random_number),"and",str(randomer_number))

我希望生成隨機數,但存在類型錯誤,其中 'decimal.Decimal' 對象不能解釋為整數

'decimal.Decimal' 對象不能解釋為整數

這里有什么不清楚的? 只需使用range(int(num1))而不是range(num1)

我想你想要類似的東西

from decimal import Decimal
import random


def Random(num1,num2):
    random_list=[random.randint(num1, num2)for i in range(int(num2))]
    random_number=(random.choice(random_list))
    randomer_number=random.choice(random_list)
    return random_number, randomer_number

r1, r2 = Random(Decimal(1), Decimal(100))
print(f"The two numbers are {r1} and {r2}")

它工作沒有任何錯誤

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM