Python 为什么我的if语句在此分级代码中不起作用?

Python 为什么我的if语句在此分级代码中不起作用?,python,Python,这是我的代码,我想让它通过并选择它适合的类别,但它总是给我F 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:

这是我的代码,我想让它通过并选择它适合的类别,但它总是给我F

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."
随机导入
def rand(启动、停止):
打印random.randint(开始、停止)
def分级器(兰德):
打印“您的考试成绩为”
x=兰特(50100)
如果x>=90:
打印“哪个是A。”
elif x=80:
打印“哪个是B”
elif x=70:
打印“哪个是C”
elif x=60:
打印“哪个是D”
其他:
打印“哪个是F”

您的
rand
函数正在返回
None
,因为它正在打印值,而不是返回值。此外,好的做法是将其命名为更具描述性的名称,例如
get\u random
get\u random\u number
。另外,您的
get_random
函数做的事情与
randint
做的事情完全相同,但我会告诉您一些疑问(需要添加更多功能?)

作为奖励,我加入了一个例子,说明鲜为人知的
对分
库是如何完美地解决这类值相交问题的

示例:

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.
样本输出:

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.

print
不是
return
。为什么您需要自己的函数来执行与
random.randint()完全相同的操作?
?我的老师让我把它放在:/,