小编Z M*_*onk的帖子

在Python 3中将运算符放在列表中

我想将运算符作为列表放置,然后从列表中调用一个元素作为运算符.

如果我没有在运算符周围放置引号,那么我在列表中的逗号中会出现语法错误:

  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)

python python-3.x

1
推荐指数
1
解决办法
145
查看次数

标签 统计

python ×1

python-3.x ×1