Trouble with if in python

后端 未结 4 1502
刺人心
刺人心 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:53

    You have to convert the input to an integer by putting it in int:

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

    Remember that raw_input always returns a string. So, in your if-statement, you are comparing a string with integers. That is why it doesn't work.

    Actually, whenever I need to print one message or another, I like to put it on one line (unless of course the messages are long):

    print("Good input" if 50 <= y <= 100 else "Invaild input")
    

    Making a whole if-else block seems a little overkill here.

提交回复
热议问题