Python 将随机运算符add、mul或sub转换为字符串+;*-
很抱歉,我是python的新手,在其他问题上找不到答案 我要做的是将随机数学运算符转换为字符串,以便在打印语句问题中使用它。这就是我所做的,我知道这是错误的,但我想让你看看我的逻辑 任何帮助都将不胜感激Python 将随机运算符add、mul或sub转换为字符串+;*-,python,Python,很抱歉,我是python的新手,在其他问题上找不到答案 我要做的是将随机数学运算符转换为字符串,以便在打印语句问题中使用它。这就是我所做的,我知道这是错误的,但我想让你看看我的逻辑 任何帮助都将不胜感激 while not exitProgram: for count in range(10): print ("Question ", (count+1),":") integer1 = random.randint(0,10) 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"))
不退出程序时:
对于范围(10)内的计数:
打印(“问题”,(计数+1),“:”)
整数1=random.randint(0,10)
打印(整数1)#用于测试
整数2=random.randint(0,10)
打印(整数2)#再次用于测试
ops=添加、分段、分段
op=随机选择(ops)
打印(op)#测试
correctAnswer=op(整数1,整数2)
打印(正确答案)
opPrint=“”
如果op==:
opPrint=“-”
打印(opPrint)
如果op==“”:
opPrint=“*”
打印(opPrint)
如果op==“”:
opPrint=“+”
打印(opPrint)
打印(“What is”,integer1,opPrint,integer2)
answer=int(输入(“请输入您的答案”))
您应该查看该模块,该模块将标准操作员作为一组功能提供访问权限。这里需要运算符、运算符、子运算符和运算符
与其使用if
/elif
/else
链,不如使用字典在代码引用和字符串之间进行映射
我会这样写你的程序
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))
你的代码没有任何意义;将一个对象与类似字符串的字符串进行比较是行不通的(除了看看它是否真的是那个字符串)。考虑把你的运算符函数放在字典<代码> {'+':Addio.}中,这将使你的生活变得简单多了。我读到“把一个对象比作一个字符串就好像永远不会去工作”,并在试图寻找原因。博罗丁错过了“那个”,对不起!谢谢你所有的帮助help@RH84这回答了你的问题吗?
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))
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")