2017-09-27 23 views
-1

Jede Unterliste hat immer drei Elemente in der folgenden Reihenfolge:Warum gibt diese Funktion keine Werte zurück?

  1. Art der Mitgliedschaft: eine Zeichenfolge, die eine von 'Student', 'Faculty' oder 'Visitor' ist.
  2. Fahrzeugtyp: eine Zeichenkette, die eine der 'Coupe', 'Sedan', 'SUV' oder 'Hybrid' ist.
  3. Dauer: die Anzahl der Stunden das Auto für Die Autos bieten kostenlosen Service für bis zu einer bestimmten Anzahl von Stunden nach der Art der Mitgliedschaft entlehnt wurde:
  4. 'Student': ersten drei Stunden sind frei
  5. 'Faculty': erste 2 Stunden sind frei
  6. 'Visitor': erste Stunde

kostenlos Nach dem kostenlosen Dienst vorbei ist, werden die Mitglieder auf der Art des Autos auf Basis berechneten sie gemietet haben:

  • Limousine: 10 $ pro Stunde
  • Coupe: 12 $ pro Stunde
  • SUV: 13 $ pro Stunde
  • Hybrid: $ 15 pro Stunde

bei Index 0 Geschäft des Gesamteinkommen von Sedans erzeugt . Bei Index 1 speichern Sie das von Coupes generierte Gesamteinkommen. Bei Index 2 speichern Sie das Gesamteinkommen der SUVs. Bei Index 3 speichern Sie die Gesamteinnahmen von Hybriden.

def car_rental(rentals): 
    car_type = [0, 0, 0, 0] 
    for rental in rentals: 
     if rental == 'Student' and car_type == "Sedan" and hours > 3: 
      income = 10 * (hours - 3) 
      car_type[0] += income 
    for rental in rentals: 
     if rental == 'Faculty' and car_type == "Sedan" and hours > 2: 
      income = 10 * (hours - 2) 
      car_type[0] += income 
    for rental in rentals: 
     if rental == 'Visitor' and car_type == "Sedan" and hours > 1: 
      income = 10 * (hours - 1) 
      car_type[0] += income 
    for rental in rentals: 
     if rental == 'Student' and car_type == "Coupe" and hours > 3: 
      income = 12 * (hours - 3) 
      car_type[1] += income 
    for rental in rentals: 
     if rental == 'Faculty' and car_type == "Coupe" and hours > 2: 
      income = 12 * (hours - 2) 
      car_type[1] += income 
    for rental in rentals: 
     if rental == 'Visitor' and car_type == "Coupe" and hours > 1: 
      income = 12 * (hours - 1) 
      car_type[1] += income 
    for rental in rentals: 
     if rental == 'Student' and car_type == "SUV" and hours > 3: 
      income = 13 * (hours - 3) 
      car_type[2] += income 
    for rental in rentals: 
     if rental == 'Faculty' and car_type == "SUV" and hours > 2: 
      income = 13 * (hours - 2) 
      car_type[2] += income 
    for rental in rentals: 
     if rental == 'Visitor' and car_type == "SUV" and hours > 1: 
      income = 13 * (hours - 1) 
      car_type[2] += income 
    for rental in rentals: 
     if rental == 'Student' and car_type == "Hybrid" and hours > 3: 
      income = 15 * (hours - 3) 
      car_type[3] += income 
    for rental in rentals: 
     if rental == 'Faculty' and car_type == "Hybrid" and hours > 2: 
      income = 15 * (hours - 2) 
      car_type[3] += income 
    for rental in rentals: 
     if rental == 'Visitor' and car_type == "Hybrid" and hours > 1: 
      income = 15 * (hours - 1) 
      car_type[3] += income 

    return car_type 

Getestet mit:

print("Testing car_rental() with rentals = [['Student','Coupe',4],['Faculty','Coupe',4],['Visitor','Coupe',4]]: " + 
     str(car_rental([['Student', 'Coupe', 4], ['Faculty', 'Coupe', 4], ['Visitor', 'Coupe', 4]]))) 
print("Testing car_rental() with rentals = [['Student','Coupe',4],['Faculty','SUV',4],['Visitor','Hybrid',4],['Visitor','Sedan',4]]: " + 
     str(car_rental([['Student', 'Coupe', 4], ['Faculty', 'SUV', 4], ['Visitor', 'Hybrid', 4], ['Visitor', 'Sedan', 4]]))) 
print("Testing car_rental() with rentals = [['Student','Coupe',3],['Faculty','SUV',2],['Visitor','Hybrid',1],['Visitor','Sedan',4]]: " + 
     str(car_rental([['Student', 'Coupe', 3], ['Faculty', 'SUV', 2], ['Visitor', 'Hybrid', 1], ['Visitor', 'Sedan', 4]]))) 
print() 
+3

Kann ich fragen, warum Sie gerade nicht eine einzige Schleife? – Xatyrian

+0

Ich habe es noch nicht gelernt, ich fange gerade an und das ist meine erste Klasse – AmatuerCoder

+0

Ich schlage vor, dass Sie einen Blick auf Ihre Vorlesungsnotizen werfen, weil es so viele Dinge in Ihrem Code gibt, zu denen Sie höchstwahrscheinlich niemanden finden werden helfe dir hier. Sehen Sie sich for loops und arrays an. Wenn du sie noch nicht hier hast, schlage ich vor, dass du dir die Zeit nimmst, um sie anzusehen oder auf eine nächste Klasse zu warten. – Xatyrian

Antwort

0

Das Problem der Art und Weise ist, dass Sie die Elemente des rentals Argument an die Funktion übergeben Referenzierung sind. Speziell in jeder der for rental in rentals:-Schleifen ist die rental Variable selbst auch eine list und Sie müssen ihren Inhalt mit der [] Indizierung Notation verweisen.

