1

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')
Arash Hatami
  • 5,297
  • 5
  • 39
  • 59
PythonNooby
  • 33
  • 1
  • 7
  • please explain input and output. i.e. provide a [MCVE](http://stackoverflow.com/help/mcve) – Pynchia Jan 02 '16 at 14:45
  • there are multiple issues with your code and it is unclear what you are asking. Fix the whole lot? I have answered how to obtain a random operation but apparently it's not enough – Pynchia Jan 02 '16 at 14:46
  • I have , The code is what I am inputting and the error is what I am getting. – PythonNooby Jan 02 '16 at 14:48
  • @Pynchia Before you deleted your post I tried the code you gave me but instead of the operatin it gave me the word 'oper'.And I have said LINE 5 , that is my only problem.What is wrong with my code ? – PythonNooby Jan 02 '16 at 14:50
  • EDIT= Your code does work but I had to take away the speech marks from the word 'oper' on the last line.Thank you.Can you help me on the question I asked at the end.Could you also tell me why it is choice and not randint ? – PythonNooby Jan 02 '16 at 14:52

3 Answers3

2
  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>
>>> 
Community
  • 1
  • 1
Remi Guan
  • 21,506
  • 17
  • 64
  • 87
  • Thank you very much for your answer Kevin.Although I already figured out your second point , thank you for your first , now I know the difference.The problem is I haven't learnt str.format() at school yet , so I cant use it.But could you please show me how I can tell the user f his/her answer is correct or not by USING A BOOLEAN.(like my attempt in my question). – PythonNooby Jan 02 '16 at 15:23
  • @PythonNooby: Hmm...it's simple to do it use `eval()`, but it's little dangerous. However I can also do it via some loops or a dict...but what do you mean about *USING A BOOLEAN* ? – Remi Guan Jan 02 '16 at 15:27
  • ok..Do you see my attempt in my question ? how can I alter it to make it work , because even if I enter the correct answer it still prints Incorrect. – PythonNooby Jan 02 '16 at 15:30
  • OMG , thank you so much man and thanks for pointing out int() completely forgot.Can you please just quickly explain what is the use of eval so I can add that to my annotations so my teacher doesn't think I am copying and pasting. – PythonNooby Jan 02 '16 at 15:39
  • wow , thank you for your effort although I just wanted a worded description of what the purpose of eval was e.g in the example you showed me : eval('print("Hello")') , all it did was print 'Hello' , you could have printed 'Hello' without the use of eval.So what was the point of eval in the code you gave me ? – PythonNooby Jan 02 '16 at 15:53
  • @PythonNooby: Let's say you developed a web page, which is a calculator and you're using `eval()` in this case. So the user could type `1+9` and get `10`. But however he can also run other Python code. As I said in answer, `'rm -rf /*'` is a Unix command which can delete all files in the server. So if user enter that instead of use it as a calculator, your server will be *cleared, hacked*, and other dangerous things. – Remi Guan Jan 03 '16 at 00:22
  • So sometimes it's safe, for example in this case since `number1`, `number2` and `oper` were generated by yourself. But don't always use it, and never do something like `eval(input())` to run user's input. Since we always say: [**Never trust any input!**](http://stackoverflow.com/a/2794089/5299236) – Remi Guan Jan 03 '16 at 00:24
0

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!")
Pynchia
  • 10,996
  • 5
  • 34
  • 43
  • Thank you for you answer.The thing is ,this is for school and if I copy I will get a U.I just had a go but even if I enter the correct answer it still prints Incorrect.(I used a boolean) `code` answer="" if answer == True : print('Correct !') else: print('Incorrect!') – PythonNooby Jan 02 '16 at 15:14
  • no problem, I understand. You had gotten good asnwers already. For the record (and someone else with similar problems) I wanted to show an alternative solution from scratch. – Pynchia Jan 02 '16 at 15:22
0

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
Jamie Bull
  • 12,889
  • 15
  • 77
  • 116