【问题标题】:Reading and performing actions from files contents从文件内容中读取和执行操作
【发布时间】:2022-01-05 11:47:02
【问题描述】:

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

这是 Lab5_Data.txt 的 3 行:

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 循环的情况下获取每个答案的函数。如果有人有任何意见可以帮助我,将不胜感激!

【问题讨论】:

    标签: python for-loop with-statement txt


    【解决方案1】:

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

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

    我将根据您的喜好保留该级别的自定义。我不想让代码过于复杂,所以我保持尽可能简单和合理的可读性,有时会牺牲一些更 Python 的方式。

    # 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)
    

    【讨论】:

    • 哇,非常感谢您!这真的很有帮助。另一个快速的问题——我如何在每组答案之间放置一个空格?
    • 已经有一个了。无论如何,编辑full_answer 格式以获得您需要的内容。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-09-08
    • 2017-08-21
    • 2013-12-14
    • 1970-01-01
    • 2019-09-19
    • 2013-06-08
    • 1970-01-01
    相关资源
    最近更新 更多