python - 如何在 Python 中使用 round() 函数?
问题描述
我想知道如何使用 round() 函数。在代码中,它3.75
为我显示,但我希望它显示4
。
我的代码
import random
def dice():
count = 0
n = input( "Enter # of dice [3, 6]: " )
n = int( n )
diceList = []
for row in range(0, n):
diceList.append(random.randint(1, 6))
count += 1
print( "\nYou have rolled: ",diceList)
total=sum(diceList)
count = round()
print("The sum is", total, "and the average rounded value is", total / count)
dice()
我希望它看起来如何
Enter # of dice [3, 6]: 4
You have rolled: [3, 4, 5, 6]
The sum is 15, and the average rounded value is 4.
解决方案
目前,您有一个TypeError
因为没有参数调用轮。round 需要 1 个 arg,这是要四舍五入的值。
您应该将函数的最后两行更改为:
average = round(total/count)
print("The sum is", total, "and the average rounded value is", average)
推荐阅读
- r - 从 emmeans 中的成对对比中提取概率
- html - 有没有办法将元素锁定在其容器的左右边缘,使其伸展以填充该容器?
- scala - 为什么scala允许从recoverWith返回
- php - PHP - 以 15 为增量计数,但最大为 45
- sql - Postgres中多个数组的平均值
- php - Laravel 5.5 使用除 ENV 之外的其他电子邮件设置发送电子邮件
- java - LinkedList 中的节点(整数索引)
- php - WooCommerce - 在结帐时显示最终运输选择
- mocking - Android espresso 匕首测试。在构造函数注入中模拟
- reactjs - 反应?有什么替代方法可以处理数据