else & elif statements not working in Python

后端 未结 9 1047
礼貌的吻别
礼貌的吻别 2020-12-10 11:06

I\'m a newbie to Python and currently learning Control Flow commands like if, else, etc.

The if statement is working all fine

9条回答
  •  被撕碎了的回忆
    2020-12-10 11:44

    Besides that your indention is wrong. The code wont work. I know you are using python 3. something. I am using python 2.7.3 the code that will actually work for what you trying accomplish is this.

    number = str(23)
    guess =  input('Enter a number: ')
    if guess == number:
       print('Congratulations! You guessed it.')
    elif guess < number:
         print('Wrong Number')
    elif guess < number:
         print("Wrong Number')
    

    The only difference I would tell python that number is a string of character for the code to work. If not is going to think is a Integer. When somebody runs the code they are inputing a string not an integer. There are many ways of changing this code but this is the easy solution I wanted to provide there is another way that I cant think of without making the 23 into a string. Or you could of "23" put quotations or you could of use int() function in the input. that would transform anything they input into and integer a number.

提交回复
热议问题