亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

線路在錯誤的地方分裂

線路在錯誤的地方分裂

Smart貓小萌 2022-06-02 18:15:29
我在從“.txt”文件中拆分行時遇到問題。我希望該行在','.split() 函數具有參數時拆分每個字符。有人可以告訴我我做錯了什么以及如何解決甚至改進它。那將不勝感激在我將 split() 函數移動到 FOR 循環之前,該行在正確的位置拆分,但是即使我輸入了正確的答案,它也不會將其識別為正確的答案,我嘗試將答案變成一個字符串檢查,但它沒有影響問題。Pythondef main():  file = open ("Spanish Words Randomized.txt", "r")  line = file.readline()  for line in file:    line.split(",")    answer = input("What is '" + line[0] + "' in Spanish?:")    if answer == str(line[1]):       print("correct")    elif answer != str(line[1]):        print("It was",line[1])main()這些是 .txt 文件的前 3 行"A shanty town,Un barrio de chabolasManagers,Los gerentesRunning water,El agua corriente"預期的結果應該允許我輸入另一側的內容','并說它是正確的
查看完整描述

2 回答

?
aluckdog

TA貢獻1847條經驗 獲得超7個贊

而不是line.split(","),嘗試類似的東西line = line.split(","),因為我認為該split()函數返回列表,并且不會修改字符串本身。



查看完整回答
反對 回復 2022-06-02
?
江戶川亂折騰

TA貢獻1851條經驗 獲得超5個贊

試試這個:


def main():


    with open ("Spanish Words Randomized.txt", "r") as fin :

        # read the whole file, stripping EOL and filtering the empty lines

        text = [line.strip() for line in fin.readlines() if len(line) > 1]


    for line in text :

       q, a = line.split(",")    # need to catch the results of the split()

       answer = input("What is '" + q + "' in Spanish?:")

       if answer == a :

           print("correct")

       else :    # no point to check what we already know

           print("It was", a)


main()


查看完整回答
反對 回復 2022-06-02
  • 2 回答
  • 0 關注
  • 134 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號