Wie kann ich diesen Code in Python fertigschreiben?
Ich möchte es hinbekommen, dass man wenn man die Aufgabe richtig beantwortet, eine y/n option hat um weiterzuspielen, und wenn man sie falsch beantwortet gezwungen wird weiterzuspielen.
Der Anfang steht schon richtig da, aber den Schluss bekomm ich nicht hin, da ich noch nicht so erfahren bin. Link zum Code: https://www.mediafire.com/file/jd7wgagg87sf1df/spiel_eigenversion2.py Oder Screenshots.


1 Antwort
Von gutefrage auf Grund seines Wissens auf einem Fachgebiet ausgezeichneter Nutzer
programmieren
So funktionierts:
#Imports
import random, time
#Actually calling random.seed (no parentheses, no call)
random.seed(time.time())
#Multiline String-literal, oh yeah. ;)
print("""-----------------
Welcome to the DiMa challenge.
-----------------
You will have to addition, substract, divide or multiply the numbers you receive after dicing.
----------------- Good Luck
-----------------""")
#Infinite loop, break condition inside
while True:
# Dicing
print("Rolling the dices...")
a = random.randint(1,6)
b = random.randint(1,6)
#The different operations:
opzahl = random.randint(1,4)
#Operator "+"
if opzahl == 1:
print("{0} + {1} = ".format(a,b), end="")
c = a + b
#Operator "-"
elif opzahl == 2:
print("{0} - {1} = ".format(a,b), end="")
c = a - b
#Operator "/"
elif opzahl == 3:
print("{0} / {1} = ".format(a,b), end="")
c = a / b
#Operator "*"
elif opzahl == 4:
print("{0} * {1} = ".format(a,b), end="")
c = a * b
#Moved the answer validation here, removing redundant code
#We don't want the game to crash, aye?
try:
#We'll use float instead of int for reasons like a=1; b=3
answer = float(input())
except ValueError:
#Invalid input = Wrong answer
print("Wrong. Let's try again.")
continue
#Don't ever compare a float (or a double) by using "==" or such!
if abs(answer - c) < 0.00001:
print("That's right! Another one? [Y/n] ",end="")
answer = input()
#Empty answer = implicit yes
#Note: this is about a "not yes" situation...
if not (len(answer) == 0 or answer.upper()[0] == "Y"):
#The break
break
else:
print("Wrong. Let's try again.")
#Fin
print("""-----------------
Thanks for playing!
-----------------""")