我想将运算符作为列表放置,然后从列表中调用一个元素作为运算符.
如果我没有在运算符周围放置引号,那么我在列表中的逗号中会出现语法错误:
File "p22.py", line 24
cat = [+,-,*]
^
SyntaxError: invalid syntax
Run Code Online (Sandbox Code Playgroud)
如果我确实放置引号,那么我似乎丢失了运算符的函数,如下例所示:
File "p22.py", line 30
a = (x which y)
^
SyntaxError: invalid syntax
Run Code Online (Sandbox Code Playgroud)
这是完整的代码:
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 = …
Run Code Online (Sandbox Code Playgroud)