だから私はここで小さなアプリを作っています、そして私はtryブロックを持っています(ファイルがすでに存在するか、または作成されるべきかどうかを確認する必要があるため)。しかし...私のtryブロックは何らかの理由で繰り返されています!なぜこれが起こっているのか、私にはまったくわかり ません。助けてください?また、ファイルは正常に作成されます:)コード:
import sys
import time
Version = "V0.1"
def user():
PISBNdat = open("PISBN.dat", "w")
PISBNdat.write(Version)
cuser = raw_input("Please enter account username!")
for line in PISBNdat:
print "Test"
if cuser in line:
print("User already exists! Try again!")
user()
def start():
print "Hello and welcome to Plaz's PISBN!"
print "Opening file..."
time.sleep(0.8)
try:
fin = open("PISBN.dat", "r")
print "Success!"
fin.close()
user()
except:
time.sleep(0.5)
print "Did not recognize/find file!"
time.sleep(0.1)
print "Creating file!"
time.sleep(0.5)
try:
fout = open("PISBN.dat", "w")
print "Success!"
fout.close()
user()
except:
print "Failed!"
exit()
start()
そして、これが出力です...:
Hello and welcome to Plaz's PISBN!
Opening file...
Did not recognize/find file!
Creating file!
Success!
Please enter account username! [This is what I entered: Plazmotech]
Failed!
明らかに、「Failed!」と表示されているので、tryブロックを実行していることを意味します...それが「Failed!」を出力できる唯一の場所だからです。だからここで助けてください!