Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Sep 2014
    Location
    Alaska
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Need very simple coding help (python)

    Very very simple help for noob beginner in python. Trying to make a very simple game of "Guess the magic number." I've set the variable MagicNumber = 3. Then I set the input to the first question to the variable x. I think something went wrong in my if, is, else statement areas. I'm am sorry if this is considered spam or something but i'm trying to learn so help would be much appreciated! I'll attach a photo and a notepad document as well as write the code out here:
    --------------------------------------------------------------------------------------------------------------------------------------
    #Guess the magic number

    MagicNumber = 3
    x = (input("Guess the magic number "))
    if x is MagicNumber: # I'm thinking there is something wrong in this line of code.
    print(MagicNumber, " is the magic number!")
    else:
    print("Sorry wrong number. Try again!")

    '''
    Program starts by asking magic number
    then when user inputs number it will give out the else statement "Sorry wrong number. Try again!"
    even if they guess the right number
    '''
    --------------------------------------------------------------------------------------------------------------------------------------
    Need very simple coding help (python)-magicnumber.png
    Guessthemagicnumber.txt

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,303
    Thanks
    203
    Thanked 2,562 Times in 2,540 Posts
    I don't speak Python, but surely it should be
    if x == MagicNumber:

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    New to the CF scene
    Join Date
    Sep 2014
    Location
    Alaska
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Philip M View Post
    I don't speak Python, but surely it should be
    if x == MagicNumber:
    Still prints the else statement even when input is 3 when change is to ==

  • #4
    New to the CF scene
    Join Date
    Sep 2014
    Location
    Alaska
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Diordna View Post
    Still prints the else statement even when input is 3 when change is to ==
    Just played around with it. I had to bind magic number to the string 3 ('3') in order for it to compare the user input with the magic number. Thanks though! =)

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,596
    Thanks
    80
    Thanked 4,633 Times in 4,595 Posts
    Better than comparing to "3" would be to compare NUMBER TO NUMBER.

    So just do
    Code:
    x = int(input("Guess the magic number "))
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,303
    Thanks
    203
    Thanked 2,562 Times in 2,540 Posts
    Ah - it seems that Python (unlike Javascript) does not do type conversion.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •