lotto

Wenn du dir nicht sicher bist, in welchem der anderen Foren du die Frage stellen sollst, dann bist du hier im Forum für allgemeine Fragen sicher richtig.
Antworten
Kranioklast
User
Beiträge: 14
Registriert: Dienstag 9. November 2004, 20:56
Wohnort: Berlin

Hallo,

ich habe ein kleines Lottoprogramm geschrieben und würde gerne Folgendes wissen:
1. Wie lässt es sich kürzen?
2. Wie kann ich verhindern, dass ein bloßes "Enter" das Programm abbricht und
3. wie kann ich verhindern, dass Kommazahlen akzeptiert werden?

Vielen Dank im Voraus!!

Code: Alles auswählen

# lotto

from random import*

def lotto():
    richtige=0
    print "Tippe 6 aus 49!"
    tipp1 = input("Tipp1: ")
    while tipp1>49 or tipp1<1:
        tipp1 = input("Die Zahl muss zwischen 1 und 49 liegen! Tipp1: ")
    tipp2 = input("Tipp2: ")
    while tipp2>49 or tipp2<1:
        tipp2 = input("Die Zahl muss zwischen 1 und 49 liegen! Tipp2: ")
    while tipp2==tipp1:
        tipp2=input ("Der Tipp kam schon vor. Nimm eine andere Zahl: ")
    tipp3 = input("Tipp3: ")
    while tipp3>49 or tipp3<1:
        tipp3 = input("Die Zahl muss zwischen 1 und 49 liegen! Tipp3: ")
    while tipp3==tipp2 or tipp3==tipp1:
        tipp3=input ("Der Tipp kam schon vor. Nimm eine andere Zahl: ")
    tipp4 = input("Tipp4: ")
    while tipp4>49 or tipp4<1:
        tipp4 = input("Die Zahl muss zwischen 1 und 49 liegen! Tipp4: ")
    while tipp4==tipp3 or tipp4==tipp2 or tipp4==tipp1:
        tipp4=input ("Der Tipp kam schon vor. Nimm eine andere Zahl: ")
    tipp5 = input("Tipp5: ")
    while tipp5>49 or tipp5<1:
        tipp5 = input("Die Zahl muss zwischen 1 und 49 liegen! Tipp5: ")
    while tipp5==tipp4 or tipp5==tipp3 or tipp5==tipp2 or tipp5==tipp1:
        tipp5=input ("Der Tipp kam schon vor. Nimm eine andere Zahl: ")
    tipp6 = input("Tipp6: ")
    while tipp6>49 or tipp6<1:
        tipp6 = input("Die Zahl muss zwischen 1 und 49 liegen! Tipp6: ")
    while tipp6==tipp5 or tipp6==tipp4 or tipp6==tipp3 or tipp6==tipp2 or tipp6==tipp1:
        tipp6=input ("Der Tipp kam schon vor. Nimm eine andere Zahl: ")
    zahl1 = int(random()*49+1)
    zahl2 = int(random()*49+1)
    zahl3 = int(random()*49+1)
    zahl4 = int(random()*49+1)
    zahl5 = int(random()*49+1)
    zahl6 = int(random()*49+1)

    while zahl2==zahl1:
        zahl2 = int(random()*49+1)
    while zahl3==zahl2 or zahl3==zahl1:
        zahl3 = int(random()*49+1)
    while zahl4==zahl3 or zahl4==zahl2 or zahl4==zahl1:
        zahl4 = int(random()*49+1)
    while zahl5==zahl4 or zahl5==zahl3 or zahl5==zahl2 or zahl5==zahl1:
        zahl5 = int(random()*49+1)
    while zahl6==zahl5 or zahl6==zahl4 or zahl6==zahl3 or zahl6==zahl2 or zahl6==zahl1:
        zahl6 = int(random()*49+1)

    print "Ziehung: ",zahl1,
    print zahl2,
    print zahl3,
    print zahl4,
    print zahl5,
    print zahl6
    
    ziehung=[zahl1,zahl2,zahl3,zahl4,zahl5,zahl6];
    ziehung.sort();
    print "Sortierte Liste:",ziehung
    
    if tipp1==zahl1 or tipp1==zahl2 or tipp1==zahl3 or tipp1==zahl4 or tipp1==zahl5 or tipp1==zahl6:
        richtige=richtige+1
    if tipp2==zahl1 or tipp2==zahl2 or tipp2==zahl3 or tipp2==zahl4 or tipp2==zahl5 or tipp2==zahl6:
        richtige=richtige+1
    if tipp3==zahl1 or tipp3==zahl2 or tipp3==zahl3 or tipp3==zahl4 or tipp3==zahl5 or tipp3==zahl6:
        richtige=richtige+1
    if tipp4==zahl1 or tipp4==zahl2 or tipp4==zahl3 or tipp4==zahl4 or tipp4==zahl5 or tipp4==zahl6:
        richtige=richtige+1
    if tipp5==zahl1 or tipp5==zahl2 or tipp5==zahl3 or tipp5==zahl4 or tipp5==zahl5 or tipp5==zahl6:
        richtige=richtige+1
    if tipp6==zahl1 or tipp6==zahl2 or tipp6==zahl3 or tipp6==zahl4 or tipp6==zahl5 or tipp6==zahl6:
        richtige=richtige+1
    print
    print "Du hast",richtige,"Richtige!"
