Python整数到字母等级问题 [英] Python integer to letter grade issue

查看:141
本文介绍了Python整数到字母等级问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试解决这段代码中的错误:

I am attempting to troubleshoot the errors in this piece of code:

import time

while1 = True

def grader (z):
    if   z >= 0 or z <= 59:
        return "F"
    elif z >= 60 or z <= 62:
        return "D-"
    elif z >= 62 or z <= 66:
        return "D"
    elif z >= 67 or z <= 69:
        return "D+"
    elif z >= 70 or z <= 62:
        return "C-"
    elif z >= 73 or z <= 76:
        return "C"
    elif z >= 77 or z <= 79:
        return "C+"   
    elif z >= 80 or z <= 82:
        return "B-"
    elif z >= 83 or z <= 86:
        return "B"
    elif z >= 87 or z <= 89:
        return "B+"
    elif z >= 90 or z <= 92:
        return "A-"
    else:
        return "A"



while while1:
    z = int(input("I will tell you the grade of this number, enter from 1 - 100\n"))
    if z < 0 or z > 100:
        print "Between 1 and 100 PLEASE!\n"
        while1 = True
    print grader(z)
    print "New number now\n"
    time.sleep(100)
    while1 = True

这种情况下的论点是整数 z z 的值由用户设置,然后该函数应该摆入并确定哪个字母等级 z 值得,无论如何它总是返回'F。'

The argument in this situation is the integer z. z's value is set by the user and then the function should swing in and determine what letter grade z is worth, no matter what though it always returns 'F.'

这对我来说相当迷惑(我是新手)我可以使用一些帮助。

This is rather befuddling to me (I am a novice) and I could use some assistance.

推荐答案

你的问题是:

if   z >= 0 or z <= 59:

使用:

if 0 <= z <= 59:

这可以缓解你使用代替的问题,而且更多可读。

This alleviates the problem you're having using or instead of and and is more readable.

但你应该看一下 bisect 模块:

>>> def grade(score, breakpoints=[60, 70, 80, 90], grades='FDCBA'):
        i = bisect(breakpoints, score)
        return grades[i]

>>> [grade(score) for score in [33, 99, 77, 70, 89, 90, 100]]
['F', 'A', 'C', 'C', 'B', 'A', 'A']

这篇关于Python整数到字母等级问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