Home > front end >  Why does my code create an extra line in problem?
Why does my code create an extra line in problem?

Time:01-05

I am on day 4 of HackerRank's 30 days of code and I am having a problem where the output will create an extra line. I have checked other's code but they are incredibly similar to mine and i can't find the problem

class Person:
    def __init__(self,initialAge):
        # Add some more code to run some checks on initialAge
        if (initialAge > 0):
            self.initialAge = initialAge
        else:
            self.initialAge = 0
            print ("Age is not valid, setting age to 0")
    def amIOld(self):
        # Do some computations in here and print out the correct statement to the console
        if (self.initialAge < 13):
            print("You are young.")
        elif (self.initialAge >= 13 and self.initialAge < 18):
            print("You are a teenager")
        else: 
            print("You are old")
    def yearPasses(self):
        # Increment the age of the person in here
        self.initialAge = self.initialAge   1 
               
t = int(input())
for i in range(0, t):
    age = int(input())         
    p = Person(age)  
    p.amIOld()
    for j in range(0, 3):
        p.yearPasses()       
    p.amIOld()
    print("")

CodePudding user response:

I assume it's due to your last line. As print function will add a new line to the output and print("") will simply add a new line.

CodePudding user response:

The problem is the last statement. print("") will always print an empty string at the end of the for loop which will have an empty line at the end of the last iteration.

  • Related