Add the condition for winning and return
in this case. Also, add return
to the condition where you lose.
Additional changes to improve the code:
Remove the useless first dice roll (which does not get counted in your case).
Declare your_list
in the smallest scope possible (here, inside the function).
Improve the prompt.
import random
def dice_simulate():
your_list = []
while(1):
flag = str(input("Roll the dice (y/n)? "))
if flag == 'y':
number = random.randint(1,6)
your_list.append(number)
print(number)
print (your_list)
list_sum = sum(your_list)
if list_sum > 14:
print ('you lose')
return
elif list_sum == 14:
print ('you win')
return
else:
print("ending the game")
return
dice_simulate()
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…