Python Forum
Issue with my 'roll the dice simulation'-exercise (cannot break out of the loop)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Issue with my 'roll the dice simulation'-exercise (cannot break out of the loop)
#1
Hello!

I know that there are many code-solutions to find with google, when you type in the name of this common exercise, but many of them had another goal for their little program.
The instruction/goals for my little exercise are exactly these one:

https://gyazo.com/d0215749fe36959036bf4ed8e0c1496f

My Code so far:

import random
print(random.randint(1, 6))
while True:
    print("Do you want to roll the dice again? Type Yes or No?")
    answer = input()
    if answer != 'No':
        print(random.randint(1, 6))

My output:

Output:
Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:06:47) [MSC v.1914 32 bit (Intel)] on win32 Type "copyright", "credits" or "license()" for more information. >>> RESTART: C:/Users/kr-ga/Desktop/Programming/Python/PRAXIS/small Projects/roll the dice simulation/Roll the dice.py 2 Do you want to roll the dice again? Type Yes or No? Yes 5 Do you want to roll the dice again? Type Yes or No? No Do you want to roll the dice again? Type Yes or No?
Basically, the program does not break after 'No' was typed in, so that I must assume , that something is missing or more likely that before my variables are wrongly placed and/or defined?
I have tried already to add and else-statement , following by 'break', but it did not work.
So I do assume that my code is already at its beginning wrong regards to what it shall acchieve (given the exercise-instruction in the linked screenshot).

Can anyone please help out with some infos and explanations?

Regards,
Placebo

Update:
I think I have solved it myelf?

import random
print(random.randint(1, 6))
while True:
    print("Do you want to roll the dice again? Type Yes or No?")
    answer = input()
    if answer != 'No':
        print(random.randint(1, 6))
    else:
        print("Thank you for playing and see you next time!")
        break
I still would like to not delete my previous post, since I want to show my struggles and at the same time ask, if my code is really correct now according to the instructions and/or if you would make something different or improve something?
Maybe I just got lucky that it seems to run now, but there might be still some fundamental mistake.

As for improving the program:
For example, I when the User types anything, unless exactly 'No', the programm will treat the answer as 'Yes' and loop again.
I would like to print the program something like "Please answer only by using 'Yes' or 'No'. Try again!".
The only opportunity which comes in mind is with an elif-statement in between of the If-and-else-statements.
But this seems not possible, since need to somehow still end with 'No' and the 'else:break'.

Quick offtopic question:
Regards to commonly used python-coding-style, I do wonder which quotationmarks are more used?
The double ones " or the single ones '
?
Reply
#2
First, you have corrected the code by adding 'break'; that provides something to be executed when the user enters inputs "No". Previously, it was recognizing the inputted "No" and returning to the beginning of the loop because there was nothing to execute in that circumstance.

Any time that I'm writing a script that relies on user input and text comparisons, I use the string.lower() method to adjust for accidental capitalization issues. The following with recognize "No", "no", "NO", and "nO":

import random
print(random.randint(1, 6))
while True:
    print("Do you want to roll the dice again? Type Yes or No?")
    answer = input()
    if answer.lower() != 'no':
        print(random.randint(1, 6))
    else:
        print("Thank you for playing and see you next time!")
        break
For the input testing, you can add a conditional that continues the loop when something not recognized is entered. This will effectively skip the yes/no testing for rolling the die and allow the user to try again:

import random
print(random.randint(1, 6))
while True:
    print("Do you want to roll the dice again? Type Yes or No?")
    answer = input()

    if answer.lower() not in ("no", "yes"):
        print("Please answer only by using 'Yes' or 'No'. Try again!")
        continue

    if answer.lower() != 'no':
        print(random.randint(1, 6))
    else:
        print("Thank you for playing and see you next time!")
        break
Reply
#3
Hey, thanks a lot for your insights.
Very valueable input on making it a habit using the string.lower() method for your mentioned reasons^^

Ah interesting, you can use the 'in' or 'not in' not only for finding values in lists, but also for checking for certain text.
Nice solution from you^^

Thanks for your input:)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Code won't break While loop or go back to the input? MrKnd94 2 907 Oct-26-2022, 10:10 AM
Last Post: Larz60+
  How to break out of a for loop on button press? philipbergwerf 6 1,664 Oct-06-2022, 03:12 PM
Last Post: philipbergwerf
  While loop not ending (Best of 10 dice game) K3nidi 3 1,457 Jul-09-2022, 09:53 AM
Last Post: K3nidi
  break out of for loop? User3000 3 1,384 May-17-2022, 10:18 AM
Last Post: User3000
  Asyncio: Queue consumer gets out of while loop without break. Where exactly and how? saavedra29 2 2,606 Feb-07-2022, 07:24 PM
Last Post: saavedra29
  Dice Roll (Find out how many rolls until specified streak) DustinKlent 4 3,918 Jun-13-2021, 09:44 AM
Last Post: Gribouillis
  tkinter control break a while loop samtal 0 2,351 Apr-29-2021, 08:26 AM
Last Post: samtal
  Cannot 'break' from a "for" loop in a right place tester_V 9 3,890 Feb-17-2021, 01:03 AM
Last Post: tester_V
  How to break a loop in this case? Blainexi 10 7,158 Sep-24-2020, 04:06 PM
Last Post: Blainexi
  Help with dice roll program kraco 4 2,044 Sep-22-2020, 02:06 PM
Last Post: kraco

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020