对不起,我在这里是个新手&到了python,我在其他问题上找不到答案。
我要做的是将随机数学运算符转换为字符串,以便我可以在print语句问题中使用它。这就是我所做的,我知道这是错的,但我希望你能看到我的逻辑。
任何帮助将不胜感激。
while not exitProgram:
for count in range(10):
print ("Question ", (count+1),":")
integer1 = random.randint(0,10)
print (integer1) #This is used for testing
integer2 = random.randint(0,10)
print (integer2)#Again used for testing
ops = add,sub,mul
op = random.choice(ops)
print (op) #testing
correctAnswer = op(integer1, integer2)
print (correctAnswer)
opPrint = ""
if op == <built-in function sub>:
opPrint = "-"
print (opPrint)
if op == "<built-in function mul>":
opPrint = "*"
print (opPrint)
if op == "<built-in function add>":
opPrint = "+"
print (opPrint)
print ("What is " ,integer1, opPrint, integer2)
answer = int(input("Please enter your answer\n"))
答案 0 :(得分:0)
import random
def add(int1, int2):
return int1+int2
def sub(int1, int2):
return int1-int2
def mul(int1, int2):
return int1*int2
exitProgram = False
while not exitProgram:
for count in range(10):
print("Question "+str(count+1)+":")
integer1 = random.randint(0,10)
integer2 = random.randint(0,10)
ops = add,sub,mul
op = random.choice(ops)
correctAnswer = op(integer1, integer2)
opPrint = ""
if op == sub:
opPrint = "-"
if op == mul:
opPrint = "*"
if op == add:
opPrint = "+"
print ("What is "+str(integer1)+opPrint+str(integer2))
answer = int(input("Please enter your answer\n"))
答案 1 :(得分:0)
您应该查看operator
模块,该模块提供对标准运算符的访问作为一组函数。在这里,您需要operator.__add__
,operator.__sub__
和operator.__mul__
。
而不是if
/ elif
/ else
链,使用字典在代码引用和字符串之间进行映射更为清晰。< / p>
我会像这样写你的程序
import random
from operator import __add__, __sub__, __mul__
ops = __add__, __sub__, __mul__
symbols = {
__add__: '+',
__sub__: '-',
__mul__: '*',
}
score = 0
for count in range(10):
p1 = random.randint(0, 10)
p2 = random.randint(0, 10)
op = random.choice(ops)
sum_text = '%d %s %d' % (p1, symbols[op], p2)
print('\nQuestion %d: what is %s?' % (count + 1, sum_text))
answer = int(input('Please enter your answer: '))
correct_answer = op(p1, p2)
if answer == correct_answer:
print('Correct!')
score += 1
else:
print("Sorry, that's wrong")
print('The correct answer is %d' % correct_answer)
print('You scored a total of %d out of %d' % (score, 10))
答案 2 :(得分:-1)
import random
def add(i1,i2):
return i1+i2
def sub(i1,i2):
return i1-i2
def mul(i1,i2):
return i1*i2
exitProgram=True
while exitProgram:
for count in range(10):
print ("Question ", (count+1),":")
integer1 = random.randint(0,10)
print ("test",integer1) #This is used for testing
integer2 = random.randint(0,10)
print ("again testing",integer2)#Again used for testing
ops = add,sub,mul
op = random.choice(ops)
#print (op) #testing
correctAnswer = op(integer1, integer2)
#print ("correct Answer",correctAnswer)
opPrint = ""
if op.func_name == "sub":
opPrint = "-"
print (opPrint)
if op.func_name == "mul":
opPrint = "*"
print (opPrint)
if op.func_name == "add":
opPrint = "+"
print (opPrint)
#print ("What is %s %s %s" ))
print ("What is " ,integer1, opPrint, integer2)
answer = int(input("Please enter your answer\n"))
print ("your answer",answer)
print ("correct answer",correctAnswer,"\n")