Wie kann ich diesen Code in Python fertigschreiben?

screen1 - (programmieren, Python) screen2 - (programmieren, Python)

1 Antwort

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!
-----------------""")
gregory1801 
Fragesteller
 10.12.2017, 08:57

Vielen Dank! :D

0