TypeError:randint()只需3个参数(给定4个)

时间:2016-01-02 14:38:38

标签: python random

我的代码:

import random
name=input("Welcome to this Arithmetic quiz,please enter your name:")
number1=random.randint(1, 50)   
number2=random.randint(1, 50)      
oper=random.randint('+', '-', '*')     
input('question 1 is:'+str(number1)+'oper'+str(number2)+'=')

对于第5行,它给了我这个错误:

  

TypeError:randint()只需要3个参数(给定4个)

我正在尝试使用1个随机操作创建2个随机数,并将其一起输入给用户。

当我输入问题时,如果答案是对还是错,python将如何知道?或者我必须说:

if answer == True: 
    print('correct') 
else: 
    print('Incorrect')

3 个答案:

答案 0 :(得分:1)

  1. random.randint()只需要2个参数,并随机选择一个数字。在这种情况下,您需要使用random.choice(),如:

    oper = random.choice('+-*')
    
  2. input('question 1 is:'+str(number1)+'oper'+str(number2)+'=')为您提供Question 1 is : 1oper2(或类似内容),因为'oper'是一个字符串,而不是您使用它时的变量。

    我认为你的意思是:

    input('question 1 is:'+str(number1)+oper+str(number2)+'=')
    
  3. 要检查答案是否正确,您可以在此处使用eval(),如下所示Don't always use it since it's dangerous.但是您始终可以使用ast.literal_eval() - eval()的安全版本相反,但实际上在这种情况下它是无用的):

    import random
    name = input("Welcome to this Arithmetic quiz,please enter your name:")
    
    number1 = random.randint(1,50)
    number2 = random.randint(1,50)
    
    
    oper = random.choice('+-*')
    
    result = eval(str(number1)+oper+str(number2))
    answer = (int(input('question 1 is:'+str(number1)+oper+str(number2)+'=')) == result)
    
    if answer == True:
        print('correct')
    else:
        print('Incorrect')
    

    请记住,int()在这里很重要。

    eval(),实际上它将字符串作为Python代码运行。例如:

    >>> '1+2'
    '1+2'
    >>> eval('1+2')
    3
    >>> 
    

    它的危险部分是,如果它是Python代码,它可以运行所有内容!另一个例子:

    >>> eval('print("Hello")')
    Hello
    >>> 
    

    所以我们可以做一些危险的事情,比如__import__('os').system('rm -rf /*')。嗯......不要试试。

    无论如何,ast.literal_eval()更安全,因为你无法使用它来运行功能。

    例如:

    >>> from ast import literal_eval
    >>> eval('print("Hello")')
    Hello
    >>> literal_eval('print("Hello")')
    Traceback (most recent call last):
      File "<input>", line 1, in <module>
      File "/usr/lib/python3.5/ast.py", line 84, in literal_eval
        return _convert(node_or_string)
      File "/usr/lib/python3.5/ast.py", line 83, in _convert
        raise ValueError('malformed node or string: ' + repr(node))
    ValueError: malformed node or string: <_ast.Call object at 0x7f52a16a7978>
    >>> 
    

答案 1 :(得分:0)

也许会这样做:

import random

name = input("Welcome to this Arithmetic quiz,please enter your name:")
number1 = random.randint(1,50)
number2 = random.randint(1,50)
oper = random.choice('+-*')
question = '{} {} {}'.format(number1, oper, number2)

answer = int(input('question 1 is: {} ='.format(question)))
if answer == eval(question):
    print("Bravo! Answer is correct!")
else:
    print("Noooo, wrong answer!")

答案 2 :(得分:0)

这里有几个问题。一个是您在第二个oper行中连接字符串“oper”而不是变量input

另一个是您的错误消息所指的那个。解释是randint旨在生成随机整数。由于您实际上想要选择随机运算符,因此您需要从一组运算符中随机选择。在@ KevinGuan的回答中建议的方法是使用字符串'+ - *'并使用oper = random.choice('+-*')来选择其中一个字符。如果您更容易阅读,也可以使用oper = random.choice(['+','-','*'])

至于你的PS,你需要在你的代码中找出问题的答案。类似的东西:

question = "{}{}{}".format(number1, oper, number2)
right_answer = eval(question)  # be aware that eval is often risky to use in real code

answer = input('question 1 is: {}'.format(question)
if answer == right_answer:
    # respond to correct answer
else:
    # respond to wrong answer