栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

TypeError: ‘decimal.Decimal‘ object cannot be interpreted as an integer

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

TypeError: ‘decimal.Decimal‘ object cannot be interpreted as an integer

这里直接翻译了一下。。。

TypeError: 'decimal.Decimal'对象不能被解释为一个整数


然后把range()函数中用到的num,直接定义为int类型,就好像OK了?

这算是解决了这个问题了吗?反正运行起来是没报错了,也不知道对不对,笑哭。

print("——拼手气红包——")
from decimal import*
import random
total = input("请输入红包总金额:")
num = input("请输入抢红包人数:")
amount_list = []
total = Decimal(total)
num = Decimal(num)
total = total * 100
total_min = 1
total_min = Decimal(total_min)
re_amount = total
pe = num
print(type(total_min),type(re_amount),type(pe))
for i in range(num - 1):
    pe -= 1
    print(pe)
    amount = random.randint(1,re_amount - (total_min * pe))
    re_amount = re_amount - amount
    print("每一次随机金额:",amount,"n每一次剩余金额:",re_amount)

这是range()函数定义为整形后:

反正运行起来是没报错了。。。。

print("——拼手气红包——")
from decimal import*
import random
total = input("请输入红包总金额:")
num = input("请输入抢红包人数:")
amount_list = []
total = Decimal(total)
num = Decimal(num)
total = total * 100
total_min = 1
total_min = Decimal(total_min)
re_amount = total
pe = num
print(type(total_min),type(re_amount),type(pe))
for i in range(int(num) - 1):
    pe -= 1
    print(pe)
    amount = random.randint(1,re_amount - (total_min * pe))
    re_amount = re_amount - amount
    print("每一次随机金额:",amount,"n每一次剩余金额:",re_amount)

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/300517.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号