You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

骰子游戏循环逻辑调整:如何让循环从指定重启点执行?

Fixing Your Dice Game Loop Logic

Hey there! Let's get your dice game looping exactly how you want it—starting with the initial question once, then repeating the "roll again" prompt every time after that. I spotted a couple of small issues in your code that are throwing off the flow, plus a quick fix to make your dice rolls actually random each time.

What's Wrong With the Current Code?

  • The initial Would you like to roll the dice? question is inside the while play loop, so it pops up every time the loop restarts—this is why you're not getting the repeat prompt you want.
  • Your dice_number is only generated once at the top of the script, so every "roll" will show the same number (not very fun for a dice game!).
  • The repeat question logic is redundant and doesn't handle edge cases like capitalized inputs (e.g., "Yes" instead of "yes") gracefully.

Corrected Code

Here's a revised version that fixes all these issues and follows your desired flow:

import random

# Ask the starting question ONCE, before the loop begins
user_choice = input("Would you like to roll the dice? ").lower()

# Keep looping as long as the user says "yes"
while user_choice == 'yes':
    # Generate a new random dice number EVERY time we roll
    dice_roll = random.randint(1, 6)
    print(f"You rolled a {dice_roll}!")
    
    # Ask the repeat question to control the next loop iteration
    user_choice = input("Do you want to roll the dice again? ").lower()
else:
    # Handle the exit case based on user input
    if user_choice == 'no':
        print("It has been fun playing with you.")
    else:
        print("Let us play later then.")

Key Changes Explained

  1. Moved the initial question outside the loop: Now it only shows once when the game starts, just like you want.
  2. Generated a new dice roll inside the loop: Every time the user chooses to roll, we create a fresh random number between 1-6.
  3. Used user_choice to control the loop: The loop runs as long as the user enters "yes" (case-insensitive thanks to .lower()), and the repeat question updates this variable directly.
  4. Cleaned up exit logic: The else block handles all non-"yes" inputs, giving appropriate messages for "no" vs. invalid responses.

Optional: Handle Invalid Inputs

If you want to make the game more robust (so users can't break it by typing something other than "yes" or "no"), you can add a small loop to validate the repeat question:

# Inside the main loop, replace the user_choice line with this:
while True:
    user_choice = input("Do you want to roll the dice again? ").lower()
    if user_choice in ['yes', 'no']:
        break
    print("Oops, please enter either 'yes' or 'no'.")

This will keep asking the repeat question until the user gives a valid answer.

内容的提问来源于stack exchange,提问作者QBear

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 08:03:49