Jede Unterliste hat immer drei Elemente in der folgenden Reihenfolge:Warum gibt diese Funktion keine Werte zurück?
- Art der Mitgliedschaft: eine Zeichenfolge, die eine von
'Student'
,'Faculty'
oder'Visitor'
ist. - Fahrzeugtyp: eine Zeichenkette, die eine der
'Coupe'
,'Sedan'
,'SUV'
oder'Hybrid'
ist. - 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:
'Student'
: ersten drei Stunden sind frei'Faculty'
: erste 2 Stunden sind frei'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()
Kann ich fragen, warum Sie gerade nicht eine einzige Schleife? – Xatyrian
Ich habe es noch nicht gelernt, ich fange gerade an und das ist meine erste Klasse – AmatuerCoder
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