从文件内容中读取和执行操作

Reading and performing actions from files contents

所以我正在尝试做这个实验,它为您提供来自 .txt 文件的大量信息。每行包含一些字母和数字,我应该编写代码来回答文本每一行的 5 个问题:使用小写字母打印 5 个字母序列,如果 5 个字母序列包含任何元音则打印,打印球体的体积具有第一个数字的半径,打印剩余 3 个数字是否可以组成三角形,并打印最后 3 个数字的平均值。

这里有 3 行 Lab5_Data.txt:

WLTQS 13 64 23 99

ZNVZE 82 06 53 82

TMIWB 69 93 68 65

这是这 3 行的预期输出:

小写的字符串是wltqs

WLTQS 不包含任何元音

半径为13的球体的体积为9202.7720799157

64,23,99不能组成三角形

64、23、99的平均值是62.0

小写字符串为znvze

ZNVZE 包含元音

半径为82的球体体积为2309564.8776326627

6,53,82不能组成三角形

06、53、82的平均值是47.0

小写的字符串是tmiwb

TMIWB 包含元音

半径为69的球体体积为1376055.2813841724

93、68、65可以组成三角形

93、68、65的平均值是75.33333333333333

到目前为止,这是我的代码:

with open('Lab5_Data.txt', 'r') as my_file:
    for line in my_file:
        data = line.split()
        print(line)

for line in my_file:
    line = line[0:5].lower()
    print('Thr string in lowercase is', line)

for i in my_file:
    if(i== 'A' or i== 'E' or i== "I" or i== 'O' or i== 'U'):
        print('contains vowels')
    else:
        print('does not contain any vowels')

我无法拆分每一行,因此当我打印输出时,它会在同一行中显示每个答案。此外,如果不做一个巨大的 for 循环,我很难尝试获得每个答案的函数。如果有人有任何意见可以帮助我,将不胜感激!

这里的主要思想是您可以逐行迭代文件,将行拆分为可用的数据块,然后将正确的块传递给不同的函数。这将是一个单一的大循环,但每个单独的计算都将在单独的函数中完成,因此有助于模块化。

现在,函数有很多方法,以及如何处理输出和打印:您可以避免在函数内部打印;您可以 return 整个答案字符串;您可以 return 计算值而不是文本字符串;您可以完全在外面处理琴弦;等。我想你明白了。

我会根据您的喜好进行定制。我不想让代码过于复杂,所以我尽可能保持简单和合理的可读性,有时会牺牲一些更多的 pythonic 方式。

# Here the functions that will be used later in the main loop
# These functions will calculate the answer and produce a string
# Then they will print it, and also return it

def make_lowercase(s):
    answer = s.lower()
    print(answer)
    return answer


def has_vowels(word):
    # For each vowel, see if it is found in the word
    # We test in lowercase for both
    answer = 'Does not contain vowels'
    for vowel in 'aeiou':
        if vowel in word.lower():
            answer = 'Contains vowels'
            break
    print(answer)
    return answer


def calculate_sphere_volume(radius):
    # Remember to convert radius from string to number
    import math
    volume = 4.0/3.0 * math.pi * float(radius)**3
    answer = "The volume of the sphere with radius {} is {}".format(radius, volume)
    print(answer)
    return answer


def check_possible_triangle(a1, b1, c1):
    # Remember to convert from string to number
    a = float(a1)
    b = float(b1)
    c = float(c1)
    answer = '{}, {}, {} can make a triangle'.format(a1, b1, c1)
    if (a + b <= c) or (a + c <= b) or (b + c <= a):
        answer = '{}, {}, {} cannot make a triangle'.format(a1, b1, c1)
    print(answer)
    return answer


def calculate_average(a1, b1, c1):
    # Remember to convert from string to number
    a = float(a1)
    b = float(b1)
    c = float(c1)
    average = (a + b + c) / 3
    answer = 'The average of {}, {}, {} is {}'.format(a1, b1, c1, average)
    print(answer)
    return answer

with open('Lab5_Data.txt', 'r') as my_file:    
    for line in my_file:
        # if you split each line, you get the individual elements
        # you can then group them appropriately, like so:
        # word: will contain the 5 letter string
        # radius: will be the first number, to be used for the sphere
        # a,b,c will be the three numbers to check for triangle
        # Important: THESE ARE ALL STRINGS!
        word, radius, a, b, c = line.split()

        # Now you can just use functions to calculate and print the results.
        # You could eliminate the prints from each function, and only use the
        # return results that will be combined in the end.

        # Lowercase:
        ans1 = make_lowercase(word)

        # Does this word have vowels?
        ans2 = has_vowels(word)

        # Calculate the sphere volume
        ans3 = calculate_sphere_volume(radius)

        # Check the triangle
        ans4 = check_possible_triangle(a, b, c)

        # Calculate average
        ans5 = calculate_average(a, b, c)

        # Now let's combine in a single line
        full_answer = '{} {} {} {} {}'.format(ans1, ans2, ans3, ans4, ans5)
        print(full_answer)