+ 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
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.
+ 6
What happens if you run it ?
+ 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.
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
0
Tried to write this