首页 > 解决方案 > 代码没有按照我想要的方式工作(python)

问题描述

def operator_updater(all_operators):
    for operator in all_operators:
        print(operator, sep=' ', end='', flush=True)

all_operators = ['|+|', '|-|', '|x|', '|/|']
op = input(f"Input Operator {operator_updater(all_operators)}: ")

#我希望它像这样打印输入运算符 |+| |-| |x| |/|:

#但这是结果--> |+| |-| |x| |/| 输入运算符无:

标签: pythonpython-3.x

解决方案


您的operator_updater函数在返回之前打印值。它也不会返回任何东西,这就是你看到 None 的原因。你可以做这样的事情,但可能有更好的方法去做。

def operator_updater(all_operators):
    result = ''
    for operator in all_operators:
        result += operator + ' '
    return result[:-1]


all_operators = ['|+|', '|-|', '|x|', '|/|']
op = input(f"Input Operator {operator_updater(all_operators)}: ")

推荐阅读