Jan-04-2018, 12:16 AM
Hi I am new to python. I am trying to create a program where the user inputs a number and the computer guesses it in as few tries as possible. This is what I have so far. I am stuck. (Side Note... I am unsure why when I posted my code to this forum that it did not maintain the indentation.)
import random print("In this game you will guess a number and the computer will try to guess it in as few tries as possible.") number = int(input("\n\nEnter a number 1-100 --- ")) cNumber = random.randint(1, 100) placeholder = cNumber tries = 1 print(number, cNumber, tries, placeholder) print("\nThe computer guessed ",cNumber) while cNumber != number: if cNumber > number: cNumber = random.randint(1,placeholder) tries += 1 print("\nThe computer guessed",cNumber) if cNumber < number: cNumber = random.randint(placeholder, 1) tries +=1 print("\nThe computer guessed",cNumber) print("The computer guessed your number in this many tries --- ",tries)