import random
def diceroll():
num_dice = random.randint(1,6)
print("You got " + str(num_dice) + "!")
diceroll()
def question():
response = input("You want to roll again?\n")
while response == "y":
diceroll()
response = input("You want to roll again?\n")
if response == "n":
print("Thank you for playing! :) ")
exit()
while "y" or "n" not in response:
response = input("Please answer with y or n!\n")
while response == "y":
diceroll()
response = input("You want to roll again?\n")
if response == "n":
print("Thank you for playing! :) ")
exit()
question()
Gibt es eine Möglichkeit, ich könnte diesen Code einfacher und habe die gleiche Funktionalität? Ich versuchte eine andere Version ohne Klassen zu verwenden, aber wenn ich ein anderes Zeichen neben "y" oder "n" eingeben Der Code endet.Meine erste Rolle ein Würfel Python Spiel
import random
answer = "yes"
while answer in ["yes", "y"]:
roll = random.randint(1,6)
print("You rolled " + str(roll) + "!")
answer = input("Would you like to roll again?\n")
if answer in ["n", "no"]:
print("Thank you for playing!")
else :
print("Please answer with yes or no!")
answer = input("Would you like to roll again?\n")
Ihre erste Version wird mit Klassen nicht. Es benutzt eine Funktion –