0
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()
このコードを単純化して同じ機能を持たせる方法はありますか?クラスを使用せずに別のバージョンを試しましたが、 "y"または "n"コードは終了します。それはあなたがループ内で滞在する条件として取ったものですので私の最初のロールスロイスサイコロゲーム
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")
あなたの最初のバージョンは、クラスを使用していません。それは関数を使用しています –