BlackJack

Kranioklast hat geschrieben:Ich habe ein kleines Lottoprogramm geschrieben und würde gerne Folgendes wissen:
1. Wie lässt es sich kürzen?
Nicht soviel Quelltext wiederholen, der eigentlich das gleiche macht nur mit unterschiedlichen Werten.

Und es macht vielleicht auch Sinn den Quelltext zu verlängern in dem man ihn in Funktionen aufteilt. Zum Beispiel eine die die Zahlen vom Benutzer entgegennimmt, eine die Zufallszahlen zieht usw.

Die vielen langen ``if`` Abfragen lassen sich durch eine geeignete Datenstruktur erschlagen. In diesem Fall bietet sich `set()` an, weil man mit Zahlenmengen operiert.
2. Wie kann ich verhindern, dass ein bloßes "Enter" das Programm abbricht und
Falls Du damit meinst, dass das Programm mit einer Fehlermeldung abbricht wenn keine Zahl eingegeben wurde: Die Ausnahme behandeln.
3. wie kann ich verhindern, dass Kommazahlen akzeptiert werden?
`raw_input()` benutzen, die Zeichenkette mit `int()` in eine ganze Zahl umwandeln und entsprechend die Ausnahme behandeln falls der Benutzer etwas falsches eingegeben hat.

Code: Alles auswählen

import random

def pick(count, of):
    available = set(xrange(1, of + 1))
    picked = set()
    while len(picked) < count:
        try:
            number = int(raw_input('Wähle eine Zahl zwischen 1 und %d: '
                                   % of))
        except ValueError:
            print 'Das war keine Zahl!'
        else:
            if number in available:
                available.remove(number)
                picked.add(number)
            else:
                print 'Diese Zahl wurde schon gewählt oder ist ungültig!'
    return picked

def draw(count, of):
    numbers = range(1, of + 1)
    random.shuffle(numbers)
    return set(numbers[:count])

def main():
    picked = pick(6, 49)
    drawed = draw(6, 49)
    print 'Ziehung:', ', '.join(map(str, sorted(drawed)))
    print 'Du hast %d Richtige!' % len(picked & drawed)
Kranioklast
User
Beiträge: 14
Registriert: Dienstag 9. November 2004, 20:56
Wohnort: Berlin

Oh, super!
Vielen Dank für die schnelle Hilfe. Ich werde mich sofort damit beschäftigen!
Antworten