在Python3中将运算符放置在列表中
我想把操作符作为一个列表,然后调用列表中的一个元素作为操作符使用 如果没有在运算符周围加引号,则列表中的逗号会出现语法错误:在Python3中将运算符放置在列表中,python,python-3.x,Python,Python 3.x,我想把操作符作为一个列表,然后调用列表中的一个元素作为操作符使用 如果没有在运算符周围加引号,则列表中的逗号会出现语法错误: File "p22.py", line 24 cat = [+,-,*] ^ SyntaxError: invalid syntax 如果我将引号放在周围,那么我似乎失去了操作符的功能,如本例所示: File "p22.py", line 30 a = (x which y) ^ Syntax
File "p22.py", line 24
cat = [+,-,*]
^
SyntaxError: invalid syntax
如果我将引号放在周围,那么我似乎失去了操作符的功能,如本例所示:
File "p22.py", line 30
a = (x which y)
^
SyntaxError: invalid syntax
以下是完整的代码:
import random
def start():
print('\n________________________________________')
print('| |')
print('| Zach\'s Tutorifier! |')
print('| |')
print('| |')
print('| Instructions: |')
print('| Select the type of math you want |')
print('| to practice with: |')
print('| 1-Add 2-Subtract 3-Multiply |')
print('|--------------------------------------|')
start()
math = int(input('> ')) - 1
cat = ['+','-','*']
def problems():
which = cat[math]
x = random.randint(0,9)
y = random.randint(0,9)
a = (x which y)
print('What is %i %s %i?' % (x, which, y) )
answer = input('> ')
if answer == a:
print('Congratulations! Try again? (Y/N)')
again = input('> ')
if again == 'Y' or again == 'y':
problems()
else:
start()
else:
print('Try again!')
problems()
为了正确地转换数学的字符串表示形式,您实际上可以使用内置运算符模块来执行此操作。简单地说,将字符串运算符映射到方法调用,并相应地工作。下面是一个示例,您应该能够了解如何使应用于代码:
from operator import add, sub, mul
operations = {'+': add, '-': sub, '*': mul}
op = input("enter +, - or *")
num1 = int(input("enter a number"))
num2 = int(input("enter another number"))
expected_result = int(input("what do you think the answer should be"))
result = operations[op](num1, num2)
if expected_result == result:
print('you are right')
else:
print('no, you are wrong')
要在此行中提供额外的信息,请执行以下操作:
operations[op](num1, num2)
operations
是一个字典,我们使用字典上的[]
访问值,方法是将输入的op
作为该字典的键传递。有了它,您现在就有了这个方法,只需要通过传递参数(num1
,num2
)来调用它 为了正确地转换数学的字符串表示形式,实际上可以使用内置运算符模块来完成此操作。简单地说,将字符串运算符映射到方法调用,并相应地工作。下面是一个示例,您应该能够了解如何使应用于代码:
from operator import add, sub, mul
operations = {'+': add, '-': sub, '*': mul}
op = input("enter +, - or *")
num1 = int(input("enter a number"))
num2 = int(input("enter another number"))
expected_result = int(input("what do you think the answer should be"))
result = operations[op](num1, num2)
if expected_result == result:
print('you are right')
else:
print('no, you are wrong')
要在此行中提供额外的信息,请执行以下操作:
operations[op](num1, num2)
operations
是一个字典,我们使用字典上的[]
访问值,方法是将输入的op
作为该字典的键传递。有了它,您现在就有了这个方法,只需要通过传递参数(num1
,num2
)来调用它 althougheval
可以使用,但他们说的是对的:除非是严格必要的,而且你没有其他选择,也没有最大的安全性,否则不要使用它。好吧,只是不要使用它,有一些方法可以做到这一点,尽管它们需要更多的代码
我的主张是映射操作符:
import random
# NEW CODE
def sum(a, b):
return a + b;
def substract(a, b):
return a - b;
def multiply(a, b):
return a * b;
# END OF NEW CODE
def start():
print('\n________________________________________')
print('| |')
print('| Zach\'s Tutorifier! |')
print('| |')
print('| |')
print('| Instructions: |')
print('| Select the type of math you want |')
print('| to practice with: |')
print('| 1-Add 2-Subtract 3-Multiply |')
print('|--------------------------------------|')
start()
math = int(input('> ')) - 1
cat = {
"+": sum,
"-": substract,
"*": multiply
}
def problems():
# NEW CODE
operator_char= cat.keys()[math]
operation = cat[math]
# END OF NEW CODE
x = random.randint(0,9)
y = random.randint(0,9)
print('What is %i %s %i?' % (x, operator_char, y) )
answer = input('> ')
# NEW CODE
if answer == operation(x, y):
# END OF NEW CODE
print('Congratulations! Try again? (Y/N)')
again = input('> ')
if again == 'Y' or again == 'y':
problems()
else:
start()
else:
print('Try again!')
problems()
尽管可以使用
eval
,但他们说的是对的:除非是严格必要的,并且您没有其他选择,并且您拥有最大的安全性,否则不要使用它。好吧,只是不要使用它,有一些方法可以做到这一点,尽管它们需要更多的代码
我的主张是映射操作符:
import random
# NEW CODE
def sum(a, b):
return a + b;
def substract(a, b):
return a - b;
def multiply(a, b):
return a * b;
# END OF NEW CODE
def start():
print('\n________________________________________')
print('| |')
print('| Zach\'s Tutorifier! |')
print('| |')
print('| |')
print('| Instructions: |')
print('| Select the type of math you want |')
print('| to practice with: |')
print('| 1-Add 2-Subtract 3-Multiply |')
print('|--------------------------------------|')
start()
math = int(input('> ')) - 1
cat = {
"+": sum,
"-": substract,
"*": multiply
}
def problems():
# NEW CODE
operator_char= cat.keys()[math]
operation = cat[math]
# END OF NEW CODE
x = random.randint(0,9)
y = random.randint(0,9)
print('What is %i %s %i?' % (x, operator_char, y) )
answer = input('> ')
# NEW CODE
if answer == operation(x, y):
# END OF NEW CODE
print('Congratulations! Try again? (Y/N)')
again = input('> ')
if again == 'Y' or again == 'y':
problems()
else:
start()
else:
print('Try again!')
problems()
<代码> >从运算符导入添加、子、MUL>代码>请考虑更改已接受的答案:(<代码>从运算符导入添加、子、MUL/CODE >请考虑更改已接受的答案:(而不是代码>操作[OP](NUM1,NUM2)< /代码>以获得有用的错误代码<代码>操作[OP](NUM1,NUM2)< /代码>以获得有用的错误