bin neu in Python und wollte kurz mal ein pyProgramm von mir vorstellen. Das Programm heißt pyLottoCheck und vergleicht Lottozahlen mit Lottozahlen aus einer Datei. Ich weiß die 37 Mio sind schon weg, aber gebrauchen kann man ja es noch immer. Vieleicht kennt ja einer das Problem, man spielt in eienr Tippgesellschaft 6 aus 49 und muss mehrere LottoTIPs mit den Lottozahlen vergleichen. Klar man koennte jetzt bis Monatsende warten, und man bekommt von der Tippgesellschaft ne Uebersicht aller Gewinne, aber wer möchte da denn warten, wenn gerade z.b. 37 Mio im Jackpot liegen. Mit pyLottoCheck geht das ganz schnell. Einfach die Lottozahlen in eine Datei eintippen, speichern und das Programm starten.
Das wäre schön wenn jemand sich das Programm anschauen würde und etwas darüber sagen könnte. Was ich falsch gemacht habe, Was ich vielleicht besser machen könnte, usw. halt solche Dinge. Wäre echt toll von euch.
Danke.
Code: Alles auswählen
"""
pyLottoCheck-V.0.1#10.10.2006
Vergleicht mehrere Lottozahlen aus einer Datei.
"""
# ============================================================
# Beim erstellen einer Lottodatei bitte folgendes beachten:
# - Zahlen werden durch ein Leerzeichen getrennt
# - Jede Zeile enthaelt nur ein LottoTIP
# - Reihenfolge: Zahl1 Zahl2 Zahl3 Zahl4 Zahl5 Zahl6 Superzahl
# Beispiel-Datei: 5 10 15 20 25 30 5
# 2 12 18 29 32 47 2
# 8 22 28 31 39 45 9
# ============================================================
from sys import exit
from string import split
def fOpenFile(psFilePath):
""" Liest den Inhalt aus einer Datei aus. """
oFile = open(psFilePath,'r')
sText = oFile.read()
oFile.close()
return sText
def fFileExist(psFilePath):
""" Prueft die Existenz einer Datei. """
try:
oFile = open(psFilePath,'r')
except IOError: #EA-Fehler
return 0
else:
oFile.close()
return 1
def fIsNumber(psCheckString):
""" IsNumber """
for f in [int, float,]:
try:
f(psCheckString)
except ValueError:
continue
return 1
return 0
def fCheckLottoString(psLottoString, iPartsCount):
""" Prueft einen LottoTIP """
lLottoNumbers = split(psLottoString)
# Verschiedene Pruefvorgaenge ..
# Enthaelt der LottoString exakt eine bestimmte Anzahl an Zahlen ?
bLottoStringOK_1 = 1
if (len(lLottoNumbers) <> iPartsCount):
bLottoStringOK_1 = 0 # Nein
# Hat der LottoString wirklich nur Zahlen ?
bLottoStringOK_2 = 1
for sLottoNumber in lLottoNumbers:
if (fIsNumber(sLottoNumber) == 0):
bLottoStringOK_2 = 0 # Nein
# Waren alle Pruefungen okay ?
if (bLottoStringOK_1 == 0) or (bLottoStringOK_2 == 0):
return 0 # Nein
else:
return 1 # Ja
def fGetLottoList(psLottoFilePath):
""" Laedt die LottoDatei und wandelt sie in eine Liste um. """
sLottoFile = fOpenFile(psLottoFilePath)
lLotto = split(sLottoFile,'\n')
# Leere LottoListe erzeugen
lLottoList = []
# Umwandeln in eine Liste
for sLottoString in lLotto:
lLottoNumbers = split(sLottoString)
# LottoString okay ?
if (fCheckLottoString(sLottoString, 7) == 0):
# LottoString ist falsch
# LottoString wird uebersprungen
print '---- ERROR: Folgender LottoTIP ist falsch: ' + sLottoString
else:
# LottoString zur LottoListe hinzufuegen
lLottoStringList = []
for sLottoNumber in lLottoNumbers:
lLottoStringList.append(sLottoNumber)
lLottoList.append(lLottoStringList)
return lLottoList
def fStartLottoCheck(lLottoString, lUserLottoString):
""" Vergleicht zwei LottoStrings. """
iLottoHits = 0
bZusatzzahl = 0
bSuperzahl = 0
# Lottozahlen (1-6) vergleichen
for i1 in range(0,6):
for i2 in range(0,6):
if (lUserLottoString[i1] == lLottoString[i2]):
iLottoHits = iLottoHits + 1
break
# Zusatzzahl pruefen
for i1 in range(0,6):
if (lUserLottoString[6] == lLottoString[i1]):
bZusatzzahl = 1
break
# Superzahl pruefen
if (lUserLottoString[7] == lLottoString[6]):
bSuperzahl = 1
# Liste in einen String umwandeln
sLottoNumbers = ''
for sLottoNumber in lLottoString:
sLottoNumbers = sLottoNumbers + sLottoNumber + ' '
# Status ausgeben
sLottoReport = ''
if (iLottoHits >= 3): # Nur ab 3 Richtige ein Status ausgeben
if (bZusatzzahl == 1):
# Mehr als 2 Richtige mit Zussatzzahl
sLottoReport = '%s | %s Richtige + Zussatzzahl' \
%(sLottoNumbers,str(iLottoHits))
elif (bSuperzahl == 1 and iLottoHits == 6):
# Jackpot
sLottoReport = '%s | JACKPOT' %sLottoNumbers
else:
# Mehr als 2 Richtige ohne Zussatzzahl
sLottoReport = '%s | %s Richtige' \
%(sLottoNumbers,str(iLottoHits))
# Status ausgeben
print sLottoReport
# ============================
# MAIN
# ============================
# Dateipfad zur LottoDatei anfragen
bRunning = 1
while (bRunning == 1):
print '>> Geben Sie den Dateipfad zur LottoDatei ein.'
print ">> Geben Sie 'x' ein um zu beenden."
inputFilePath = raw_input('Eingabe: ')
# Hat der Benuetzer ein x eingegeben?
if (inputFilePath == 'x'):
print '>> Auf Widersehen.'
exit()
# Pruefe Existenz
print '>> Pruefe ob Datei existiert ...'
if (fFileExist(inputFilePath) == 0):
# Datei existiert nicht
print '---- ERROR: Datei kann nicht gelesen werden ' \
'oder existiert nicht.'
print '---- Versuchen sie es erneut.'
else:
print '---- Datei existiert.'
bRunning = 0
# Lottodaten aus der Datei importieren
print '>> Importiere alle Lottodaten aus der Datei ...'
lFileLottoList = fGetLottoList(inputFilePath)
print '---- Lottodaten importiert.'
# Nun die Lottozahlen anfragen
bRunning = 1
while (bRunning == 1):
print '>> Geben Sie jetzt Ihre Lottozahlen ein.'
print '>> Bitte trennen Sie jede Zahl durch eine Leerzeichen.'
print '>> Reihenfolge (Z1 Z2 Z3 Z4 Z5 Z6 Superzahl Zusatzzahl).'
print ">> Geben Sie 'x' ein um zu beenden"
inputLottoNumbers = raw_input('Eingabe: ')
if (inputLottoNumbers == 'x'): # Hat der Benuetzer ein x eingegeben?
print '>> Auf Widersehen.'
exit() # Programm beenden
print '>> Pruefe eingegebene Lottozahlen ...'
if (fCheckLottoString(inputLottoNumbers,8) == 0):
print '---- Lottozahlen nicht korrekt! Versuchen Sie es erneut.'
else:
print '---- Lottozahlen okay.'
bRunning = 0
print '>> Vergleiche Lottozahlen ...'
# Eingegebene Lottozahlen in eine Liste umwandeln
lLottoNumbers = split(inputLottoNumbers)
lUserLottoList = []
for lLottozNumber in lLottoNumbers:
lUserLottoList.append(lLottozNumber)
#Lottozahlen auswerten
print '='*40
for lLottoString in lFileLottoList:
fStartLottoCheck(lLottoString,lUserLottoList)
print '='*40
# Fertig!
print '>>>> Ueberpruefung abgeschlossen.'
- 3 9 25 42 45 46 4
3 9 28 30 38 48 4
4 6 27 28 38 49 4
4 15 37 42 45 49 4
4 25 30 46 48 49 4
6 7 13 25 27 46 4
6 27 30 42 45 48 4
7 13 15 30 37 48 4