0

そこで私は、コンピュータが選択した数字を推測する代わりに、コンピュータが私が考えている数字を推測しようとするプログラムを書く方法を見つけようとしています。ほとんどの場合は機能しますが、たとえば、考えている値が「7」よりも大きいなど、前に言ったとしても、チェーンの下に数字が繰り返される場合があります。場合によっては、数値が高いか低いかを伝えても、同じ数値を繰り返すこともあります。より経験豊富な人がこれを見て、これらのループで何が欠けているかを教えてくれれば、大いに役立つでしょう.

#computer enters a value x
#lower - computer guesses lower than x
#higher - computer guesses higher than x
#when string "You got it!" - game over

import random

lowBound = 0
highBound = 100
randomNumber = random.randint(lowBound,highBound)

print ("Is it ", randomNumber, " ?")
response = input()

while response != "You got it!":
    if response == "higher":
        lowBound = randomNumber    
        randomNumber = random.randint (lowBound, highBound)
        print ("Is it ", randomNumber, " ?")
        response = input()

    elif response == "lower":
        highBound = randomNumber
        randomNumber = random.randint (lowBound, highBound)
        print ("Is it ", randomNumber, " ?")
        response = input()

    if response == "You got it!":

        print ("Woohooo, I'm so bitchin'")
4

5 に答える 5

1

random.randint(a, b)aと を含む~の間の数値を返しますb。新しい乱数を生成するときは、使用する必要がありますrandom.randint(lowBound+1, highBound-1)

于 2013-02-15T01:54:52.717 に答える
0

これは、Michael Dawson の本からのこの演習の私のバージョンです。コンピューターが使用する試行回数を最小限に抑えようとしました。私はコードが危険に見えることを知っています、それは私の2日目です:)


answer=""
guess=50
counter=3
x=25

print("hi, guess the number from 1 too 100")
input("\n")

print ("i will try to guess it")
print ("is it ", guess, "?")

while answer not in ("y","l","s"):
    print ("sorry, i didn't understand \n")
    answer=input("type in: (Y) for yes, or (L) if it is to large, or (S) if it is to small:")

if answer in ("s","l"):
    while answer!="y":

        if answer=="l":
            guess=int(guess-x)
            print ("how about", guess,"?")
            answer=input("\nis it? type in: (Y) for yes, or (L) if it is to large, or (S) if it is to small:")
            x=100/2**counter
            counter=counter+1
            if x<1:
                x=1

        elif answer=="s":
            guess=int(guess+x)
            print ("how about", guess,"?")
            answer=input("\nis it? type in: (Y) for yes, or (L) if it is to large, or (S) if it is to small:")
            x=100/2**counter
            counter=counter+1
            if x<1:
                x=1

        elif answer=="y":
            break
else:
    pass

print("\ngreat! the number that you guessed is", guess)
print("i can read your mind with no guesses!")
input("\n")
于 2014-10-13T16:47:15.827 に答える
0

言及されている他の問題の1つは、次の行にあります。

highBound = randomNumber
randomNumber = random.randint (lowBound, highBound)

あなたは新しい境界を設定していますが、それは良いことですが、別の乱数を選択しています!

あなたがすべきことは、境界を半分にし、そこからユーザーに高くまたは低く尋ねることです。二分探索アルゴリズムを見てください。

highBound = randomNumber
randomNumber = randomNumber / 2

あなたのプログラムは (ここで述べた他の変更を加えて) 引き続き動作しますが、これにより、ほとんどの場合、数値がより速く推測されます。

ウィキペディアに実際にこのゲームの例があります。

于 2013-02-15T01:58:10.340 に答える
-1

random.randintの境界は包括的であるため、数値を 2 回取得します。random.randint(1, 3)1、2、または 3 を返すことができます。また、応答が「より高い」、「より低い」、または「わかりました!」のいずれでもないかどうかを人間に尋ね続ける必要があることに注意してください。

import random
lowBound = 0
highBound = 100

while True:
    randomNumber = random.randint(lowBound, highBound)
    print ("Is it ", randomNumber, " ?")
    response = input()

    if response == "higher":
        lowBound = randomNumber + 1
    elif response == "lower":
        highBound = randomNumber - 1

    if response == "You got it!":
        print ("Woohooo, I'm so bitchin'")
        break
于 2013-02-15T01:55:06.113 に答える