2013-05-14 4 views
-3

안녕하세요, 저는 이미 두 번 질문하지만 저는 답을 찾을 수 없다는 것을 알고 있습니다. 질문은 내 번호 게임의 역 추측에 관한 것입니다. 코드는 프로그램을 실행하지만 "인간과 같은"방식으로 실행하지는 않습니다. 숫자가 50이고 20이 더 높다고 추측하면 컴퓨터는 예를 들어 30이라고 말합니다. 응답이 낮을 것으로 추측합니다. 15. 어떻게 해결합니까? 연습은 다음과 같습니다. 절대 초보자를위한 Python. 누군가 나를 도울 수 있습니까? 나는 책에서 건너 뛴다. 코드를 보면 내가 아는 것과 볼 수없는 것을 볼 수 있다고 생각합니다. Binary Search에 최대 읽기역방향 게임 파이썬

#Guess My Number 
# 
#The computer picks a random number between 1 and 100 
#The playes tries to guess it and the coputer lets 
#the player know if the guess is too high, too low 
#or right on the money 

print ("\t// // // // // // // // // //") 
print ("\tWelcome to 'Guess My Number'!") 
print ("\tComputer VS Human") 
print ("\t// // // // // // // // // //") 
name = input("What's your name?") 
print ("Hello,", name) 
print ("\nOkay, think of a number between 1 and 100.") 
print ("I'll try to guess it within 10 attemps.") 

import random 

#set the initial values 

the_number = int(input("Please type in the number to guess:")) 
tries = 0 
max_tries = 10 
guess = random.randint(1, 100) 

#guessing loop 
while guess != the_number and tries < max_tries: 
    print("Is it", guess,"?") 
    tries += 1 

    if guess > the_number and tries < max_tries: 
     print ("It's lower") 
     guess = random.randint(1, guess) 
    elif guess < the_number and tries < max_tries: 
     print ("It's higher") 
     guess = random.randint(guess, 100) 
    elif guess == the_number and tries < max_tries: 
     print("Woohoo, you guessed it!") 
     break 
    else: 
     print("HAHA you silly computer it was", the_number,"!") 

input ("\n\nTo exit, press enter key.") 

답변

2

이 올바른 방향을 가리켜 야 :

코드 ... 제발 도와주세요.

4

지능적으로 추측 할 수 있도록 가능한 가장 높은 값과 가장 낮은 값을 추적해야합니다.

처음에는 가능한 가장 낮은 값은 1이고 가장 높은 값은 100입니다. 50을 가정하면 컴퓨터가 "높음"으로 응답합니다. 두 변수는 어떻게됩니까? 숫자가 그보다 더 낮을 수 없으므로 가장 낮은 값은 이제 50이됩니다. 가장 높은 값은 동일하게 유지됩니다.

컴퓨터가 "lower"로 응답하면 반대가 발생합니다.

random.randint(lowest, highest)

을 그리고 예상대로 추측 작동합니다 :

그럼 당신은 가장 낮고 가장 높은 값 사이에 추측됩니다.

+0

고맙습니다.하지만 코드에 어디에 넣어야합니까? – bogaardesquat

+0

'import random '뒤에'lowest'와'highest' 두 개의 변수를 선언하십시오. "lower"와 "higher"if 문 모두에서 변수의 값을 재조정합니다. 여러분이 추측 할 때마다'guess = random.randint (최저, 최고)' – Lanaru

0

일반적으로이 게임은 새로운 추측이있을 때마다 가능한 숫자 범위를 작게 만들어 작동합니다. 즉

1st guess = 20 
guess is too low 
--> range of guesses is now (21, 100) 

2nd guess = 45 
guess is too high 
--> range of guesses is now (21, 44) 
etc... 

테스트에서 이전의 모든 추측을 잊어 버렸기 때문에이를 수행 할 수 없습니다. 범위의 더 낮은 쪽과 더 높은 쪽을 추적하려고 시도 할 수 있습니다.

lower_range, higher_range = 1, 100 
max_tries = 10 

#guessing loop 
while tries < max_tries: 
    guess = random.randint(lower_range, higher_range) 
    print("Is it", guess,"?") 
    tries += 1 

    if guess > the_number:  
     print ("It's lower") 
     higher_range = guess - 1 

    elif guess < the_number: 
     print ("It's higher") 
     lower_range = guess + 1 

    else: # i.e. correct guess 
     print("Woohoo, you guessed it!") 
     input ("\n\nTo exit, press enter key.") 
     sys.exit(0) 

print("HAHA you silly computer it was", the_number,"!") 

while 루프를 약간 정돈했습니다.

종종이 게임은 바이너리 검색 방법도 활용합니다. 재미로, 당신은 이것을 구현하려고 할 수 있습니다 :) 희망이 도움이!

+0

고맙습니다. 당신은 정말 유휴 상태 (데비안 리눅스에서 파이썬 3.1 유휴 상태)에서 나를 위해 일하지 않았다. 그러나 나는 higher_range = -1을 내 코드에 lower_range로 추가했다. 고맙습니다. – bogaardesquat