Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

Number game extra credit - Is this what he wants...

So I have completed the game. I have made it so it check to see if the random number is either higher or lower than my number given.

If it is generate a new random number but first check if that random number is in a list of previous random numbers if it is not append that random number to the list and carry on until either the guesses run out or the computer guesses the number.

Is this what the extra credit wants?

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat May 13 15:11:20 2017

@author: mayur
"""

import random


def get_input():

    #Computer generates first random number
    randNum = random.randint(1,10)
   #Input a number
    try:
        num = int(input("Enter a number for pc to guess: "))
    except ValueError:
        print('{} is not a valid number please input again'.format(num))
    else:
        game(randNum,num)

def game(randNum,num):
    listOfRandomNums = [randNum]
    while len(listOfRandomNums) <= 5:
        if randNum > num:

            print('{} is greater than my number'.format(randNum))
            #check if generated number is already in list
            while True:
                #Computer generate new random number according to condition
                randNum = random.randint(1,randNum - 1)
                #check if randNum is in list
                if randNum not in listOfRandomNums:
                    #if not in list append to list and break loop
                    listOfRandomNums.append(randNum)
                    break

        elif randNum < num:
            print('{} is less than my number'.format(randNum))

            while True:
                randNum = random.randint(randNum + 1, 10)
                if randNum not in listOfRandomNums:
                    listOfRandomNums.append(randNum)
                    break
        else:
            print('{} is equal to my answer woohoo'.format(randNum))
            print()
            print('It took {} guesses for computer to guess your number'.format(len(listOfRandomNums)))
            break

    else:
        print('The computer did not guess your number')
    next_game = input('Do you want to play again Y for yes any key for no: ')
    if next_game.lower() == 'y':
        get_input()
    else:
        print('Thank you for playing')

get_input()

Just take the original script and swap the computer and human input. To tell the computer if higher or lower change the range of the random number with the computer guess.