Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python条件打印格式_Python - Fatal编程技术网

Python条件打印格式

Python条件打印格式,python,Python,我有这样一个函数: def PrintXY(x,y): print('{:<10,.3g} {:<10,.3g}'.format(x,y) ) 但假设x和y不一定存在: >>> PrintXY(x, None) unsupported format string passed to NoneType.__format__ 在这种情况下,我不想打印任何内容,只想打印空白。我试过: def PrintXY(x,y): if y is None:

我有这样一个函数:

def PrintXY(x,y):
    print('{:<10,.3g} {:<10,.3g}'.format(x,y) )
但假设
x
y
不一定存在:

>>> PrintXY(x, None)
unsupported format string passed to NoneType.__format__
在这种情况下,我不想打印任何内容,只想打印空白。我试过:

def PrintXY(x,y):
    if y is None: 
        y = ''
    print('{:<10,.3g} {:<10,.3g}'.format(x,y) )

如果数字不存在,如何打印空白,如果数字存在,如何正确格式化?我不希望打印0或-9999来表示错误。

我已将其分离出来,以明确这些语句的作用。您可以将其合并为一行,但这会使代码更难阅读

def PrintXY(x,y):
    x_str = '{:.3g}'.format(x) if x else ''
    y_str = '{:.3g}'.format(y) if y else ''
    print('{:<10} {:<10}'.format(x_str, y_str))
另一种确保格式保持一致的方法是

def PrintXY(x,y):
    fmtr = '{:.3g}'
    x_str = fmtr.format(x) if x else ''
    y_str = fmtr.format(y) if y else ''
    print('{:<10} {:<10}'.format(x_str, y_str))
def PrintXY(x,y):
fmtr='{:.3g}'
x_str=fmtr.format(x)如果x else''
y_str=fmtr.格式(y),如果y为“其他”

print(“{:您可以使用不同的
print
命令,如下所示:

def PrintXY(x,y):
    if y is None: 
        print('{:<10,.3g}'.format(x) )
    else:
        print('{:<10,.3g} {:<10,.3g}'.format(x,y) )
def PrintXY(x,y):
如果y为无:
打印(“{:您可以尝试以下操作:

def PrintXY(x=None, y=None):        
    print(''.join(['{:<10,.3g}'.format(n) if n is not None else '' for n in [x, y]]))
def PrintXY(x=None,y=None):

print(“”.join(['{:您可以使代码更可读,更容易理解问题陈述中的条件,您也可以尝试以下方法:

def PrintXY(x,y):
    formatter = None

    if x is None and y is None:
        x, y = '', ''
        formatter = '{} {}'
    if x is None:
        y = ''
        formatter = '{} {:<10,.3g}'
    if y is None:
        x = ''
        formatter = '{:<10,.3g} {}'
    else:
        formatter = '{:<10,.3g} {:<10,.3g}'

    print(formatter.format(x,y))
def PrintXY(x,y):
格式化程序=无
如果x为无,y为无:
x、 y='','
格式化程序=“{}{}”
如果x为无:
y=''

格式化程序='{}{:由于灵活性、可读性和模块性,我认为这是最好的答案!是的,它可以扩展到任何数量的参数。我也喜欢这一个。
y='
。您缺少这一行的空格。这是一个好主意,但实际上我有两个以上的参数,所有参数都可以是
None
,组合的数量将是o需要实施的内容很多。
def PrintXY(x,y):
    if y is None: 
        print('{:<10,.3g}'.format(x) )
    else:
        print('{:<10,.3g} {:<10,.3g}'.format(x,y) )
def PrintXY(x=None, y=None):        
    print(''.join(['{:<10,.3g}'.format(n) if n is not None else '' for n in [x, y]]))
def PrintXY(x,y):
    formatter = None

    if x is None and y is None:
        x, y = '', ''
        formatter = '{} {}'
    if x is None:
        y = ''
        formatter = '{} {:<10,.3g}'
    if y is None:
        x = ''
        formatter = '{:<10,.3g} {}'
    else:
        formatter = '{:<10,.3g} {:<10,.3g}'

    print(formatter.format(x,y))