2017-09-20 13 views
-2

私は以下のPythonコードを書いています.1〜100の数字を推測することになっています。考えている数値が高いか低いかを教えてください。私はそれを再生しようとしたとき、私は私の電話番号は、それが低いか、またはその逆だということを言った後に高いことを伝えるときしかし、いくつかの理由で、それは常に立ち往生:なぜこの推測コードがPythonで停止しているのですか

import random 
import time 
import math 

correct = 0 
goon = 'yes' 
biggest = 100 
smallest = 1 
tries = 10 



print 'Hi!' 
time.sleep(0.5) 
print 'I´m going to try and guess your number' 
time.sleep(0.5) 
print 'Just tell me if your number is bigger or smaller than what I guessed' 
time.sleep(0.5) 
print 'And of course you have to tell me when I´m right, ok?' 
time.sleep(0.5) 
print 'Type "b" if your number is smaller than what I guessed and type "s" if it´s bigger. When I´m right, type "r".' 
time.sleep(0.5) 
print 'Oh by the way, your number should be between 1 and 100.' 

if goon == 'no' or goon == 'No' or goon == 'n': 
    print 'Ok, see you soon!' 
else: 
    while goon == 'yes' or goon == 'Yes' or goon == 'y': 

     guess = random.randint(1,100) 
     print guess 
     answer = raw_input() 
     while correct == 0: 

      if answer == 'r': 
       correct = 1 
       endhooray = random.randint(1, 3) 
       if endhooray == 1: 
        print 'Yay, I got it!' 
       elif endhooray == 2: 
        print 'Finally!' 
       elif endhooray == 3: 
        print 'See, I´m good at this!' 




      elif answer == 'b': 
       smallest = guess 
       difference = 100 - guess 
       add = random.randint(1, difference) 
       guess = guess + add 
       if guess < biggest: 
        print guess 
        answer = raw_input() 
       elif guess > biggest: 
        while tries == 10: 
         add = random.randint(1, difference) 
         guess = guess + add 

         if guess < biggest: 
          print guess 
          answer = raw_input() 
          tries == 1000000 



      elif answer == 's': 
       biggest = guess 
       difference = guess - 100 
       difference = difference * -1 
       subtract = random.randint(1, difference) 
       guess = guess - subtract 
       if guess > smallest: 
        print guess 
        answer = raw_input() 
       elif guess < smallest: 
        while tries == 10: 
         subtract = random.randint(1, difference) 
         guess = guess - subtract 

         if guess > smallest: 
          print guess 
          answer = raw_input() 
          tries = 100000 



      else: 
       print 'Oops, I don´t know what that means' 
       break 
+1

問題を引き起こしたのと同じ入力を使用して、Pythonインタープリタであるかのように、コードを紙面で実行することをお勧めします。あなたは間もなく間違ったことをすぐに理解するでしょう – user234461

+0

私はこれまで数回これを行ってきましたが、なぜそれがうまくいかないのかの理由を見つけることはできません。 – shayan012

+0

教えてください:/ – shayan012

答えて

0

私はあなたのコードを簡素化するために自由を取りました。

import random 
import time 

# your intro here  

correct = False 
min_, max_ = 0, 100 

while not correct: 
    guess = random.randint(min_, max_) 
    answer = raw_input("Is your number %d? (b/s/r): " % guess) 
    if answer == "b": 
     min_ = guess+1 
     print "Ok, I'll aim higher!" 
    elif answer == "s": 
     max_ = guess 
     print "Ok, I'll aim lower!" 
    elif answer == "r": 
     hooray = random.choice(["Yay, I got it!", "Finally!", "See, I'm good at this!"]) 
     print hooray 
     correct = True 
    else: 
     print "Oops, I don't know what that means!" 
+0

ありがとうございますが、実際には動作しません。 79番はですか? (b/s/r):s よろしくお願いします。 あなたの番号は78ですか? (b/s/r):s よろしくお願いします。 あなたの番号は78ですか? (b/s/r):s よろしくお願いします。 番号77ですか? (b/s/r):s よろしくお願いします。 あなたの番号は78ですか? (b/s/r):s よろしくお願いします。 番号77ですか? (b/s/r):s よろしくお願いします。 あなたの番号は78ですか? (b/s/r):s よろしくお願いします。 あなたの番号は78ですか? (b/s/r):s よろしくお願いします。 番号79ですか? (b/s/r):s – shayan012

+0

申し訳ありません!推測では+1を取り除く= random.randint(min_、max_ + 1) –

+1

今はうまくいきますが、私のコードがうまくいかない理由を知りたいのです。 – shayan012

関連する問題