我用python3.5编写了以下代码。 问题是,我希望从用户处获取的值存储在excel中,当程序重新启动时,它会自动从保存的excel文件中读取所有数据。我肯定有办法做到这一点。请引导我。我几乎是Python的初学者。在
另外,请检查代码是否有错误。它有时运行得很好,有时当它要求重复而我说不,它仍然重复。在
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 )
我不确定这是否是一个复制问题,但对于最后的if语句,缩进似乎是错误的。除此之外,如果代码更易于阅读,那么调试逻辑问题会更容易——例如,在python中可以使用许多快捷方式来代替
你可以用
^{pr2}$此外,您不需要在while结束时使用continue语句—如果没有中断,它将自动继续。在
我还要确定哪个“重复”部分不起作用-你有几个看起来一样的,也许在重复问题中加上一个数字。在
我不知道你有什么逻辑问题,但希望上面的内容能帮你找到它。在
我给出了一个在python中读写excel文件的例子。请注意,在python中可以使用许多替代包来读写excel文件。查看here中所有可用的包。我给出了一个使用xlrd和xlwt的示例,我通常在读写excel文件时使用它们。在
从Excel文件读取:
写入Excel文件:
^{pr2}$网上有很多其他的例子,如果你需要更简单的例子,你可以看看。在
相关问题 更多 >
编程相关推荐