Hier ist eine feste Version des Codes:

def car_rental(rentals): 
    car_type = [0, 0, 0, 0] 
    for rental in rentals: 
     if rental[0] == 'Student' and rental[1] == "Sedan" and rental[2] > 3: 
      income = 10 * (rental[2] - 3) 
      car_type[0] += income 
    for rental in rentals: 
     if rental[0] == 'Faculty' and rental[1] == "Sedan" and rental[2] > 2: 
      income = 10 * (rental[2] - 2) 
      car_type[0] += income 
    for rental in rentals: 
     if rental[0] == 'Visitor' and rental[1] == "Sedan" and rental[2] > 1: 
      income = 10 * (rental[2] - 1) 
      car_type[0] += income 
    for rental in rentals: 
     if rental[0] == 'Student' and rental[1] == "Coupe" and rental[2] > 3: 
      income = 12 * (rental[2] - 3) 
      car_type[1] += income 
    for rental in rentals: 
     if rental[0] == 'Faculty' and rental[1] == "Coupe" and rental[2] > 2: 
      income = 12 * (rental[2] - 2) 
      car_type[1] += income 
    for rental in rentals: 
     if rental[0] == 'Visitor' and rental[1] == "Coupe" and rental[2] > 1: 
      income = 12 * (rental[2] - 1) 
      car_type[1] += income 
    for rental in rentals: 
     if rental[0] == 'Student' and rental[1] == "SUV" and rental[2] > 3: 
      income = 13 * (rental[2] - 3) 
      car_type[2] += income 
    for rental in rentals: 
     if rental[0] == 'Faculty' and rental[1] == "SUV" and rental[2] > 2: 
      income = 13 * (rental[2] - 2) 
      car_type[2] += income 
    for rental in rentals: 
     if rental[0] == 'Visitor' and rental[1] == "SUV" and rental[2] > 1: 
      income = 13 * (rental[2] - 1) 
      car_type[2] += income 
    for rental in rentals: 
     if rental[0] == 'Student' and rental[1] == "Hybrid" and rental[2] > 3: 
      income = 15 * (rental[2] - 3) 
      car_type[3] += income 
    for rental in rentals: 
     if rental[0] == 'Faculty' and rental[1] == "Hybrid" and rental[2] > 2: 
      income = 15 * (rental[2] - 2) 
      car_type[3] += income 
    for rental in rentals: 
     if rental[0] == 'Visitor' and rental[1] == "Hybrid" and rental[2] > 1: 
      income = 15 * (rental[2] - 1) 
      car_type[3] += income 

    return car_type 

print("Testing car_rental() with rentals = " 
     "[['Student','Coupe',4], ['Faculty','Coupe',4], ['Visitor','Coupe',4]]: " + 
     str(car_rental([['Student', 'Coupe', 4], ['Faculty', 'Coupe', 4], ['Visitor', 'Coupe', 4]]))) 

print("Testing car_rental() with rentals = " 
     "[['Student','Coupe',4], ['Faculty','SUV',4], ['Visitor','Hybrid',4], ['Visitor','Sedan',4]]: " + 
     str(car_rental([['Student', 'Coupe', 4], ['Faculty', 'SUV', 4], ['Visitor', 'Hybrid', 4], ['Visitor', 'Sedan', 4]]))) 

print("Testing car_rental() with rentals = " 
     "[['Student','Coupe',3],['Faculty','SUV',2],['Visitor','Hybrid',1],['Visitor','Sedan',4]]: " + 
     str(car_rental([['Student', 'Coupe', 3], ['Faculty', 'SUV', 2], ['Visitor', 'Hybrid', 1], ['Visitor', 'Sedan', 4]]))) 

Ausgang:

Testing car_rental() with rentals = [['Student','Coupe',4], ['Faculty','Coupe',4], ['Visitor','Coupe',4]]: [0, 72, 0, 0]

Testing car_rental() with rentals = [['Student','Coupe',4], ['Faculty','SUV',4], ['Visitor','Hybrid',4], ['Visitor','Sedan',4]]: [30, 12, 26, 45]

Testing car_rental() with rentals = [['Student','Coupe',3],['Faculty','SUV',2],['Visitor','Hybrid',1],['Visitor','Sedan',4]]: [30, 0, 0, 0]

Ein besserer Weg, es zu beheben wäre, GIV die lesbaren Namen für jedes der Elemente der Unterlisten rental am Anfang jeder verschachtelten Schleife for. Etwas in dieser Richtung:

def car_rental(rentals): 
    car_type = [0, 0, 0, 0] 
    for customer, style, hours in rentals: 
     if customer == 'Student' and style == "Sedan" and hours > 3: 
      income = 10 * (hours - 3) 
      car_type[0] += income 
    for customer, style, hours in rentals: 
     if customer == 'Faculty' and style == "Sedan" and hours > 2: 
      income = 10 * (hours - 2) 
      car_type[0] += income 
    etc ... 

Darüber hinaus Sie auch alle if Aussagen innerhalb einer großen for rental in rentals: Schleife setzen könnte.Dies wäre schneller und kürze die Menge an Code leicht benötigt:

def car_rental(rentals): 
    car_type = [0, 0, 0, 0] 
    for customer, style, hours in rentals: 
     if customer == 'Student' and style == "Sedan" and hours > 3: 
      income = 10 * (hours - 3) 
      car_type[0] += income 

     if customer == 'Faculty' and style == "Sedan" and hours > 2: 
      income = 10 * (hours - 2) 
      car_type[0] += income 

     etc ... 
+0

Danke und ich sehe, was ich falsch gemacht habe, und danke dir, dass du mir die Korrektur gezeigt hast. – AmatuerCoder

Verwandte Themen