Guess the number game optimization (user creates n

2020-07-27 20:53发布

I am very new to programming so I decided to start with Python about 4 or 5 days ago. I came across a challenge that asked for me to create a "Guess the number" game. After completion, the "hard challenge" was to create a guess the number game that the user creates the number and the computer (AI) guesses.

So far I have come up with this and it works, but it could be better and I'll explain.

``````from random import randint

print ("In this program you will enter a number between 1 - 100."
"\nAfter the computer will try to guess your number!")

number = 0

while number < 1 or number >100:
number = int(input("\n\nEnter a number for the computer to guess: "))
if number > 100:
print ("Number must be lower than or equal to 100!")
if number < 1:
print ("Number must be greater than or equal to 1!")

guess = randint(1, 100)

print ("The computer takes a guess...", guess)

while guess != number:
if guess > number:
guess -= 1
guess = randint(1, guess)
else:
guess += 1
guess = randint(guess, 100)
print ("The computer takes a guess...", guess)

print ("The computer guessed", guess, "and it was correct!")
``````

This is what happened on my last run:

Enter a number for the computer to guess: 78

The computer takes a guess... 74

The computer takes a guess... 89

The computer takes a guess... 55

The computer takes a guess... 78

The computer guessed 78 and it was correct!

Notice that it works, however when the computer guessed 74, it then guessed a higher number to 89. The number is too high so the computer guesses a lower number, however the number chosen was 55. Is there a way that I can have the computer guess a number that is lower than 89, but higher than 74? Would this require additional variables or more complex if, elif, else statements?

Thank you Ryan Haining

I used the code from your reply and altered it slightly so the guess is always random. If you see this, let me know if this is the best way to do so.

``````from random import randint

def computer_guess(num):
low = 1
high = 100
# This will make the computer's first guess random
guess = randint(1,100)
while guess != num:
print("The computer takes a guess...", guess)
if guess > num:
high = guess
elif guess < num:
low = guess + 1
# having the next guess be after the elif statement
# will allow for the random guess to take place
# instead of the first guess being 50 each time
# or whatever the outcome of your low+high division
guess = (low+high)//2

print("The computer guessed", guess, "and it was correct!")

def main():
num = int(input("Enter a number: "))
if num < 1 or num > 100:
print("Must be in range [1, 100]")
else:
computer_guess(num)

if __name__ == '__main__':
main()
``````

10条回答
``````import random

corr_num = random.randint(1,100)

player_tries = 0
com_tries = 0

while player_tries <5 and com_tries < 5:
player = int(input("player guess is "))
if player > corr_num:
print("too high")
player_tries +=1

if player < corr_num:
print("too low")
player_tries +=1

if player == corr_num:
print("Player wins")
break

computer = random.randint(1,100)
print("computer guess is ", computer)

if computer > corr_num:
print("too high")
com_tries = 0

if computer < corr_num:
print("too low")
com_tries = 0

if computer == corr_num:
print ("computer wins")
break

else:
print("Game over, no winner")**strong text**
``````

You only need two new variables to keep track of the low and high limits :

``````low = 1
high = 100
while guess != number:
if guess > number:
high = guess - 1
else:
low = guess + 1
guess = randint(low, high)
print ("The computer takes a guess...", guess)
``````

What I did for the same challenge was:

1) Define a variable that records the max value input by guessing computer. Ex:

``````max_guess_number = 0
``````

2) Define another variable with the lowest guessed value. Ex.

``````min_guess_number = 0
``````

3) Added in the "if computer_guess > secret_number" clause the following code (I added -1 so that the computer wouldn't try to guess the already previously tried number):

``````max_guess_number = guess - 1
computer_guess = random.randint(min_guess_number, max_guess_number)
``````

4) Added the following code in the "if computer_guess < secret_number":

``````min_guess_number = guess + 1
computer_guess = random.randint(min_guess_number, max_guess_number)
``````

Worth noting is the fact that I set my while loop to loop until another variable "guess_status" changes into a value 1 (the default I set to 0). This way I actually saw the result when the while loop finished.

``````import random
x = 1
y = 99
javab = input('user id: ')
while javab != 'd':
if javab == 'b':
javab = input('user id: ')
else:
javab == 'k'
javab = input('user id: ')
``````

I briefly made the game which you need with follows:

``````                  import random

guess=int(input("Choose a number you want the computer to guess from  1-100: "))

turns=0
a=None

compguess=random.randint(1,100)

while turns<10 and 100>guess>=1 and compguess!=guess: #computer has 10 turns to guess number, you can change it to what you want
print("The computer's guess is:  ", compguess)
if compguess>guess:
a=compguess
compguess=random.randint(1,compguess)

elif compguess<guess:
compguess=random.randint(compguess,a)
turns+=1

if compguess==guess and turns<10:
print("The computer guessed your number of:" , guess)
turns+=1

elif turns>=10 and compguess!=guess:
print("The computer couldn't guess your number, well done.")

input("")
``````

This is a bit rusty, but you could improve it by actually narrowing down the choices so the computer has a greater chance of guessing the right number. But where would the fun in that be? Notice how in my code, if the computer guesses a number which is greater than than the number the user has inputed, it will replace 100 from the randint function with that number. So if it guesses 70 and its too high, it won't choose a number greater than 70 after that. I hope this helps, just ask if you need any more info. And tell me if it's slightly glitchy

``````print 'Please think of a number between 0 and 100!'
low = 0
high = 100
while(True):
rand = (high+low)/2
print 'Is your secret number '+str(rand)+'?'
ans = raw_input("Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate I guessed correctly.")
if ans=='h':
high = rand
elif ans=='l':
low = rand
elif ans=='c':
print "Game over. Your secret number was:",rand
break
else:
print "Sorry, I did not understand your input"
``````