简体   繁体   English

如何使函数在同一行上多次打印,例如print('test')* 3

[英]How to make a function print many times on the same line, e.g. print('test')*3

Let's say I have the code (although it doesn't do anything, just an example) 假设我有代码(尽管它什么也没做,只是一个例子)

def myprint():
    print("foobar")
foo = random.randint(1, 6)
myprint() * foo    #Obviously doesn't work

What I want it to do is carry out print() 'foo' times. 我要执行的操作是执行print() 'foo'次。 How do I do this? 我该怎么做呢?

You can change your code to this one: 您可以将代码更改为此:

def myprint(times):
    print(times*"foobar")
foo = random.randint(1, 6)
myprint(foo)

The "pythonic" way is with a for-loop: “ pythonic”方式是使用for循环的:

for n in range(foo):
    printfunc()

(Note that if you define print like you did in python 3, you're masking the original print with your own). (请注意,如果您像在python 3中一样定义打印,则将用自己的遮罩原始print )。

for i in range(foo):
    print('test')

If you really want to do it as an expression that returns something arbitrary (typically very bad, or at least extremely hackish, form)... 如果您真的想将其用作返回任意内容(通常非常糟糕,或者至少极其骇人听闻的形式)的表达式...

[print('test') for _ in range(foo)]

Or if you want to be obscure about it and annoy people reading your code: 或者,如果您想对此一目了然,并且使阅读代码的人烦恼:

import random
print('\n'.join(["foo" for _ in range(random.randint(1,6))]))

This is why you shouldn't call your own function print 这就是为什么您不应该调用自己的函数print

Python 3.2 (r32:88445, Dec  8 2011, 15:26:51) 
[GCC 4.5.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> help(print)

Help on built-in function print in module builtins:

print(...)
    print(value, ..., sep=' ', end='\n', file=sys.stdout)

    Prints the values to a stream, or to sys.stdout by default.
    Optional keyword arguments:
    file: a file-like object (stream); defaults to the current sys.stdout.
    sep:  string inserted between values, default a space.
    end:  string appended after the last value, default a newline.


>>> def print():
...     print("foobar")
... 
>>> help(print)

Help on function print in module __main__:

print()


>>>

oops now you have overwritten the builtin print so you can't use it to actually print stuff anymore 哎呀,现在您已经覆盖了内置print因此您无法再使用它来实际打印内容

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在一行中多次打印多行? - How to print many times in one line, with many rows? SQLAlchemy + Postgres(打印创建的类型,例如枚举类型) - SQLAlchemy + Postgres (print created types, e.g. enum types) 如何在Windows cmd上将不支持的unicode字符打印为“?”而不是引发异常? - How to print unsupported unicode characters on Windows cmd as e.g. “?” instead of raising exception? 为什么你必须在 PyCharm 中指定 print 来例如 print(data.head) - Why do you have to specify print in PyCharm to e.g. print(data.head) 在同一行 Python 上打印同一个单词 3 次 - Print the same word 3 times on the same line Python 如何打印某些内容,然后在同一行上调用打印功能? - How can I print something, and then call a function with a print on the same line? 使用 eval function 多次打印字符串 - print string many times using eval function 在同一行上多次打印字符串 - Print String multiple times on the same line OpenMaya API(python):从MObjects打印更多有用的信息? 例如MFloatVector(x,y,z) - OpenMaya API (python): print more useful info from MObjects? E.g. MFloatVector(x, y, z) 搜索特定的词性(例如名词)并将它们与前面的单词一起打印 - Search for particular parts of speech (e.g. nouns) and print them along with a preceding word
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM