I am working on an exercise in a book and have difficulty with it. I have completed the first part of the exercise which was to create a guessing game where the the player attempts to guess a secret number randomly generated. Suggestions such as to guess Lower and Higher are also provided to help to narrow down to the secret number The code for that is below. The second part of the exercise is for the player to choose a number and allow the computer to guess that number. This is where I am struggling and I dont know why. I would appreciate it if someone would give me a a little 'push' to help me on this rather than the solution as I will need to do this on my own.
Complete code for first part of exercise:
#Guess my Number - Exercise 3 #Limited to 5 guesses import random attempts = 1 secret_number = random.randint(1,100) isCorrect = False guess = int(input("Take a guess: ")) while secret_number != guess and attempts < 6: if guess < secret_number: print("Higher...") elif guess > secret_number: print("Lower...") guess = int(input("Take a guess: ")) attempts += 1 if attempts == 6: print("\nSorry you reached the maximum number of tries") print("The secret number was ",secret_number) else: print("\nYou guessed it! The number was " ,secret_number) print("You guessed it in ", attempts,"attempts") input("\n\n Press the enter key to exit")
Thank you for your help.
Edited 4 Years Ago by fdama