PRG-105: Working with Numbers in .txt Files in Python

preview_player
Показать описание
Python: Read in a file of blood sugars, convert to int values, determine if the values are high or low and add them to the appropriate accumulator.

View an annotated PDF transcription of the video:
Рекомендации по теме
Комментарии
Автор

thank u for your beautiful tutorial. I am stuck. I get the following error message.
'high is not defined'... please help

def main():
high = 0
low = 0

input_file = open('marks.txt', 'r')

record = input_file.readline()
record = record.rstrip('\n')

while record != "":
record = int(record)
if record > 50:
high += 1
if record < 50:
low += 1

record = input_file.readline()
record = record.rstrip('\n')


print("There were" + high + 'blood sugar readings above 50' )
input_file.close()

main()

vivanshetty
Автор

Or you can use an f string and put high into {} so print(f"some text {high} some more text")

silentraccoon
Автор

Should have used the '+' before recording the video, it would have been shorter and more concise. Thanks though, very helpful.

DanielADickey