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
Question Arduino simulation eeman_572 0 247 Sep-06-2021, 10:07 AM
Last Post: eeman_572
  Dice Roll (Find out how many rolls until specified streak) DustinKlent 4 1,079 Jun-13-2021, 09:44 AM
Last Post: Gribouillis
  tkinter control break a while loop samtal 0 716 Apr-29-2021, 08:26 AM
Last Post: samtal
  Cannot 'break' from a "for" loop in a right place tester_V 9 1,408 Feb-17-2021, 01:03 AM
Last Post: tester_V
  How to break a loop in this case? Blainexi 10 2,373 Sep-24-2020, 04:06 PM
Last Post: Blainexi
  Help with dice roll program kraco 4 948 Sep-22-2020, 02:06 PM
Last Post: kraco
  how to break the loop? bntayfur 8 1,320 Jun-07-2020, 11:07 PM
Last Post: bntayfur
  break for loop Agusben 1 800 Apr-01-2020, 05:07 PM
Last Post: Larz60+
  Help For AutoGui Loop break ahmetnwpal 0 932 Mar-11-2020, 01:14 PM
Last Post: ahmetnwpal
  no idea for a simulation accoumar 7 1,525 Feb-19-2020, 03:06 PM
Last Post: jefsummers

Forum Jump:

User Panel Messages

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