2016-03-22 18 views
0

Bir .csv dosyasına ayrılan sonuçlarla bir sınavım var, sorular yanıtlandıktan sonra, kullanıcılardan sonuçların alfabetik olarak mı yoksa en düşüğe doğru en alta mı sorulması gerektiğini sorar. Bu soru daha sonra cevap girilirse tekrarlanır, ancak alfabetik olarak en az birkaç kez en düşük değere girilirse çalışır.Yinelenen bir csv içinde nasıl sıralanacağını sorma

print ("Would you like to see the results alphabetically or by highest to lowest?") 
alpha = input() 
while alpha != "alphabetically": 
    alpha = str(input ("Would you like to see the results alphabetically or by highest to lowest? ")) 
    break 
while alpha != "highest to lowest": 
    alpha = str(input ("Would you like to see the results alphabetically or by highest to lowest? ")) 
    break 


def updatefile(file,sortby,Classnumber): #this shortens the code by about 3 lines per file update 
if Class == Classnumber: 
    with open(file,'a') as f: 
     file_writer = csv.writer(f, delimiter = ',', lineterminator='\n') 
     file_writer.writerow((name,score)) 
     sortcsv(file,sortby) 

if alpha == "alphabetically": 
    updatefile('Class 1 Results.csv',0,"1") #saves space using shortened code, makes the code use alphabetical sorting 
    updatefile('Class 2 Results.csv',0,"2") 
    updatefile('Class 3 Results.csv',0,"3") 

elif alpha == "highest to lowest": 
    updatefile('Class 1 Results.csv',1,"1") #makes the code use highest to lowest sorting 
    updatefile('Class 2 Results.csv',1,"2") 
    updatefile('Class 3 Results.csv',1,"3") 
+0

I = alfabetik alfa sonrasında mola ekledik ve onu takip ne şimdi iki kez soruyu sorar ve ikinci kez bunu anlatıyor neyse. – SamH314

cevap

0

Tamam, hadi bu giriş bloğundan ilerleyelim ve neler olup bittiğine bakın. Tamam

print ("Would you like to see the results alphabetically or by highest to lowest?") 
alpha = input() 

şimdiye kadar, ancak bu print istemi bu sefer ed neden şaşırtıcı ve input() açıklamada kalanını yerleştirdi. Kullanıcı ilk isteminde "alfabetik" girmediyseniz yaptıkları kadar

while alpha != "alphabetically": 
    alpha = str(input ("Would you like to see the results alphabetically or by highest to lowest? ")) 

Şimdi, onlar tekrar tekrar istenir. Muhtemelen bunun için değil. (Ayrıca, input() etrafında str() gerekmez.)

while alpha != "highest to lowest": 
    highesttolowest = str(input ("Would you like to see the results alphabetically or by highest to lowest? ")) 
    break 

Şimdi kullanıcı nihayet "alfabetik" girdiğini, bunları tekrar istemi ("alfabetik" beri! = "En yüksekten en düşüğe"). Yine de burada iki büyük sorun var. İlk olarak, koşulsuz break, bir döngüden sonra her zaman çıkacağı için while döngüsünü anlamsız hale getirir. İkincisi, girdiyi highesttolowest numaralı yeni bir değişkene atayacaksınız, ancak daha sonra hala alpha numaralı sayfaya karşı test ediyorsunuz, bu nedenle bu komutun sonuçları hiçbir zaman kontrol edilmeyecek.

Yapmak istediğiniz şeyleri yapmanın en iyi yolu, her iki durumu da aynı anda kontrol etmek için üyelik testi kullanmaktır. o bütün bloğu değiştirin:

alpha = '' 
while alpha not in ("alphabetically", "highest to lowest"): 
    alpha = input("Would you like to see the results alphabetically or by highest to lowest? ") 
İlgili konular