调用函数时的回溯错误

时间:2015-01-22 23:33:53

标签: python

问题: 定义一个函数goguess(),它实现了如下所述的数字猜谜游戏。作为一对程序员制定战略。然后迭代编码和测试。 在[]:goguess() 我有1到20之间的数字。 猜猜:6 6太低了。 猜猜:10 10太高了。 猜猜:9 对!我的号码是9!你猜三猜!

代码:

   from __future__ import print_function
import random
def goguess():
    guesses=0
    HighNumber = random.randint()
    LowNumber = random.randint()
    if LowNumber >= HighNumber:
        LowNumber = LowNumber - HighNumber
    answer = random.randint(LowNumber,HighNumber)
    print ('I have a number between', LowNumber, 'and', HighNumber, 'inclusive.')
    guess = raw_input("Guess:")
    while guess != answer:
        if guess < answer:
            print (guess, 'is too low')
            guess = raw_input("Guess:")
            guesses += 1
        elif guess > answer:
            print (guess, 'is too high')
            guess = raw_input("Guess:")
            guesses += 1
        else:
            print ('Right! My number is', answer, '!', 'You guessed in', guesses,  'guesses!')

错误:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 4, in goguess
TypeError: randint() takes exactly 3 arguments (1 given)

2 个答案:

答案 0 :(得分:2)

您需要调用随机整数:

random.randint(1, 20)

你需要IE ()。否则,您将变量分配给函数本身,而不是调用随机整数函数的结果。

答案 1 :(得分:0)

而不是

HighNumber = random.randint
你应该

HighNumber = random.randint(1,20) #Inclusive Range

这是因为randint是一个函数,因此它需要括号(在这种情况下是参数)。