循环 - 多次打印,但在1行。 [英] While loop - print several times but on 1 line.
问题描述
你好。
我正在用Python创建一个小文本转换器。在程序中有一个
while循环。问题是while循环将有1个打印语句
并且它将循环直到它到达文本的末尾。
示例:
num = 5 //将num设置为5
而num> = 1://循环5次。
print""" ; text"""
num = num-1
// end。
程序输出将为:
text
text
(etc)
我怎么能做这个print:texttexttexttexttext?
我已经研究并浏览了谷歌,到目前为止我找不到
任何有用的东西(或者对此事情有所了解)。
>
感谢您的光临。
Hello there.
I''m creating a little text changer in Python. In the program there is a
while loop. The problem is that a while loop will have 1 print statement
and it will loop until it gets to the end of the text.
Example:
num = 5 // Set num to 5
while num >= 1: // loop 5 times.
print """text"""
num = num-1
// end.
The programs output will be:
text
text
(etc)
How could I make this print: texttexttexttexttext?
Ive researched and looked through google and so far I can''t find
anything that will help (or revelent for that matter).
Thanks for looking.
推荐答案
Danny写道:
Danny wrote:
你好。
我正在用Python创建一个小文本转换器。在程序中有一个
while循环。问题是while循环将有1个打印语句
它将循环直到它到达文本的末尾。
示例:
num = 5 // Set num为5
而num> = 1://循环5次。
print""" text"""
num = num-1
//结束。
Hello there.
I''m creating a little text changer in Python. In the program there is a
while loop. The problem is that a while loop will have 1 print statement
and it will loop until it gets to the end of the text.
Example:
num = 5 // Set num to 5
while num >= 1: // loop 5 times.
print """text"""
num = num-1
// end.
不要使用while,这被认为是unpythonix。使用带有
xrange的for循环代替。如果您需要按该顺序排列的nums,请使用带有
负步骤的xrange:
使用sys.stdout.write:
import sys
$ x $ b for x in xrange(num,0,-1):
sys.stdout.write(" text" )
Diez
Don''t use while for that, this is considered unpythonix. Use a for-loop with
xrange instead. If you need the nums in that order, use xrange with a
negative step:
Use sys.stdout.write:
import sys
for i in xrange(num, 0, -1):
sys.stdout.write("text")
Diez
Danny写道:
< snip>
<snip>
作为快捷方式:
打印" text" * 5
--- Heiko。
As a shortcut:
print "text"*5
--- Heiko.
我想我应该把一些程序代码粘贴一点我想要的东西......
var = 0
而var< = 5:
打印一个[t [var]]
var = var +1
a是一个代表(非常大),t是一串文本。 (如果那个'
现在重要的话)。
我只是想把[t [var]]的值打印出来在一条线上,如果那个
有意义......
对不起,如果我不是很好解释这个,如果我的例子不是
非常好,我正在尝试。
I think I should paste some of the programs code a little more of what I
want...
var = 0
while var <= 5:
print a[t[var]]
var = var +1
a is a dectionary (very big) and t is a string of text. (if that''s
important right now).
I''m just trying to make the value of a[t[var]] print on one line if that
makes any sense...
Sorry if I''m not explaining this very well and if my examples aren''t
very good, I am trying.
这篇关于循环 - 多次打印,但在1行。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!