Pyton-2: Unterschied zwischen den Versionen

Aus MeWi
Zeile 83: Zeile 83:
             i = i + 1
             i = i + 1
     print(f"Total Wörter mit mindestens {min_word_length} Zeichen: {i}")
     print(f"Total Wörter mit mindestens {min_word_length} Zeichen: {i}")
    return i




n = 0  # Gesamttotal aller Wörter
cities = ["Basel", "Bern", "Lausanne", "Zug", "Lugano", "Zürich", "St. Gallen"]
cities = ["Basel", "Bern", "Lausanne", "Zug", "Lugano", "Zürich", "St. Gallen"]
count_word(cities, 6)
countries = ["Schweiz", "Deutschland", "Italien", "Frankreich"]
 
n = n + count_word(cities, 6)
print(f"%%%% Total: {n}")
n = n + count_word(countries, 8)
print(f"%%%% Total: {n}")
</pre>
</pre>



Version vom 16. November 2024, 13:06 Uhr

008

Lists

# Lists
# array von 1 bis 42
lottozahlen = list(range(1, 43))
print(lottozahlen)

009

Lottozahlen generieren

# Zufallszahlen
import random


def my_print(output):
    print(output)
    print("********")


# range from 1 to 42 / random.sample gives unique elements
lottozahlen = random.sample(range(1, 43), 6)
my_print(lottozahlen)
lottozahlen.sort()
my_print(lottozahlen)
totelemente = len(lottozahlen)
my_print(totelemente)
i = 1
while i <= totelemente:
    print(f"zahl {i}  {lottozahlen[i - 1]}")
    i += 1

010

Reverse String

# reverse a string
def reverse_string(my_string):
    # uppercase the string
    upper_string = my_string.upper()
    # reverse it with slice
    reversed = upper_string[::-1]
    # how many chars in string?
    tot_chars = len(reversed)
    i = 0
    # loop through all chars
    while i < tot_chars:
        print(reversed[i])
        i += 1


my_string = "Das ist ein Satz."
reverse_string(my_string)

my_string = "Yet another one."
reverse_string(my_string)

011

einfacher for-loop

# simple for loop
teams = ["FCZ", "GC", "YF", "Red Star"]
teams.sort()
for team in teams:
    print(team)

012

Wörter mit mindestens n Zeichen ausgeben und zählen

# check word length
def count_word(words, min_word_length):
    i = 0
    for word in words:
        if len(word) >= min_word_length:
            print(word)
            i = i + 1
    print(f"Total Wörter mit mindestens {min_word_length} Zeichen: {i}")
    return i


n = 0  # Gesamttotal aller Wörter
cities = ["Basel", "Bern", "Lausanne", "Zug", "Lugano", "Zürich", "St. Gallen"]
countries = ["Schweiz", "Deutschland", "Italien", "Frankreich"]
n = n + count_word(cities, 6)
print(f"%%%% Total: {n}")
n = n + count_word(countries, 8)
print(f"%%%% Total: {n}")

001

Timestamp ausgeben

fff

001

Timestamp ausgeben

fff

001

Timestamp ausgeben

fff

001

Timestamp ausgeben

fff

001

Timestamp ausgeben

fff

001

Timestamp ausgeben

fff

001

Timestamp ausgeben

fff

001

Timestamp ausgeben

fff

001

Timestamp ausgeben

fff

001

Timestamp ausgeben

fff

001

Timestamp ausgeben

fff

001

Timestamp ausgeben

fff