第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

線路在錯誤的地方分裂

線路在錯誤的地方分裂

Smart貓小萌 2022-06-02 18:15:29
我在從“.txt”文件中拆分行時遇到問題。我希望該行在','.split() 函數(shù)具有參數(shù)時拆分每個字符。有人可以告訴我我做錯了什么以及如何解決甚至改進(jìn)它。那將不勝感激在我將 split() 函數(shù)移動到 FOR 循環(huán)之前,該行在正確的位置拆分,但是即使我輸入了正確的答案,它也不會將其識別為正確的答案,我嘗試將答案變成一個字符串檢查,但它沒有影響問題。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"預(yù)期的結(jié)果應(yīng)該允許我輸入另一側(cè)的內(nèi)容','并說它是正確的
查看完整描述

2 回答

?
aluckdog

TA貢獻(xiàn)1847條經(jīng)驗(yàn) 獲得超7個贊

而不是line.split(","),嘗試類似的東西line = line.split(","),因?yàn)槲艺J(rèn)為該split()函數(shù)返回列表,并且不會修改字符串本身。



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

TA貢獻(xiàn)1851條經(jīng)驗(yàn) 獲得超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()


查看完整回答
反對 回復(fù) 2022-06-02
  • 2 回答
  • 0 關(guān)注
  • 132 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號