0
from datetime import datetime
from random import randint

def days_to_birthday(date):
    datetime_object = datetime.strptime(date, "%Y-%m-%d")
    date = datetime_object.date()
    num_days = date.timetuple().tm_yday
    return num_days

file_name = input("")
input_file =  open(file_name , "r")
output_file =  open("output.txt", "w")
year_list = []

for lines in input_file:
    
    char_info = lines.split(" ")
    char_name = char_info[0]
    b_day = char_info[1]
    bDay_list = b_day.split("-")
    b_year = bDay_list[0]
    num_of_days = int(days_to_birthday(b_day))
    gender = char_info[2]
            
    year_list.append(b_year)
    count = year_list.count(b_year)
    
    if gender == "F":
        days = num_of_days + 500
    else:
        days = num_of_days
        
    part_1 = b_year
    part_2 = f'{days:03d}'
    part_3 = f'{count:03d}'
        
    ID_num = part_1+str(part_2)+str(part_3)
    new_line = char_name, ID_num
    output_file.write(str(new_line))
    print(new_line[0], new_line[1])

when I give female info as the input it doesn't add that 500 I want it to. But when I input multiple female inputs on the row it adds the 500 to only the last female input

Here are the input file details I've used

Saman 1990-05-03 M  
Kumaran 1988-03-05 M  
Nazar 1997-09-24 M  
Ravindu 2001-03-21 M  
Nimasha 2001-08-10 F  
Aruni 1990-04-06 F  
Asha 2001-03-06 F  
mkrieger1
  • 19,194
  • 5
  • 54
  • 65
  • Welcome to Stack Overflow. Please read [ask] and the [formatting help](https://stackoverflow.com/help/formatting). Your English is fine (we can fix small mistakes as long as we understand you), but we need you to do more work in order to have a proper question. Please show us directly: when you use this input file, exactly what should the output be? Exactly what output do you get instead? Then, try to [check what the code does](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/), carefully, step by step. Does `gender` have the value you expect? How about `char_info`? – Karl Knechtel Oct 21 '22 at 11:05
  • As an aside, requiring the user to engage interactively with the script to provide the input file name prevents you from using this script from another script. It would make a lot more sense to read the input file name from `sys.argv[1]` (or use `fileinput`, or look at `argparse` for more sophisticated command-line parsing) and write the results to standard output. This way, you also get the benefits of your shell's interactive features, like searchable command history and file-name completion. – tripleee Oct 21 '22 at 11:08
  • 1
    There are two problems here that combine to cause the bug: reading a line from a file will include the trailing newline, and `.split(' ')` will leave that trailing newline on the last item. You can use `.split()` (no argument) to split a line on any kind of whitespace (which also strips out any whitespace around the items). Or you can remove the newline first. – Karl Knechtel Oct 21 '22 at 11:08
  • @KarlKnechtel Thanks a lot. It worked perfectly. You ended my suffering XD. – Ravidu Madhusanka Oct 21 '22 at 11:22

0 Answers0