简体   繁体   English

如何使项目打印三遍,然后在保持用户输入名称的同时开始新的一行

[英]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 我只有

  • HB 乙肝
  • HB 乙肝
  • HB 乙肝
  • HB 乙肝

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: 这里有两个方面:

  1. printing 'HB' on the same line multiple times 在同一行上多次打印“ HB”
  2. adding a new line every 3 times HB prints 每3次HB打印添加一个新行

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.

相关问题 我如何让这个 for 循环每 8 次开始一个新行 - how do I make this for loop start a new line every 8 times 如何进行验证循环以确保用户在 a.split 输入中输入正确的次数? - How do I make a validation loop to make sure the user inputs the correct amount of times in a .split input? 如何使用 (while) 和 (try) 语句来处理来自三个选择之一的用户输入错误 - how to use (while) and (try) statements to handle errors of user inputs from one of three choices only 如何在保持程序化行选择的同时防止用户单击来选择行? - how to prevent row selection by user's click while keeping programmatic row selection? 如果字符串中的字符连续出现 3 次,如何使 output 返回 False? - How can I make the output return False if a character in the string appears three times in a row? 如何使 pandas dataframe 在迭代时在不同的行开始迭代? - how to make pandas dataframe start iterating at a different row while iterating? 在python中使用.writelines时,如何使代码在文档的新行上添加新输入? - How do I make my code add new inputs on a new line in a document while using the .writelines in python? “如何在一行上打印多个用户输入” - “how to print multiple user inputs on one line” 如何打印只要用户输入的水平线 - How to print a horizontal line that is as long as the user inputs it 如何在多次运行代码时不添加另一个相同的行来创建 SQLlite 行? - How to make a SQLlite row without adding another identical row while running the code multiple times?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM