ZeroDivisionError: division by zero. What needs to be changed

huangapple go评论58阅读模式
英文:

ZeroDivisionError: division by zero. What needs to be changed

问题

def get_success_rate(statistics: str) -> int:
    count = 0
    for i in statistics:
        if statistics == '':
            return 0
        if i == '1':
            count += 1
    res = count / len(statistics) * 100
    return round(res)

print(get_success_rate("11100"))  # 60
print(get_success_rate("1100"))  # 50
print(get_success_rate("000000"))  # 0
print(get_success_rate("11111"))  # 100
print(get_success_rate(""))  # 0
60
50
0
100
Traceback (most recent call last):
  File "D:\Program Files\Python\CHERHOBNK\Folder - 1\draft.py", line 807, in <module>
    print(get_success_rate(""))  # 0
  File "D:\Program Files\Python\CHERHOBNK\Folder - 1\draft.py", line 799, in get_success_rate
    res = count / len(statistics) * 100
ZeroDivisionError: division by zero

<details>
<summary>英文:</summary>


def get_success_rate(statistics: str) -> int:
count = 0
for i in statistics:
if statistics == '':
return 0
if i == '1':
count += 1
res = count / len(statistics) * 100
return round(res)

print(get_success_rate("11100")) # 60
print(get_success_rate("1100")) # 50
print(get_success_rate("000000")) # 0
print(get_success_rate("11111")) # 100
print(get_success_rate("")) # 0


    60
    50
    0
    100
    Traceback (most recent call last):
      File &quot;D:\Program Files\Python\CHERHOBNK\Folder - 1\draft.py&quot;, line 807, in &lt;module&gt;
        print(get_success_rate(&quot;&quot;))  # 0
      File &quot;D:\Program Files\Python\CHERHOBNK\Folder - 1\draft.py&quot;, line 799, in get_success_rate
        res = count / len(statistics) * 100
    ZeroDivisionError: division by zero

</details>


# 答案1
**得分**: 2

将空字符串检查移到循环外:

```python
def get_success_rate(statistics: str) -> int:
    if statistics == '':
        return 0
    count = 0
    for i in statistics:
        if i == '1':
            count += 1
    res = count / len(statistics) * 100
    return round(res)

如果字符串为空,循环将不会执行,因此检查实际上将被跳过。

英文:

Move the empty string check outside the loop:

def get_success_rate(statistics: str) -&gt; int:
    if statistics == &#39;&#39;:
        return 0
    count = 0
    for i in statistics:
        if i == &#39;1&#39;:
            count += 1
    res = count / len(statistics) * 100
    return round(res)

If string is empty the loop will not be executed so the check will actually be skipped.

答案2

得分: 1

你函数的逻辑不够严谨。从代码看,你可能希望在迭代for i in statistics之前,先处理if statistics == &#39;&#39;这个条件判断。这样,你的函数就不会再出现除以零的错误。

你当前代码的问题在于,当statistics == &#39;&#39;时,迭代根本不会发生,没有东西可以进行迭代。然后,程序调用res = count / len(statistics) * 100,由于len(&quot;&quot;)等于零,就会导致除以零的错误。

英文:

The logic of your function is not sound. Guessing from the code, you probably want to do the if statistics == &#39;&#39; clause before your iteration, for i in statistics. Then you'll no longer have the division by zero error of your function.

the problem with your current code, is that the iteration doesn't happen when statistics == '', there's nothing to iterate over. And then the call is made to res = count / len(statistics) * 100, and as len(&quot;&quot;) equals zero, that's giving you the division by zero error

huangapple
  • 本文由 发表于 2023年2月19日 05:06:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/75496398.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定