Modifizieren Sie die Datei tuple_length.py
, um den folgenden Code einzufügen:
def check_tuple_length(my_tuple, desired_length):
"""
Checks if the length of a tuple matches the desired length.
"""
if len(my_tuple) == desired_length:
print("The tuple has the desired length.")
else:
print("The tuple does not have the desired length.")
## Example usage:
my_tuple = (1, 2, 3)
desired_length = 3
check_tuple_length(my_tuple, desired_length)
another_tuple = ("a", "b", "c", "d")
desired_length = 3
check_tuple_length(another_tuple, desired_length)
In diesem Code definieren wir eine Funktion namens check_tuple_length
, die zwei Argumente akzeptiert: ein Tupel (my_tuple
) und eine gewünschte Länge (desired_length
). Die Funktion verwendet die len()
-Funktion, um die Länge des Tupels zu erhalten und vergleicht sie dann mit der gewünschten Länge. Wenn die Längen übereinstimmen, wird "The tuple has the desired length." ausgegeben. Andernfalls wird "The tuple does not have the desired length." ausgegeben.
Anschließend geben wir zwei Beispiele für die Verwendung der Funktion. Im ersten Beispiel erstellen wir ein Tupel my_tuple
mit drei Elementen und setzen die desired_length
auf 3. Im zweiten Beispiel erstellen wir ein Tupel another_tuple
mit vier Elementen und setzen die desired_length
ebenfalls auf 3.
Die vollständige tuple_length.py
-Datei sollte jetzt wie folgt aussehen:
## Tuple of strings
string_tuple = ("apple", "banana", "cherry")
string_length = len(string_tuple)
print("The length of the string tuple is:", string_length)
## Tuple of mixed data types
mixed_tuple = (1, "hello", 3.14, True)
mixed_length = len(mixed_tuple)
print("The length of the mixed tuple is:", mixed_length)
## Empty tuple
empty_tuple = ()
empty_length = len(empty_tuple)
print("The length of the empty tuple is:", empty_length)
## Tuple of numbers
number_tuple = (1, 2, 3, 4, 5)
number_length = len(number_tuple)
if number_length > 3:
print("The tuple has more than 3 elements.")
else:
print("The tuple has 3 or fewer elements.")
def check_tuple_length(my_tuple, desired_length):
"""
Checks if the length of a tuple matches the desired length.
"""
if len(my_tuple) == desired_length:
print("The tuple has the desired length.")
else:
print("The tuple does not have the desired length.")
## Example usage:
my_tuple = (1, 2, 3)
desired_length = 3
check_tuple_length(my_tuple, desired_length)
another_tuple = ("a", "b", "c", "d")
desired_length = 3
check_tuple_length(another_tuple, desired_length)
Führen Sie das Skript erneut aus:
python ~/project/tuple_length.py
Sie sollten die folgende Ausgabe sehen:
The length of the string tuple is: 3
The length of the mixed tuple is: 4
The length of the empty tuple is: 0
The tuple has more than 3 elements.
The tuple has the desired length.
The tuple does not have the desired length.
Diese Ausgabe zeigt, wie Sie die len()
-Funktion verwenden können, um die Länge eines Tupels mit einer gewünschten Länge zu vergleichen und je nach Ergebnis unterschiedliche Aktionen auszuführen.