2009-12-15 12 views
1

Je ne vais poster la partie où le problème est à, le programme n'a pas d'erreur (tous les codes sont valables sauf pour ce problème raw_input)python problème d'entrée def raw_input

Je l'ai testé avec search_function(1) et etc et ça a marché. Mais si je fais cela en boucle, cela n'imprime rien. Exemple de sortie:

Entrez un numéro d'imprimer table spécifique, ou STOP pour quitter: 2 Entrez un numéro à impression table spécifique ou STOP pour quitter: 2 Entrez un numéro d'imprimer la table spécifique, ou STOP pour quitter: 1 Entrez un numéro pour imprimer table spécifique, ou STOP pour quitter: Entrez un numéro d'imprimer table spécifique ou STOP pour quitter: 1 Entrez un numéro d'imprimer table spécifique, ou STOP pour quitter : Entrez un nombre à imprimez tableau spécifique ou appuyez sur STOP pour quitter: STOP

def search_function(x): 
    if x == 1: 
     for student in students: 
      print "%-17s|%-10s|%-6s|%3s" % student.print_information() 
     print '\n' 

    if x == 2: 
     print "%-17s|%-10s|%s" %(header[0],header[1],header[4]) 
     print "-" * 45 
     for student in students: 
      print "%-17s|%-10s|%s" %student.print_first() 
     print '\n' 
     print "Simple Analysis on favorite sports: " 
     # Printing all sports that are specified by students 
     for s in set(Student.sports): # class attribute 
      print s, Student.sports.count(s), round(((float(Student.sports.count(s))/num_students) *100),1) 

     # Printing sports that are not picked 
     allsports = ['Basketball','Football','Other','Baseball','Handball','Soccer','Volleyball','I do not like sport'] 
     for s in set(allsports) - set(Student.sports): 
      print s, 0, '0%' 
     choice_list = Student.sports 
     for choice in choice_list: 
      choice_dict[choice] = choice_dict.get(choice, 0) + 1 
     print max(choice_dict) 
     print min(choice_dict) 

    elif x == 3: 
     print "%-17|%-10s|%-16s|%s" %(header[0],header[1],header[5],header[6]) 
     print "-" * 45 
     for student in students: 
      print "%-17s|%-10s|%-16s|%s" % student.print_second() 
     print '\n' 

    elif x == 4: 
     print "%-17s|%-10s|%s" %(header[0],header[1],header[7]) 
     print "-" * 45 
     for student in students: 
      print "%-17s|%-10s|%s" %student.print_third() 
     print '\n' 

    elif x == 5: 
     print "%-17s|%-10s|%-15s|%s" %(header[0],header[1],header[8],header[9]) 
     print "-" * 45 
     for student in students: 
      print "%-17s|%-10s|%-16s|%s" % student.print_fourth() 
     print '\n' 

x = raw_input("Enter a number to print specific table, or STOP to quit: ") 
while x != 'STOP': 
    search_function(x) 
    x = raw_input("Enter a number to print specific table, or STOP to quit: ") 

Répondre

1

test pour x == 'STOP' premier et break si vrai, d'autre CAST pour int et appeler search_function:

while True: 
    x = raw_input("Enter a number to print specific table, or STOP to quit: ") 
    if x == 'STOP': 
     break 
    search_function(int(x)) 
+0

merci, je l'ai utilisé. c'était bon – CppLearner

2

raw_input() chaîne de retour, alors que votre code attend entier. Utilisez search_function(int(x)) ou modifiez les conditions pour les comparer avec des chaînes.

+0

Merci pour le conseil. Cependant, une des raisons d'utiliser raw est de reconnaître l'entrée STOP, mais parce qu'elle est int, elle renvoie simplement une erreur. C'est bien pour mon but, mais je veux juste savoir, y a-t-il une alternative à cela? Je peux créer une instruction if/else supplémentaire, mais s'il vous plaît int, retournera juste l'erreur avant qu'elle frappe ma fausse déclaration – CppLearner

+0

Oui, il y a une alternative (quoting): "ou changer les conditions pour comparer avec les chaînes" = 1' à 'x == '1'' etc. Mais passer' int' dans 'search_function()' ne causera pas d'erreur, puisque la comparaison avec '' STOP'' est une fonction extérieure. –

+0

euhhh bonne idée. :) merci beaucoup – CppLearner