【问题标题】:printing format string function and variable (python)打印格式字符串函数和变量(python)
【发布时间】:2017-12-15 11:22:51
【问题描述】:

函数和变量的定义:

def secret_formula(started):
    jelly_beans = started * 500
    jars = jelly_beans / 1000
    crates = jars / 100
    return jelly_beans, jars, crates

start_point = 10000 / 10

打印声明:

print """crazy different style:
startpoint: %d\n
beans:\t\t %d\n
jars:\t\t %d\n
crates:\t\t %d\n
""" % (start_point, (secret_formula(start_point)))

我收到的错误消息是“ %d 格式:需要一个数字,而不是元组。 请帮我修复它。我真的是编程新手……还是无法将变量和被调用函数打包到同一个格式打印中?

【问题讨论】:

  • 您的问题陈述中就有解决方案。 " %d format: a number is required, not a tuple.also see about python string formatting.if you are new to Programming work up on your basics.任何关于 python 的像样的教程/网站都会有你想了解的关于 python 基本结构的东西。
  • 您似乎想将您的元组 (secret_formula(start_point)) 解压缩成几个参数。您可以使用解包操作符来做到这一点;即*(secret_formula(start_point))

标签: python string printing tuples formatted


【解决方案1】:

python 2 中的一个变体:

print """crazy different style:
startpoint: %d\n
beans:\t\t %d\n
jars:\t\t %d\n
crates:\t\t %d\n
""" % ((start_point, ) + secret_formula(start_point))

我通过将元组 (start_point, ) 添加到函数的结果中来创建一个新元组。


在 python 3 中你可以unpack the tuple with a *:

print("""crazy different style:
startpoint: %d\n
beans:\t\t %d\n
jars:\t\t %d\n
crates:\t\t %d\n
""" % (start_point, *secret_formula(start_point)))

【讨论】:

    【解决方案2】:

    考虑添加* 以解包元组(python 3.x 解决方案):

    print ("""crazy different style:
    startpoint: %d\n
    beans:\t\t %d\n
    jars:\t\t %d\n
    crates:\t\t %d\n
    """ % (start_point, *(secret_formula(start_point))))
    

    如果你使用的是 python 2.x 可以输入:

    start_point = 10000 / 10
    res = secret_formula(start_point)
    print """crazy different style:
    startpoint: %d\n
    beans:\t\t %d\n
    jars:\t\t %d\n
    crates:\t\t %d\n
    """ % (start_point, res[0], res[1], res[2])
    

    【讨论】:

      【解决方案3】:
      #According to zen of python Explicit is better than implicit, so
      
      bs, js, cs = secret_formula(start_point)
      
      print """crazy different style:
      startpoint: %d\n
      beans:\t\t %d\n
      jars:\t\t %d\n
      crates:\t\t %d\n
      """ % (start_point, bs, js, cs)
      

      【讨论】:

        【解决方案4】:

        在 python 3 上工作

        def secret_formula(started):
            jelly_beans = started * 500
            jars = jelly_beans / 1000
            crates = jars / 100
            return jelly_beans, jars, crates
        
        start_point = 10000 / 10
        
        print ("startpoint : {0} \n \
               beans:\t\t {1}  \n \
               jar :\t\t {2}".format(start_point,*secret_formula(start_point)[:2]))
        

        输出

        startpoint : 1000.0 
            beans:       500000.0  
            jar :        500.0
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-12-12
          • 2016-06-21
          • 1970-01-01
          • 2020-11-23
          • 2019-10-26
          • 1970-01-01
          • 2017-02-27
          • 1970-01-01
          相关资源
          最近更新 更多