简体   繁体   English

Python 格式化输出偶数和奇数

[英]Python formatting output of even and odd numbers

start = int(input("Enter a number: "))

for number in range (1, start + 1):
    if (number % 2 != 0):
        print("{0}".format(number, ','))

I have this code that works, but when it outputs, it does so in different lines.我有这个有效的代码,但是当它输出时,它会在不同的行中进行。 How to I get it to display as 1,3,5,...?如何让它显示为 1,3,5,...?

Save your results into an array, say results将您的结果保存到一个数组中,比如results

results = []

# your logic here

print(','.join(results))

When you print you can add the end argument:打印时,您可以添加end参数:

print({0}.format(number), end=",")

By default this is "\\n" which is a new line.默认情况下,这是“\\n”,这是一个新行。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM