You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将字符串形式的运算符转换为可执行的Python运算符?

解决方法

这里有几种靠谱的方式把运算符字符串转换成可执行的运算:

方法1:用operator模块+字典映射(推荐,安全清晰)

Python的operator模块提供了对应算术运算符的函数,我们可以用字典把运算符字符串和这些函数做映射:

首先要导入operator和random模块(原代码里用了randint,别忘了导入),然后修改代码如下:

import random
import operator
from easygui import buttonbox, integerbox  # 假设你用的是easygui的buttonbox/integerbox

t = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"]
t2 = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"]

number1 = int(t[random.randint(0, 9)])  
number2 = int(t2[random.randint(0, 9)])
operator_str = buttonbox("What do you want your questions to be?", choices=["+", "*", "/", "-"])
question = integerbox(f"What is {number1}{operator_str}{number2} ?")

# 建立运算符字符串到operator函数的映射
op_map = {
    "+": operator.add,
    "-": operator.sub,
    "*": operator.mul,
    "/": operator.truediv  # 如果要整数除法用operator.floordiv
}
# 计算正确结果
correct_result = op_map[operator_str](number1, number2)

# 注意:如果用integerbox,输入的是整数,除法可能得到浮点数,需要做类型兼容
if question == int(correct_result) if operator_str != "/" else question == correct_result:
    print("答对了!")
else:
    print(f"答错了,正确答案是{correct_result}")

方法2:用if-elif分支判断

如果不想导入operator模块,直接用条件判断:

import random
from easygui import buttonbox, integerbox

t = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"]
t2 = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"]

number1 = int(t[random.randint(0, 9)])  
number2 = int(t2[random.randint(0, 9)])
operator_str = buttonbox("What do you want your questions to be?", choices=["+", "*", "/", "-"])
question = integerbox(f"What is {number1}{operator_str}{number2} ?")

correct_result = 0
if operator_str == "+":
    correct_result = number1 + number2
elif operator_str == "-":
    correct_result = number1 - number2
elif operator_str == "*":
    correct_result = number1 * number2
elif operator_str == "/":
    correct_result = number1 / number2  # 或//做整数除法

if question == int(correct_result) if operator_str != "/" else question == correct_result:
    print("答对了!")
else:
    print(f"答错了,正确答案是{correct_result}")

方法3:用eval(不推荐在不可控输入场景使用,这里可选)

如果运算符是固定的几个选项(像你用buttonbox限定了范围),可以用eval直接执行字符串表达式,但要注意如果输入不可控会有安全风险:

import random
from easygui import buttonbox, integerbox

t = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"]
t2 = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"]

number1 = int(t[random.randint(0, 9)])  
number2 = int(t2[random.randint(0, 9)])
operator_str = buttonbox("What do you want your questions to be?", choices=["+", "*", "/", "-"])
question = integerbox(f"What is {number1}{operator_str}{number2} ?")

# 构造表达式字符串并执行
correct_result = eval(f"{number1}{operator_str}{number2}")

if question == int(correct_result) if operator_str != "/" else question == correct_result:
    print("答对了!")
else:
    print(f"答错了,正确答案是{correct_result}")

注意:除法运算时,integerbox获取的是整数输入,如果用真除法(/)会得到浮点数,需要做类型匹配;如果要整数除法,把/换成//即可。

内容的提问来源于stack exchange,提问作者pro coder

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.24 07:35:12