简体   繁体   English

“如何修改以下程序,使其以特定格式打印?”

[英]“How can I modify the below program so it prints in a specific format?”

I am trying to modify the below program so it prints the string in the following format 我正在尝试修改以下程序,因此它将以以下格式打印字符串

input : Fred, 4 输入Fred, 4

output : 输出

Fred

Fred Fred

Fred Fred Fred

Fred Fred Fred Fred

There are 3 mistakes in this code: 此代码中有3个错误:

def prlines(str, num):

    n in range(0,num):

    print (str (n + 1))


prlines()

So I already tried the following code: 所以我已经尝试了以下代码:

name=str(input('Enter a name:'))  
num=int(input('Enter a number:'))  

 def prlines(str, num):  

    for n in range(0, num):

      print(str*(n + 1))

 print(prlines(name, num))

I get this Actual Results (eg with the Name 'Fred' and the Number '4'): 我得到以下实际结果(例如,名称为“ Fred”,数字为“ 4”):

Fred

FredFred

FredFredFred

FredFredFredFred

None

My question is How can I get the expected results to display with the following constraints: 我的问题是如何获得预期的结果并显示以下约束:

  • without None at the end 最后没有没有

  • with spaces between the strings 字符串之间有空格

You can use: 您可以使用:

f'{s} ' * n + f'{s}'

Code : 代码

name = input('Enter a name: ')
num = int(input('Enter a number: '))

def prlines(s, num):
    for n in range(num):
        print(f'{s} ' * n + f'{s}') 

prlines(name, num)

Few points : 几点

  • Don't name variable as str . 不要将变量命名为str

  • A function by default with no return , returns None , hence you have a None at the end. 默认情况下,不带return函数返回None ,因此结尾处为None

First 第一

Code Analysis 代码分析

name=str(input('Enter a name:'))  
num=int(input('Enter a number:'))  

def prlines(str, num):  

    for n in range(0, num):

        print(str*(n + 1)) # No spaces added = No spaces on the output

print(prlines(name, num)) # You are printing the output of the above function, which is None since it has no "return"

The print(func()) is where you get your None from print(func())是您从中获得None的地方

Since you are only printing the str multiple times, you didn't include spaces (python won't know :) 由于您仅多次打印str,因此不包含空格(python不会知道:)

Answer 回答

name=str(input('Enter a name:'))  
num=int(input('Enter a number:'))  

def prlines(str, num):  
    for n in range(0, num):
        print((str+' ')*(n + 1))

prlines(name, num)

Advice 忠告

This code is quite rough, you would need to think about error handling when someone doesn't input an int in the second input. 这段代码很粗糙,当有人在第二个输入中未输入int时,您需要考虑错误处理。

暂无
暂无

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

相关问题 如何修改以下程序,以确保替换的每个字母都是唯一的? - How can I modify the following program so that I can make sure that each letter it replaces is unique? 我现在可以用打开的 cv 检测到蓝色,如果程序检测到蓝色它会打印一些东西,我该如何做到这一点? - I can now detect the colour blue with open cv, How do I make it so now if the program detects blue it prints something? 如何将特定用户分配给用户上传的文件,以便他们可以对其进行修改/删除(Django + Apache) - How do I assign specific users to a user-uploaded file so they can modify it/delete it (Django + Apache) 我如何获得该程序打印的所有数字的总和? - How can i get sum of all the numbers that this program prints? 如何在python3.x中格式化多个打印的数字? - How can I format a number for multiple prints in python3.x? 我将如何制作它,以便我可以在该程序开始时询问用户是否想要执行它,因为下面的代码会带来语法错误 - How would I make it so I can ask the user at the start of this program in the want to execute it because the below code brings back syntax errors Python 3:如何在打印时对齐此数据的格式 - Python 3: How can I align the the format of this data when it prints python:我有下面的代码,但是它反向打印,我该如何反转 - python: I have this code (below) but it prints in reverse, how can I reverse it 如果没有三个数字,如何格式化整数以便在它之前打印空格 - how to format integers so that it prints spaces before it if there are not three number 没有属性“编译”,我如何修改类,使其工作? - No attribute 'compile', how can I modify the class, so that it works?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM