[英]How to make an item print three times then start a new row while keeping the name of user inputs
Currently using Python 3.5.1 当前使用Python 3.5.1
I've been trying to work on this code for hours. 我一直在努力处理此代码达数小时之久。 I need this for homework any tips or leads for anything im struggling tired and frustrated. 对于家庭作业,我需要此技巧或线索来解决陷入困境和沮丧的任何事情。
All I got was 我只有
What I need is HB HB HB 我需要的是HB HB HB
HB HB HB HB HB HB HB HB
This is my first programming class so all we really worked on was If, else, print, input, while, if i in range, and other basic python statements. 这是我的第一个编程类,因此我们真正要做的只是If,else,print,input,while,if i在范围内以及其他基本的python语句。
There are two aspects here: 这里有两个方面:
To get HB to print on the same line, you add additional arguments to the print function like this: 为了使HB在同一行上打印,您可以向打印函数添加其他参数,如下所示:
print('HB', end='') print('HB',end ='')
The default end is a new line, so this changes it to print on one line. 默认结尾是新行,因此将其更改为在一行上打印。
To have a new line added after every 3 prints, the easiest way is to use the modulus operator (%) so that you add a new line when division by 3 has a 0 remainder. 要在每3次打印后添加新行,最简单的方法是使用模数运算符(%),以便在被3除以0的余数时添加新行。
One way to achieve this for your example would be this: 对于您的示例,实现此目标的一种方法是:
num_times = int(input('How many times should display HB '))
for i in range(num_times):
print('HB ', end='')
if (i + 1)%3 == 0:
print('\n')
对于python 3,请在print函数中使用参数end=''
(空字符串)以防止更改行(请参阅文档https://docs.python.org/3/library/functions.html#print )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.