小编Den*_*gen的帖子

How to do math operations with string?

If a have the string calculation = '1+1x8'. How can I convert this into calculation = 1+1*8? I tried doing something like

for char in calculation:
    if char == 'x':
        calculation = calculation.replace('x', *)

    # and

    if char == '1':
        calculation = calculation.replace('1', 1)
Run Code Online (Sandbox Code Playgroud)

This clearly doesn't work, since you can't replace just one character with an integer. The entire string needs to be an integer, and if I do that it doesn't work either since I can't …

python math types for-loop calculator

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

标签 统计

calculator ×1

for-loop ×1

math ×1

python ×1

types ×1