Nov-01-2022, 04:27 PM
Hello,
Me and a lot of my classmates are so confused in today's work.
The answer sheet was published, but that didn't help us at all.
Here's what they want:
Write a Python function named extract_temp that is given a line read from a text file and displays the one number (integer) found in the string:
'The high today will be 15 degrees' → 15.
Here's my code (helped by the answer sheet):
Apparently, we just want the number "15" outputted, with None I think. I'm not quite sure.
Any help would be greatly appreciated.
Thank you!
Me and a lot of my classmates are so confused in today's work.
The answer sheet was published, but that didn't help us at all.
Here's what they want:
Write a Python function named extract_temp that is given a line read from a text file and displays the one number (integer) found in the string:
'The high today will be 15 degrees' → 15.
Here's my code (helped by the answer sheet):
# ----------------------------------------------------------------------------------------------------------------------- def extract_temp(s): for n in range(0,len(s)): if s[n].isdigit(): ss = s[n:] for n1 in range(0,len(ss)): if not ss[n1].isdigit: snum = s[n:n+1] num=int(snum) return num # ----------------------------------------------------------------------------------------------------------------------- input_file = open('ExtractTemp.txt','r') line = input_file.readline() number = extract_temp(line) print(line.strip(), "-", number)The output:
Quote:The high today will be 15 degrees - None
Apparently, we just want the number "15" outputted, with None I think. I'm not quite sure.
Any help would be greatly appreciated.
Thank you!