Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 变量未定义?_Python_Variables - Fatal编程技术网

Python 变量未定义?

Python 变量未定义?,python,variables,Python,Variables,我是python的早期初学者,需要一些帮助。游戏的重点是猜一个数字,如果不正确,NUMGUESS加1。我知道代码不是很有效,但我需要它的一个项目。代码如下: 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:

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

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
回溯(最近一次呼叫最后一次):
文件“C:----------------------------.py”,第17行,在
numguesses=numguesses()+1
NameError:未定义名称“numgueses”


因此,如果有人能对此提供一个简单的解释,我将不胜感激。谢谢

在增加数值之前,必须先设置数值。试试这个:

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]?'))
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循环中嵌套第二个while循环时,在缩进中还发生了另一个错误。我也解决了这个问题。

您必须给
numguesss
一个起始值。试试这个:

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]?'))
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循环之前添加
numguesss=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

请阅读代码中的注释

numguesss
从来没有初始值,因此如何向其添加1?在
while
循环之前,需要
numguesss=0
。错误消息与脚本中的行不匹配。为什么在
numguesss
之后有
()
?您还需要在播放时缩进
的主体!='n'
如果您刚开始使用Python,并且在Windows上,为了您的理智,请立即安装Python 3!