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 :
对于第5行,它给出了这个错误:
TypeError : randint() takes exactly 3 arguments (4 given)
TypeError: randint()只接受3个参数(给定4个)
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:
当我输入问题时,python如何知道答案是对还是错?或者我必须说:
if answer == True:
print('correct')
else:
print('Incorrect')
3 个解决方案
#1
1
-
random.randint()
only takes 2 arguments and choice a number between them randomly. You need userandom.choice()
in this case like:randint()只接受两个参数,并在它们之间随机选择一个数字。您需要使用random.choice(),比如:
oper = random.choice('+-*')
-
input('question 1 is:'+str(number1)+'oper'+str(number2)+'=')
gives youQuestion 1 is : 1oper2
(or something like that) because'oper'
is a string, not a variable when you use it.输入('问题1是:'+str(number1)+'oper'+str(number2)+'=')给你的问题1是:1oper2(或类似的东西),因为'oper'是字符串,在你使用它时不是变量。
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):
要检查答案是否正确,可以在这里使用eval()(不要总是使用它,因为它很危险。不过,您可以始终使用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')
Remember, int()
is important here.
记住,int()在这里很重要。
eval()
, actually it runs string as Python code. For example:
eval(),实际上它以Python代码的形式运行字符串。例如:
>>> '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:
它的危险之处在于,如果是Python代码,它可以运行一切!另一个例子:
>>> eval('print("Hello")')
Hello
>>>
So we can do something dangerous like __import__('os').system('rm -rf /*')
. Hmm...don't really try it.
所以我们可以做一些危险的事情,比如……系统(rm射频/ *)。嗯…不试一试。
Anyways, ast.literal_eval()
is more safe since you can't use it to run function.
无论如何,ast.literal_eval()更安全,因为您不能使用它来运行函数。
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>
>>>
#2
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!")
#3
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.
这里有几个问题。一是在第二个输入行中连接字符串“oper”,而不是变量oper。
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.
另一个是错误消息所指的。解释是randint打算生成一个随机整数。由于您实际上想要选择一个随机操作符,所以需要从一组操作符中随机选择。@KevinGuan建议的方法是使用字符串'+-*',并使用oper = random.choice('+-*')来选择其中一个字符。如果您更容易阅读,还可以使用oper = random.choice(['+','-','*'])。
As for your PS, you'll need to figure out the answer to the question in your code. Something like:
至于你的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
#1
1
-
random.randint()
only takes 2 arguments and choice a number between them randomly. You need userandom.choice()
in this case like:randint()只接受两个参数,并在它们之间随机选择一个数字。您需要使用random.choice(),比如:
oper = random.choice('+-*')
-
input('question 1 is:'+str(number1)+'oper'+str(number2)+'=')
gives youQuestion 1 is : 1oper2
(or something like that) because'oper'
is a string, not a variable when you use it.输入('问题1是:'+str(number1)+'oper'+str(number2)+'=')给你的问题1是:1oper2(或类似的东西),因为'oper'是字符串,在你使用它时不是变量。
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):
要检查答案是否正确,可以在这里使用eval()(不要总是使用它,因为它很危险。不过,您可以始终使用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')
Remember, int()
is important here.
记住,int()在这里很重要。
eval()
, actually it runs string as Python code. For example:
eval(),实际上它以Python代码的形式运行字符串。例如:
>>> '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:
它的危险之处在于,如果是Python代码,它可以运行一切!另一个例子:
>>> eval('print("Hello")')
Hello
>>>
So we can do something dangerous like __import__('os').system('rm -rf /*')
. Hmm...don't really try it.
所以我们可以做一些危险的事情,比如……系统(rm射频/ *)。嗯…不试一试。
Anyways, ast.literal_eval()
is more safe since you can't use it to run function.
无论如何,ast.literal_eval()更安全,因为您不能使用它来运行函数。
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>
>>>
#2
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!")
#3
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.
这里有几个问题。一是在第二个输入行中连接字符串“oper”,而不是变量oper。
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.
另一个是错误消息所指的。解释是randint打算生成一个随机整数。由于您实际上想要选择一个随机操作符,所以需要从一组操作符中随机选择。@KevinGuan建议的方法是使用字符串'+-*',并使用oper = random.choice('+-*')来选择其中一个字符。如果您更容易阅读,还可以使用oper = random.choice(['+','-','*'])。
As for your PS, you'll need to figure out the answer to the question in your code. Something like:
至于你的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