+ 1

Can anyone please help with this below code for python files

Tom has done pull ups every day and recorded his results. He recorded each day's results in a new line, so that each line represents each day he has done pull ups. Create a program that takes n number as input and outputs the n-th days result (starting from 0). Sample Input 4 Sample Output Day 4, 9 pull ups

30th May 2024, 2:22 PM
Pankaj
Pankaj - avatar
5 Answers
+ 7
Pankaj please show us your attempt... The code itself doesn't appear to be difficult. We, the community, are not a "write a program" service. If you are having trouble we need to see what you have done so we can help you.
30th May 2024, 2:50 PM
BroFar
BroFar - avatar
+ 6
What happens if you run it ?
30th May 2024, 4:52 PM
Oma Falk
Oma Falk - avatar
+ 4
Pankaj , do you try to solve this task as an exercice from a python tutorial? if yes: only required outputs are allowed. in this case do *not* use try... except... or any error message. there is an issues in the output. this contains ...{result}... which is a string read from the file, so it contains both the day and the number of pull-ups. the line: `result = lines[day].strip()` should be split in the 2 parts: `day` and `number of pull-ups`. use this 2 elements like: ... result = lines[day].strip().split() return f"Day {result[0]}, {result[1]} pull ups" ... keep in mind that it is mentioned that day starts with 0.
30th May 2024, 8:25 PM
Lothar
Lothar - avatar
0
def get_pull_ups_for_day(day): try: # Open the file with pull-up results with open('pull_ups.txt', 'r') as file: # Read all lines into a list lines = file.readlines() # Check if the day exists in the list if day < len(lines): # Get the result for the specific day result = lines[day].strip() return f"Day {day}, {result} pull ups" else: return f"Day {day} result not found" except FileNotFoundError: return "The file 'pull_ups.txt' does not exist." except Exception as e: return f"An error occurred: {e}" U
30th May 2024, 3:06 PM
Pankaj
Pankaj - avatar
0
Tried to write this
30th May 2024, 3:07 PM
Pankaj
Pankaj - avatar