Kombination af Positionelle og Valgfrie Argumenter
Overvej en funktion designet til at beregne den samlede pris for smartphones, hvor det er muligt at angive forskellige attributter både under initialisering og ved funktionskald.
123456789101112131415161718192021222324def calculate_smartphone_cost(model, price, quantity=1, discount=0): total_cost = price * quantity discount_amount = total_cost * (discount / 100) discounted_cost = total_cost - discount_amount print(f"Model: {model}") print(f"Unit price: ${price}") print(f"Quantity: {quantity}") print(f"Total cost before discount: ${total_cost}") if discount > 0: print(f"Discount: {discount}%") print(f"Discount amount: ${discount_amount}") print(f"Discounted cost: ${discounted_cost}") else: print("No discount applied.") print(f"Final cost: ${discounted_cost}") print() # Examples of using the function calculate_smartphone_cost("iPhone 13", 1099, 2) calculate_smartphone_cost("Samsung Galaxy S21", 999, 1, 10) calculate_smartphone_cost("Google Pixel 6", 799, quantity=3, discount=5)
Regler for angivelse af argumenter
Positionelle argumenter
Positionelle argumenter skal følge rækkefølgen i funktionsdefinitionen. I calculate_smartphone_cost er model og price påkrævede positionelle argumenter.
Valgfrie (navngivne) argumenter
Valgfrie argumenter kan angives positionelt eller ved navn. quantity og discount har standardværdier, som kan ændres ved brug af navngivne parametre.
Standardværdier
Hvis et valgfrit argument udelades, anvendes dets standardværdi. I eksemplet er standardværdien for quantity 1 og for discount 0.
Navngivne parametre
Navngivne parametre øger tydeligheden ved eksplicit at tildele værdier, især når der findes flere valgfrie argumenter.
Disse regler viser, hvordan kombinationen af positionelle og navngivne argumenter gør funktioner fleksible og læsbare.
1. Hvad er kombinationen af positionelle og valgfrie argumenter i funktioner?
2. Hvordan definerer man en funktion med positionelle argumenter efterfulgt af valgfrie argumenter?
Tak for dine kommentarer!
Spørg AI
Spørg AI
Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat
Can you explain how to use named parameters in this function?
What happens if I omit the optional arguments?
Can you show more examples with different argument combinations?
Fantastisk!
Completion rate forbedret til 4.17
Kombination af Positionelle og Valgfrie Argumenter
Stryg for at vise menuen
Overvej en funktion designet til at beregne den samlede pris for smartphones, hvor det er muligt at angive forskellige attributter både under initialisering og ved funktionskald.
123456789101112131415161718192021222324def calculate_smartphone_cost(model, price, quantity=1, discount=0): total_cost = price * quantity discount_amount = total_cost * (discount / 100) discounted_cost = total_cost - discount_amount print(f"Model: {model}") print(f"Unit price: ${price}") print(f"Quantity: {quantity}") print(f"Total cost before discount: ${total_cost}") if discount > 0: print(f"Discount: {discount}%") print(f"Discount amount: ${discount_amount}") print(f"Discounted cost: ${discounted_cost}") else: print("No discount applied.") print(f"Final cost: ${discounted_cost}") print() # Examples of using the function calculate_smartphone_cost("iPhone 13", 1099, 2) calculate_smartphone_cost("Samsung Galaxy S21", 999, 1, 10) calculate_smartphone_cost("Google Pixel 6", 799, quantity=3, discount=5)
Regler for angivelse af argumenter
Positionelle argumenter
Positionelle argumenter skal følge rækkefølgen i funktionsdefinitionen. I calculate_smartphone_cost er model og price påkrævede positionelle argumenter.
Valgfrie (navngivne) argumenter
Valgfrie argumenter kan angives positionelt eller ved navn. quantity og discount har standardværdier, som kan ændres ved brug af navngivne parametre.
Standardværdier
Hvis et valgfrit argument udelades, anvendes dets standardværdi. I eksemplet er standardværdien for quantity 1 og for discount 0.
Navngivne parametre
Navngivne parametre øger tydeligheden ved eksplicit at tildele værdier, især når der findes flere valgfrie argumenter.
Disse regler viser, hvordan kombinationen af positionelle og navngivne argumenter gør funktioner fleksible og læsbare.
1. Hvad er kombinationen af positionelle og valgfrie argumenter i funktioner?
2. Hvordan definerer man en funktion med positionelle argumenter efterfulgt af valgfrie argumenter?
Tak for dine kommentarer!