簡體   English   中英

我正在編碼PigLatin轉換器以調用文件?

[英]I'm coding a PigLatin converter to call a file?

我編碼了一個豬拉丁轉換器,向用戶詢問以獲取文件名並打開它,該轉換器將所有單詞轉換為豬拉丁並將所有字詞從字母更改為“ *”

VOWELS = ("a", "e", "i", "o", "u", "A", "E", "I", "O", "U")

f = open('textfile', 'w')

line = f

words =  line.split()

count = 0

    for word in words:
        for vowel in VOWELS:
            if vowel in word:
                # Find the first vowel that comes up
                i = word.find(vowel)
                #Found the first one, stop there!
                break
            else:
                # Set the error value again
                i = -1


def find_vowel(word):
    # This is from 0 to the length of the word
    for i in range(len(word)):
       if word[i] in VOWELS: 
         return i
    # Return an error value if there are not any (which solves another problem too!)
    return -1

    for vowel in file:
        if curlet.strip()==vowels:
            print('*')
    else:
        print (curlet,end='')

現在我得到這個錯誤:

"Traceback (most recent call last):
  File "D:/Python/Projects/piglatin1.py", line 12, in <module>
    words =  line.split()
AttributeError: '_io.TextIOWrapper' object has no attribute 'split'"

linef f是您的文件。 您不能拆分文件,只能拆分字符串。 你是說line = f.read()嗎? 那么line會被誤用,因為這將是文件中的所有數據,但是您的代碼中沒有任何東西會循環遍歷並一次處理它們。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM