2017-02-17 88 views
1

这是我的代码,我想它要经过,并选择它适合于哪一类,但它总是给我F.为什么我的if语句在这个分级代码中不起作用?

import random 

def rand(start, stop): 
    print random.randint(start,stop) 

def grader(rand): 
    print "Your test score is " 
    x = rand(50, 100) 
    if x >= 90: 
    print "which is an A." 
    elif x <= 89 and x >= 80: 
    print "which is a B." 
    elif x <= 79 and x >= 70: 
    print "which is a C." 
    elif x <= 69 and x >=60: 
    print "which is a D." 
    else: 
    print "which is a F." 
+4

'print'不是'return'。 – user2357112

+0

为什么你需要和random.randint()完全相同的函数? – Barmar

+0

我的老师让我把它放在:/ – Hat

回答

0

,而不是返回random.randint(start,stop)的,你打印。

变化

def rand(start, stop): 
    print random.randint(start,stop) 

def rand(start, stop): 
    return random.randint(start,stop) 
+0

OMG THANK YOU !!!!!!我的老师给了我那段代码,所以我从来没有想过要看那里! – Hat

0

rand函数返回None,因为它是印刷的,而不是返回一个值。另外,最好的做法是将其命名为更具描述性的内容,如get_randomget_random_number。另外,您的get_random功能确实与randint的功能完全相同,但我会给您带来疑问的好处(需要添加更多功能?)。

作为奖励,我已经包含了一个例子,知道如何知道bisect库对于这些类型的价值交叉点问题是完美的!

实施例:

import bisect, random 

def get_random(start, stop): 
    return random.randint(start,stop) 

def match_grade(score): 
    breakpoints = [60, 70, 80, 90] 
    grades = ["which is a F.", "which is a D.", 
    "which is a C.", "which is a B.", "which is an A."] 
    bisect_index = bisect.bisect(breakpoints, score) 
    return grades[bisect_index] 

random_number = get_random(50, 100) 
grade_range = match_grade(random_number) 
print "Your test score is {}, {}".format(random_number, grade_range) 

样本输出:

Your test score is 63, which is a D. 
相关问题