2017-12-22 24 views
-3

私は非常に早い初心者のpythonで、いくつかの助けが必要です。ゲームのポイントは、数字を推測することです。正しいnumguessesが1を追加していない場合は、コードはあまり効率的ではありませんが、私はプロジェクトに必要です。 HERESにコード:変数が定義されていませんか?

import random 

from datetime import datetime 
answer = random.randint(1, 2) 
guess = 0 
now = datetime.now() 
play = 'y' 


print 'Welcome to Hi-Lo' 
print 'The current time is: ' '%s-%s-%s' % (now.year, now.month, now.day) 

while play != 'n': 
while guess != answer: 
    guess = int(input('Pick a number inbewteen 1-100 to see if its higher or lower:')) 
    if guess == answer: 
     numguesses = numguesses + 1 
     print 'You are correct! The number was: ' + str(answer) 
     print 'It took ' + str(numguesses) + ' guess(es)' 
    elif guess > answer: 
     print 'Too high' 
    else: 
     print 'Too low' 

play = str(input('Play again? [y/n]?')) 

そして、それが生成するエラーは次のとおりです。

Traceback (most recent call last): 
    File "C:-----------------------------.py", line 17, in <module> 
numguesses = numguesses() + 1 

NameError:誰かがこのための簡単な説明を提供することができれば名 'numguesses' がそう

が定義されていません、私はそれをお願い申し上げます。ありがとう!

+2

コード内のコメントをお読みください'while'ループの前に' numguesses = 0'が必要です。 – roganjosh

+3

エラーメッセージがスクリプト内の行と一致しません。 'それは' 'numguesses'の後に'() 'を持つのはなぜですか? – Barmar

+0

また、 'while play!= 'n''の本文を字下げする必要があります – Barmar

答えて

0

numguessesを増やす前に設定する必要があります。これを試してみてください:

import random 

from datetime import datetime 
answer = random.randint(1, 2) 
guess = 0 
numguesses = 0 
now = datetime.now() 
play = 'y' 


print 'Welcome to Hi-Lo' 
print 'The current time is: ' '%s-%s-%s' % (now.year, now.month, now.day) 

while play != 'n': 
    while guess != answer: 
     guess = int(input('Pick a number inbewteen 1-100 to see if its higher or lower:')) 
     if guess == answer: 
      numguesses = numguesses + 1 
      print 'You are correct! The number was: ' + str(answer) 
      print 'It took ' + str(numguesses) + ' guess(es)' 
     elif guess > answer: 
      print 'Too high' 
     else: 
      print 'Too low' 

    play = str(input('Play again? [y/n]?')) 

はまた、あなたの最初のループしながら、あなたの第二の入れ子にするときインデントで別のエラーをしました。私もこれを修正しました。

0

numguessesに開始値を指定する必要があります。試してみてください:

numguesses = 0 
while guess != answer: 
    guess = int(input('Pick a number inbewteen 1-100 to see if its higher or lower:')) 
    if guess == answer: 
     numguesses = numguesses + 1 

whileループの前にちょうどnumguesses = 0を追加してください。変数を使用する前に宣言する必要があります。

0
1 #!/usr/bin/env python 
    2 
    3 import random 
    4 from datetime import datetime 
    5 
    6 answer = random.randint(1, 2) 
    7 guess = 0 
    8 # You need to define and initialise numguesses before you can increment below by 1 
    9 numguesses = 0 
10 now = datetime.now() 
11 play = 'y' 
12 
13 print 'Welcome to Hi-Lo' 
14 print 'The current time is: ' '%s-%s-%s' % (now.year, now.month, now.day) 
15 
16 while play != 'n': 
17 while guess != answer: 
18  guess = int(input('Pick a number inbewteen 1-100 to see if its higher or lower:')) 
19  if guess == answer: 
20   numguesses = numguesses + 1 
21   print 'You are correct! The number was: ' + str(answer) 
22   print 'It took ' + str(numguesses) + ' guess(es)' 
23   # after the user has guessed correctly, it will start all over again 
24   # hence you need to reset 'guess' and 'numguesses' 
25   guess = 0 
26   numguesses = 0 
27   play = raw_input('Play again? [y/n]? ') 
28   # and you need to get out of the loop altogether if user selects 'n' 
29   break 
30  elif guess > answer: 
31   print 'Too high' 
32   # everytime guess fails, numguesses needs to be increased by 1 
33   numguesses = numguesses + 1 
34  else: 
35   print 'Too low' 
36   # everytime guess fails, numguesses needs to be increased by 1 
37   numguesses = numguesses + 1 

`numguesses`は初期値を持つことはありませんので、どのようにあなたはそれに1を追加することができますか?

関連する問題