我已經在Python 3.5中編寫了下面的代碼。 問題是我想從用戶獲取的值存儲在Excel中,當程序重新啓動時,它會自動讀取保存的Excel文件中的所有數據。我確信有一些方法可以做到這一點。請指導我。我幾乎是Python的初學者。Python數據導出/導入到/從Excel文件
也請檢查錯誤代碼。它有時會完美運行,有時它會重複,我說不,它仍然會重複。
name=[]
roll=[]
grade=[]
omarks=[]
tmarks=[]
p=-1
while True:
st=input("Do You want to add a Data or Search the Existing Data? \n1=Search \n2=Add \n3=Print All Data???\n")
while st=="3" or st=="P" or st=="Print" or st=="print" or st=="p":
for i in range (len(name)):
print ("Name of the Student: ",(name[i]))
print ("Roll Number:",(roll[i]))
print ("Class of the Student:",(grade[i]))
print (" ")
rpp=input("Repeat?? Y?N: ")
if rpp=="Y" or rpp=="y" or rpp== "Yes" or rpp=="YES" or rpp=="yes" or rpp=="1":
continue
if rpp=="N" or rpp=="n" or rpp== "No" or rpp=="NO" or rpp=="no" or rpp=="0":
break
while st=="2" or st=="A" or st=="Add" or st=="add" or st=="a":
nm=str(input("Enter The Name Here: "))
rl=int(input("Enter The Roll Number Here: "))
gr=int(input("Enter The Class Number Here: "))
om=int(input("Enter the Obtained Marks Here: "))
tm=int(input("Enter the Total Marks Here: "))
name.append(nm)
roll.append(rl)
grade.append(gr)
omarks.append(om)
tmarks.append(tm)
rpa=input("Add Another?? Y?N: ")
if rpa=="Y" or rpa=="y" or rpa== "Yes" or rpa=="YES" or rpa=="yes" or rpa=="1":
continue
if rpa=="N" or rpa=="n" or rpa== "No" or rpa=="NO" or rpa=="no" or rpa=="0":
break
while st=="1" or st=="S" or st=="Search" or st=="s" or st== "search":
rn=int(input("Enter the Roll Number Here: "))
for i in range (len(roll)):
if rn==roll[i]:
#p=i
print ("Name of the Student: ",name[i])
print ("Class of the Student: ",grade[i])
print ("Obtained Marks: ",omarks[i])
print ("Total Marks: ",tmarks[i])
else:
print ("The Roll Number Could not be Found! Search Again or Add the Data.")
rps=input("Search Again?? Y?N: ")
if rps=="Y" or rps=="y" or rps== "Yes" or rps=="YES" or rps=="yes" or rps=="1":
continue
elif rps=="N" or rps=="n" or rps== "No" or rps=="NO" or rps=="no" or rps=="0":
break
rp=input("Repeat The Program?? Y?N: ")
if rp=="Y" or rp=="y" or rp== "Yes" or rp=="YES" or rp=="yes" or rp==1:
continue
if rp=="N" or rp=="n" or rp== "No" or rp=="NO" or rp=="no" or rp==0:
break
try:
untrusted.execute()
except: # catch *all* exceptions
e = sys.exc_info()[0]
write_to_page("<p>Error: %s</p>" % e)
它更好,如果你開始學習如何調試代碼。從你的問題規範看來,你在打破循環方面存在問題,所以仔細檢查那些不符合你期望的條件。 –
將rp與1或0進行比較之前的一件事情,將rp轉換爲整數作爲'int(rp)',否則它將不適用於輸入1或0. –
實際上命名變量有意義的名稱可能也有幫助... – Lunaweaver