1. #1
    Will Watts's Avatar
    Join Date
    Mar 2012
    Location
    %tmp%
    Posts
    5,189
    • specs System Specs
      • Manufacturer:
        Alienware M15x
      • Model Number:
        M15x
      • CPU:
        i5 520 M @ 2.40GHz
      • Memory:
        8GB
      • Graphics:
        ATI Radeon HD 5800
      • Hard Drives:
        720GB 7200 rpm
      • Operating System:
        Windows 8 + Ubuntu

    Python - I'm stuck.

    So I was doing some exercises to help me learn python - I had to give it a break during exams, but I'm learning again. I can't get this bit of code to work, there is a logical error in it somewhere which I can't spot.

    The idea is, the user types a number and then the computer tries to guess it. It gets 20 attempts, if it goes through all 20, then the user wins and the game ends - if it guesses the correct number, it's SUPPOSED to break the while loop and tell the user that the computer won. I can't get it to work, all that happens is it goes through all 20 attempts (I've tried 100 attempts for testing to make sure it hits the number without having to run it 10 times) - after it's reached attempt 20 it tells the user that they won - even if it had guessed the number.

    Code:
    # The number guessing game
    #
    # The computer tries to guess your number within 20 tries.
    
    # Random import
    
    import random
    
    # Introduction
    
    print("\tThe Number Guessing Game")
    print("\t--Created by me")
    
    print("\n\nWelcome to the game. To play, pick a number between 1 and 100")
    
    number = input("What is your number? ")
    
    tries = 1
    count = 1
    
    while tries <=20 and count == 1:
        guess = random.randint(1, 100)
        print("\n", guess)
    
        if guess == number:
            print("\n The computer has guessed your number!")
            count += 1
    
        tries += 1
    
    if tries <=20:
        print("\nThe computer has won! It guessed, ", number)
        print("Thanks for playing!")
    
    elif tries >20:
        print("\nCongratulations! You have won! The computer couldn't guess your number.")
    
    input("\n Press Enter to exit the program")
    This always results in the while loop completing, even if the computer hits the right number. I've tried using a break, as below, but it still doesn't work in the above code. What is confusing me most, is that the code below works fine:

    Code:
    import random
    
    tries = 1
    number = 42
    
    while tries <=20:
        guess = random.randint(1, 100)
        print("\n", guess)
    
        if guess == number:
            print("\n The computer has guessed your number!")
            break
        tries += 1
    
    input("")
    If the computer hits 42, the while loop breaks. Have I messed up the user input somehow? I've tried both the count, and break method to break the loop - now I'm stuck .
    Has Sysnative Forums helped you? Please consider donating to help support the forum.



    • Ad Bot

      advertising
      Beep.

        
       

  2. #2
    Will Watts's Avatar
    Join Date
    Mar 2012
    Location
    %tmp%
    Posts
    5,189
    • specs System Specs
      • Manufacturer:
        Alienware M15x
      • Model Number:
        M15x
      • CPU:
        i5 520 M @ 2.40GHz
      • Memory:
        8GB
      • Graphics:
        ATI Radeon HD 5800
      • Hard Drives:
        720GB 7200 rpm
      • Operating System:
        Windows 8 + Ubuntu

    Re: Python - I'm stuck.

    I fixed it . Problem line:

    Code:
    number = input("What is your number? ")
    should be....

    Code:
    number = int(input("What is your number? "))
    I was assigning the user input to be a string, not an integer as intended.... oooops.

    ------------

    Hopefully my next thread here will be to show off some amazing program that's a lot better than this one... or at least a less simple problem.
    Last edited by Will Watts; 05-19-2012 at 07:33 PM.
    Has Sysnative Forums helped you? Please consider donating to help support the forum.


  3. #3
    AceInfinity's Avatar
    Join Date
    Feb 2012
    Location
    Canada
    Posts
    1,725

    Re: Python - I'm stuck.

    I was assigning the user input to be a string, not an integer as intended.... oooops
    Type casting is critical :)
    Automation Programmer
    Microsoft MVP [2012 - 2018]

  4. #4

    Re: Python - I'm stuck.

    Just for the hell of it, I decided to refactor. Works exactly the same, with one of the conditional statements cut out:

    Code:
    # The number guessing game
    #
    # The computer tries to guess your number within 20 tries.
    
    # Random import
    
    import random
    
    # Introduction
    
    print("\tThe Number Guessing Game")
    print("\t--Created by me")
    
    print("\n\nWelcome to the game. To play, pick a number between 1 and 100")
    
    number = int(input("What is your number? "))
    
    tries = 1
    guessed = False
    
    while guessed == False and tries <=20:
        guess = random.randint(1, 100)
        
        if guess == number:
            print("\n The computer has guessed your number! It is", guess)      
            guessed = True        
            
        tries += 1
    
    if tries >20:
        print("\nCongratulations! You have won! The computer couldn't guess your number.")
    
    input("\nThanks for playing! Press Enter to exit the program")

  5. #5
    Will Watts's Avatar
    Join Date
    Mar 2012
    Location
    %tmp%
    Posts
    5,189
    • specs System Specs
      • Manufacturer:
        Alienware M15x
      • Model Number:
        M15x
      • CPU:
        i5 520 M @ 2.40GHz
      • Memory:
        8GB
      • Graphics:
        ATI Radeon HD 5800
      • Hard Drives:
        720GB 7200 rpm
      • Operating System:
        Windows 8 + Ubuntu

    Re: Python - I'm stuck.

    That is neater, thanks . I now have a new problem (see my new thread), but another question here.

    At this point: number = int(input("What is your number? "))

    How can I make this more foolproof, so that if the user mistypes or doesn't enter a number it won't generate errors? I want this to happen:

    "What is your number?"
    cheese
    "Please enter a number. What is your number?"
    cheese
    "Please enter a number. What is your number?"
    ... etc
    Has Sysnative Forums helped you? Please consider donating to help support the forum.


  6. #6

    Re: Python - I'm stuck.

    Quote Originally Posted by someguy201 View Post
    At this point: number = int(input("What is your number? "))

    How can I make this more foolproof, so that if the user mistypes or doesn't enter a number it won't generate errors? I want this to happen:

    "What is your number?"
    cheese
    "Please enter a number. What is your number?"
    cheese
    "Please enter a number. What is your number?"
    ... etc
    Looks like you had the right idea, in your code in the other thread:
    Code:
        while response not in range(low, high):
            response = int(input(question))

Similar Threads

  1. HiLo Game - Python
    By AceInfinity in forum Programming
    Replies: 2
    Last Post: 02-06-2013, 01:24 AM
  2. Python - Will's Amazing Menu Program
    By Will Watts in forum Programming
    Replies: 10
    Last Post: 07-04-2012, 02:22 AM
  3. CYPHER - Python
    By Will Watts in forum Programming
    Replies: 1
    Last Post: 06-13-2012, 02:14 PM
  4. [Python] - Naughts and Crosses/Tic-Tac-Toe
    By Will Watts in forum Programming
    Replies: 2
    Last Post: 05-22-2012, 03:32 PM

Log in

Log in