Python formatting output of even and odd numbers

Submitted 3 years, 7 months ago
Ticket #116
Views 226
Language/Framework Python
Priority Medium
Status Closed

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,...?

Submitted on Sep 07, 20
add a comment

2 Answers

Verified

Try this, print function in python has a paramenter value "end" and its default value is always set to "\n" so we need to change that.

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

Submitted 3 years, 7 months ago

def find_odd_even_till_a_num(last_num):
    even_list = []
    odd_list = []
    num = 1
    while num <= last_num:
        if (num % 2) > 0:
            even_list.append(num)
        else:
            odd_list.append(num)
        num = num +1

    print(str(odd_list), "is ODD number")
    print(str(even_list), "is Even Number")


find_odd_even_till_a_num(100)

You can try this . May be it gives the desired output..

Submitted 3 years, 7 months ago

i think issue in formatting.

- Vengat 3 years, 7 months ago


Latest Blogs