Python Forum
Help with university work - reading and writing files
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with university work - reading and writing files
#1
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):
# -----------------------------------------------------------------------------------------------------------------------
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!
Reply


Messages In This Thread
Help with university work - reading and writing files - by MrKnd94 - Nov-01-2022, 04:27 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [split] University Assignment Help needed Nomathemba 4 4,135 Apr-20-2024, 12:24 PM
Last Post: jas31
Lightbulb Python University Laboratory - Help camibaldessini 3 1,482 Nov-03-2023, 01:28 PM
Last Post: Garrypyton
  reading text file and writing to an output file precedded by line numbers kannan 7 10,706 Dec-11-2018, 02:19 PM
Last Post: ichabod801
  University Assignment Help needed Diovanno 3 4,543 Apr-10-2018, 02:46 PM
Last Post: Diovanno

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020