Ich mache ein Quiz im Stil von 20 Fragen. Es verwendet eine Textdatei, um ein Wörterbuch mit Codes zu erstellen, die sich auf Antworten beziehen. Im Moment hat es nur 5 Fragen, die nicht annähernd genug sind, um genau zu sein mit ‚errät‘ aber schon es sucht chaotisch und schwerWie mehrere verschiedene if-Anweisungen (Python) zu kondensieren
CODES.TXT Beispiel Inhalt zu verstehen:
a1000, Ein Buch
A1111, eine Pfanne geben
Code:
File = open("CODES.txt","r")
CODES = { }
for line in File:
x = line.split(",")
a = x[0]
b = x[1]
c = len(b)-1
b = b[0:c]
CODES[a] = b
print("Think of anything: \n")
Q1 = str(input("Is it a) An Object, b) A Person, c) A Film: "))
if Q1 == "a":
Q2 = input("Is it hard: ")
if Q2 == "0":
Q3 = input("Is it light: ")
if Q3 == "0":
Q4 = input("Is it smaller than your head: ")
if Q4 == "0":
Q5 = input("Is it square: ")
elif Q4 == "1":
Q5 = input("Is it circular: ")
elif Q3 == "1":
Q4 = input("Is it bigger than your head: ")
if Q4 == "0":
Q5 = input("Is it square: ")
elif Q4 == "1":
Q5 = input("Is it circular: ")
elif Q2 == "1":
Q3 = input("Is it heavy: ")
if Q3 == "0":
Q4 = input("Is it smaller than your head: ")
if Q4 == "0":
Q5 = input("Is it square: ")
elif Q4 == "1":
Q5 = input("Is it circular: ")
elif Q3 == "1":
Q4 = input("Is it bigger than your head: ")
if Q4 == "0":
Q5 = input("Is it square: ")
elif Q4 == "1":
Q5 = input("Is it circular: ")
CCODE = str(Q1+Q2+Q3+Q4+Q5)
if CCODE in CODES:
print("You are thinking of " + CODES[CCODE])
else:
NV = str(input("You have outsmarted me. What were you thinking of: "))
File = open("CODES.txt","a")
File.write((CCODE+","+NV+"\n"))
File.close()
Wie würde ich die Frage Segment machen, um die If-Statements leichter zu lesen/verstehen . Derzeit habe ich viele eingebettete und es besteht nur aus 5 Fragen mit jeweils 2/3 Antworten.
Vielleicht ist Ihre Frage Struktur auf einem Wörterbuch hilft Einstellung? – danielfranca