Trouble with if in python

后端 未结 4 1500
刺人心
刺人心 2021-01-24 01:36

Hey just starting to learn python and got some problems already I made a code with an if statement It should work but it isn\'t working can someone fix it and tell me what I did

4条回答
  •  自闭症患者
    2021-01-24 01:49

    You have convert it to an int. raw_input gives you a string.

    x= raw_input("Enter your name: ")
    y= raw_input("Enter your grade: ")
    print(y)
    if 50 <= int(y) <= 100:
        print("Good input")
    else:
         print ("Invaild input")
    

    But what if the user does not enter a number, then you need to add a Try-Except block:

    x = raw_input("Enter your name: ")
    y = raw_input("Enter your grade: ")
    print(y)
    
    try:
        if 50 <= int(y) <= 100:
            print("Good input")
        else:
            print ("Invaild input")
    except ValueError:
        print "You did not enter a number for your grade"
    

提交回复
热议问题