python 除数为0_Python中防止程序运行的零除法

该博客讲述了作者在开发一个多项选择测验项目时遇到的问题,当用户未回答任何问题时,由于正确率计算涉及到除以0,导致程序无法运行。作者分享了项目的代码,并寻求解决方案。代码包括生成随机问题、用户交互和统计答题情况的部分,其中`percentage`变量的计算可能会导致零除错误。
摘要由CSDN通过智能技术生成

我还在为我的多项选择测验项目而辛苦工作。我快到了。只有几个虫子还没解决。这个程序会记录你得到的正确的,错误的,以及当前的百分比。但是,我的程序不会运行,因为最初,用户没有回答任何问题。因此,它们的正确率为NULL%。我的代码如下:import random

import sys

import os

import math

right_answer_total = float(0)

wrong_answer_total = float(0)

answer_total = float(right_answer_total + wrong_answer_total)

percentage = 100 * (float(right_answer_total) / float(answer_total))

word_drills = {'class': 'Tell Python to make a new kind of thing.',

'object': 'Two meanings: the most basic kind of thing, and any instance of some thing.',

'instance': 'What you get when you tell Python to create a class.',

'def': 'How you define a function inside a class.',

'self': 'Inside the functions in a class, self is a variable for the instance/

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值