PythonNooby
PythonNooby

Reputation: 33

TypeError : randint() takes exactly 3 arguments (4 given)

My code :

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)+'=')

For line 5 it gives me this error :

TypeError : randint() takes exactly 3 arguments (4 given)

I am trying to create 2 random numbers with 1 random operation and input it together for the user.

When I input the question how will python know if the answer is right or wrong? Or do I have to say:

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

Upvotes: 1

Views: 8415

Answers (3)

Remi Guan
Remi Guan

Reputation: 22312

  1. random.randint() only takes 2 arguments and choice a number between them randomly. You need use random.choice() in this case like:

    oper = random.choice('+-*')
    
  2. input('question 1 is:'+str(number1)+'oper'+str(number2)+'=') gives you Question 1 is : 1oper2 (or something like that) because 'oper' is a string, not a variable when you use it.

    I think you mean:

    input('question 1 is:'+str(number1)+oper+str(number2)+'=')
    

To check the answer is correct or not, you can simply use eval() here like below (Don't always use it since it's dangerous. However you can always use ast.literal_eval() - a safe version of eval() instead, but actually it's useless in this case):

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

Remember, int() is important here.


eval(), actually it runs string as Python code. For example:

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

The dangerous part of it is, it can run everything if it's Python code! Another example:

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

So we can do something dangerous like __import__('os').system('rm -rf /*'). Hmm...don't really try it.

Anyways, ast.literal_eval() is more safe since you can't use it to run function.

For example:

>>> 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>
>>> 

Upvotes: 2

Jamie Bull
Jamie Bull

Reputation: 13539

You have a couple of issues here. One is that you are concatenating the string "oper" rather than your variable oper in your second input line.

The other is the one your error message is referring to. The explanation is that randint is intended to generate a random integer. Since you actually want to choose a random operator, you need to choose at random from a group of operators. The approach suggested in @KevinGuan's answer is to use the string '+-*' and use oper = random.choice('+-*') to select one of the characters. You could also use oper = random.choice(['+','-','*']) if that is easier for you to read.

As for your PS, you'll need to figure out the answer to the question in your code. Something like:

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

Upvotes: 0

Pynchia
Pynchia

Reputation: 11606

Maybe this will do:

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!")

Upvotes: 0

Related Questions