为什么我在python中的“ if”语句不起作用?

时间:2019-09-27 20:11:42

标签: python python-3.x

我想知道为什么我的“ if”语句不起作用。我被要求提供输入,此后什么也没有发生。它不会打印出来是A,B,C还是D。

def main():
    printLetterGradeFromNumber(input("The grade as a number: "))

def printLetterGradeFromNumber(number_grade):

    A = range(90, 100)
    B = range(80, 89)
    C = range(70, 79)
    D = range(60, 69)

    if number_grade == A:
         print("That student gets an A")
    elif number_grade == B:
        print("That student gets a B")
    elif number_grade == C:
        print("That student gets a C")
    elif number_grade == D:
        print("That student gets a D")

    return number_grade


main()

4 个答案:

答案 0 :(得分:4)

有两个问题:

  • if nums.count == 2 { if nums[slowPointer] == nums[fastPointer] { nums.remove(at: 1) } nums.count } 中的input必须转换为main
    • 当前,intinput,在strTrue,{{1} }或A
    • 示例:BC进行比较,因此D'90' == 90
  • 使用membership operator, in代替comparison operator, ==
False

输出:

'90' in A

替代实现:

  • Don’t Forget NumPy!
  • numpy.digitize
    • False返回def main(): printLetterGradeFromNumber(int(input("The grade as a number: "))) def printLetterGradeFromNumber(number_grade): A = range(90, 100) B = range(80, 89) C = range(70, 79) D = range(60, 69) if number_grade in A: print("That student gets an A") elif number_grade in B: print("That student gets a B") elif number_grade in C: print("That student gets a C") elif number_grade in D: print("That student gets a D") return number_grade main() The grade as a number: 90 That student gets an A 的{​​{1}}。 digitize返回index,而value返回bins
    • 使用value < 60中的index = 0找到了字母等级。因此,value >= 90的顺序必须与index = 4对应。
index

答案 1 :(得分:1)

我希望这就是您要寻找的

def printLetterGradeFromNumber(number_grade):

    #A = range(90, 100)
    #B = range(80, 89)
    #C = range(70, 79)
    #D = range(60, 69)

    if number_grade >= 90 and number_grade <= 100:
         print("That student gets an A")
    if number_grade >= 80 and number_grade <= 89:
         print("That student gets a B")
    elif number_grade >= 70 and number_grade <= 79:
        print("That student gets a C")
    elif number_grade >= 60 and number_grade <= 69:
        print("That student gets a D")

   return number_grade

答案 2 :(得分:0)

range(90,100)等于(90,91,92,93,94,95,96,97,98,99)

您需要使用“ in”运算符

    if number_grade in A:
         print("That student gets an A")
    elif number_grade in B:
        print("That student gets a B")
    elif number_grade in C:
        print("That student gets a C")
    elif number_grade in D:
        print("That student gets a D")

答案 3 :(得分:0)

您无需定义功能。使用“ For”循环。

将成绩设置为A = range(90,101)。 B =范围(80,90),C =范围(70,80),D =范围(60,70)

使用范围时,python会排除上限整数