teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

1

ptulos.txtへすべての回文を書き込みたいのですが…

2020/07/03 15:55

投稿

Chibichan
Chibichan

スコア72

title CHANGED
File without changes
body CHANGED
@@ -1,17 +1,21 @@
1
- ```tiedosto=open("pmalli.txt","r",encoding="utf-8")
1
+ ```def paaohjelma(word):
2
- while True:
2
+ while True:
3
- word=tiedosto.readline()
3
+ word=tiedosto.readline().rstrip()
4
- if word=="":
4
+ if word=="":
5
- break
5
+ break
6
- elif word==word[::-1]:
6
+ elif word==word[::-1]:
7
+ file=open("ptulos.txt","w",encoding="utf-8")
8
+ file.write(word) #ここに入れますと一番最後に出力された回文しかでません。
7
- print("Rivi ","'",word.strip(),"'"," on palindromi.",sep='')
9
+ print("Rivi ","'",word.strip(),"'"," on palindromi.",sep='')
8
- elif word!=word[::-1]:
10
+ file.close()
9
- print("Rivi ","'",word.strip(),"'"," ei ole palindoromi.",sep='')
10
- elif word==word.isdigit():
11
+ elif word.isdigit():
11
- print("Rivi ","'",word.strip(),"'"," on numerorivi.",sep='')
12
+ print("Rivi ","'",word.strip(),"'"," on numerorivi.",sep='')
13
+ else:
14
+ print("Rivi ","'",word.strip(),"'"," ei ole palindromi.",sep='')
15
+ return
16
+
17
+ tiedosto=open("pmalli.txt","r",encoding="utf-8")
18
+ paaohjelma(tiedosto)
12
19
  tiedosto.close()
13
20
 
14
- ```
21
+ ```
15
- ファイルを開けて、ファイル内にあるワードの回文判定をしますが、答えがすべて3番目のIF文(全部回文ではない)になってしまいます。4番目は、ワードが数字のみの場合は、数列で返します。
16
- どこがまちがっていますでしょうか?
17
- どうぞよろしくお願いいたします。