Thread: Help!

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    2
    Rep Power
    0

    Help!


    I'm a newbie to Python, and I was trying to make a number guessing game, using Python 3.

    Even if you choose the right number, it says you've lost- why, and how do I fix it? Keep in mind I'm new to programming, so just tell me why, without using too much hi-tech vocabulary which I don't understand!

    Here is my script:

    enter = "Please press Enter to continue..."

    import random

    myName = input ("Hello, what is your name? ")

    print ("Ok, "+myName+", guess a number between 1 and 5")

    number = random.randint(1, 1)

    Guess1 = input(""+myName+"'s first guess is... ")
    if Guess1 == number:
    print ("Well done- "+myName+", you win! Your prize is safe in the arms of this penguin!")
    input (enter)
    sys.exit(0)
    else:
    print ("Sorry, try again!")
    print ("Two more guesses...")
    Guess2 = input (""+myName+"'s second guess is... ")
    if Guess2 == number:
    print ("Well done- "+myName+", you win! Your prize is safe in the arms of this penguin")
    input (enter)
    sys.exit(0)
    else:
    print ("Sorry, try again!")
    print ("Last guess- choose wisely!")
    Guess3 = input (""+myName+"'s final guess is... ")
    if Guess3 == number:
    print ("Well done- "+myName+", you win! Your prize is safe in the arms of this penguin")
    else:
    print ("Sorry, you lose- maybe next time...")
    number = str(number)
    print ("The number I chose was "+number+"")
    input (enter)
    sys.exit(0)



    Please help-

    brindapari1
  2. #2
  3. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,600
    Rep Power
    1906
    input return a text value, whereas random return a numeric/integer value.

    When you compare two values, you have to make sure both variables are the same type.
    This mean you will have to convert the input text to integer:
    Code:
    Guess1 = int(input(""+myName+"'s first guess is... "))
    I suggest you read up about Built-in Types, especially the part about Numeric Types
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    2
    Rep Power
    0
    Thanks! It worked!

IMN logo majestic logo threadwatch logo seochat tools logo