3

I'm very new to programming and just made an addition loop that will, once age is given, count up from one only stopping when it hits the user's age (e.g. if I say I am 15, the computer will then count up from one until it hits 15).

My current goal is to try and get it to print out ONLY the odd or even numbers until it hits the age. Odd if the age is odd, even if the age is even.

age = int(input("What is your age?: "))
count = 1

while count != age:
    print(count)
    count += 1

    if count == age:
        print(age)
        break

I've been looking for a long time about how I can print out only these numbers, but I haven't been able to find anything that works outside of a list. I just need some help on applying it to this program. I had written something like this, but I couldn't get it to work with this specific program. I'm having trouble putting it into this one.

if age % 2 != 0: 
       print(age, end = " ") 

Part of this is probably me being too new to be able to do this, and I think that the code right above only works if it has a list to look at. Any tips putting me in the right direction are much appreciated.

spirebyte
  • 73
  • 5

4 Answers4

5

First, read how to debug small programs. This is a crucial skill for a programmer, and the sooner into your programming career you learn it, the easier your life will be.

Second, when you do while count != age, it already checks to make sure that count is not equal to age before it does the loop once more. There is no need for if count == age: break

Third, if you only want to print when a certain condition is satisfied, put the print inside the corresponding if.

Fourth, you want to check if count % 2 is the same as age % 2

Finally, read about truthy and falsy values in Python. if age % 2 != 0 is identical to writing if age % 2.

Incorporating all these, we have:

while count != age:
    if count % 2 == age % 2:
        print(count)
    count += 1

Of course, there are more efficient ways of doing this: for example, you could start at an odd or even number depending on whether your age is odd or even, and then simply count up by 2 every time.

Pranav Hosangadi
  • 23,755
  • 7
  • 44
  • 70
3

Set a variable to the modulus of age with 2. Then you can compare that with the modulus of each number as you're counting up.

age = int(input("What is your age?: "))
age_mod = age % 2
for i in range(1, age+1):
    if i % 2 == age_mod:
        print(i)

Another way is to count by 2, and set the starting number depending on whether the age is odd or even.

age = int(input("What is your age?: "))
if age % 2 == 0:
    start = 2
else:
    start = 1
for i in range(start, age+1, 2):
    print(i)
Barmar
  • 741,623
  • 53
  • 500
  • 612
1

if I stick to your version of code, so this might work (your problem was that you checked age instead of count):

age = int(input("What is your age?: "))
count = 1

while count <= age:
    if (count%2) != 0: 
        print(count)
    count += 1

better version is:

age = int(input("What is your age?: "))
ages = [*range(1, age+1, 2)]
ages = [str(item) for item in ages]
print ('\n'.join(ages))

good luck!

Yossi Levi
  • 1,258
  • 1
  • 4
  • 7
1

You could do it like this:

First detect if the age is even or not.

If it is even, print out all the even counts with with the if statement.

If it is not even print all the counts which are not even.

age = int(input("What is your age?: "))
count = 1

for i in range(1, age):
    if(age % 2) == 0:
        if (count % 2) == 0:
            print(count)
        count +=1
    else:
        if(count % 2) != 0:
            print(count)
        count +=1
Yven Ven
  • 36
  • 4