变量未定义?

时间:2017-12-22 21:07:11

标签: python variables

我是python的初学者,需要一些帮助。游戏的目的是猜测一个数字,如果不正确的数字加上1.我知道代码不是很有效但我需要它为一个项目。下面是代码:

$(RootNamespace).gui.%(Filename).resources

它产生的错误是:

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]?'))

NameError:名称'numguesses'未定义

所以,如果有人可以为此提供一个简单的解释,我将不胜感激。谢谢!

3 个答案:

答案 0 :(得分: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]?'))

当您在第一个while循环中嵌套第二个while循环时,您还在缩进时出现了另一个错误。我也解决了这个问题。

答案 1 :(得分: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即可。您必须在使用它们之前声明变量。

答案 2 :(得分: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

请阅读代码中的评论