Python Forum
My while loop won't loop
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
My while loop won't loop
#1
Hello! I'm new to python and I'm trying to solve a homework exercise that covers while-loops but the problem is that my while loop won't loop... Huh

The instructions for the exercise:

You should complete a program that takes care of rolling the dice so that there are new rolls as long as the last roll is larger than the previous one. Here's how the program starts (this is ready, you should not write it):
myRoll = 0 # represents the latest roll, start value 0
mySum = 0 # Sum for points collected
dice = random.randint(1,6) # Here new cast is generated between 1 and 6

Write a while-loop that runs as long as the newest roll (in the variable dice) is larger than the previous roll (its starting value can be found in myRoll)! Inside the loop:

- the latest roll should be added to mySum
- myRoll should get the value of the last roll
- a new roll must be generated for the variable dice.
- this number should be printed with print "Now you threw:", dice

When the newest roll is less than or equal to the most recent, the while loop must end and the score must be printed (it must not contain the latest roll) with two print sets. The first set should print the text "Your sum:" and the second should print the score. Do not print anything else!

Example of the functioning program:

Now you threw: 1
Now you threw: 3
Now you threw: 4
Now you threw: 4
Your sum:
8

This is what I've done so far:

while True: 
    if dice > myRoll:
        mySum += dice
        myRoll = dice
    
    elif dice <= myRoll:
        print("Your sum:")
        print(mySum)
        break
But it won't loop. It will toss the dice once and then it prints out the sum. I can't just wrap my head around it, what am I doing wrong here? Confused
Reply
#2
Show more code, in your submitted code:
  • There's no indication of what value of dice value is prior to entering loop
  • There's no indication of what myRoll value is prior to entering loop
  • Above refer to initialization
  • if dice > myRoll there will be no display, but myRoll will be set to dice value
    so on next iteration, elif will execute since there equal. This will display results,
    and exit the loop (the break statement).
Reply
#3
Just because your while loop doesn't work, it doesn't always mean the problem is in the loop itself - it may be something else outside your while loop. Show your full code, to help us help you.
pyzyx3qwerty
"The greatest glory in living lies not in never falling, but in rising every time we fall." - Nelson Mandela
Need help on the forum? Visit help @ python forum
For learning more and more about python, visit Python docs
Reply
#4
I suspect that your 'while' loop executes exactly as written:

- first loop: 'if' condition evaluates to True (as myRoll initially is 0 and dice is in range 1..6) and assignment is made myRoll = dice
- second loop:'if' condition is not True anymore (myRoll and dice are equal) and 'elif' clause is executed (print & break).
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#5
import random

myRoll = 0
mySum = 0
dice = random.randint(1,6)
print("Now you threw:", dice)

while True: 
    if dice > myRoll:
        mySum += dice
        myRoll = dice
     
    elif dice <= myRoll:
        print("Your sum:")
        print(mySum)
        break
Here's all the code. I previously left out the ones from the beginning that were already included (that I didn't have to write myself).

(Sep-10-2020, 07:49 PM)perfringo Wrote: I suspect that your 'while' loop executes exactly as written:

- first loop: 'if' condition evaluates to True (as myRoll initially is 0 and dice is in range 1..6) and assignment is made myRoll = dice
- second loop:'if' condition is not True anymore (myRoll and dice are equal) and 'elif' clause is executed (print & break).

Thank you for the answer and for clarifying. I know while loop will continue the loop unless the condition becomes false. However I don't understand how I can assign the value from the variable "dice" to the variable "myRoll" without it breaking the condition. "myRoll should get the value of the last roll", but I don't know how to do this.
Reply
#6
Always read assignment carefully:

Quote:Inside the loop:

- the latest roll should be added to mySum
- myRoll should get the value of the last roll
- a new roll must be generated for the variable dice.
- this number should be printed with print "Now you threw:", dice

Do you have last two inside your loop?
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#7
(Sep-16-2020, 02:17 PM)perfringo Wrote: Always read assignment carefully:

Quote:Inside the loop:

- the latest roll should be added to mySum
- myRoll should get the value of the last roll
- a new roll must be generated for the variable dice.
- this number should be printed with print "Now you threw:", dice

Do you have last two inside your loop?

Okay this really cleared things up for me, thank you! :) I fixed my code and got it to work like it should eventually. I guess I just assumed that those two things were already included inside the loop.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Loop to find the best combination/score KoinKoin 21 23,635 Jan-05-2023, 10:31 AM
Last Post: KoinKoin
  Many iterations for loop question adesimone 9 1,742 Nov-12-2022, 07:08 PM
Last Post: deanhystad
  Please check whether the code about the for loop question is correct. (SyntaxError) lilliancsk01 10 2,484 Nov-08-2022, 01:25 PM
Last Post: deanhystad
  while loop idddj 8 1,618 Oct-03-2022, 05:03 PM
Last Post: jefsummers
  Beginner Python Question: FIzz Buzz using while loop camoyn13 2 1,729 Sep-20-2022, 09:00 AM
Last Post: deanhystad
  Function combining file manipulation and loop Leyo 5 1,703 Mar-23-2022, 09:47 AM
Last Post: Leyo
  Using If Statements Instead of While Loop in Simple Game Program new_coder_231013 5 3,077 Dec-14-2021, 12:23 AM
Last Post: supuflounder
Big Grin for loop nadun 3 1,832 Nov-22-2021, 03:36 PM
Last Post: deanhystad
  How to compile following python loop program reinispl 3 1,910 Oct-27-2021, 01:57 PM
Last Post: DeaD_EyE
  Printing During a Loop apeltes 16 5,038 Oct-21-2021, 12:19 AM
Last Post: apeltes

Forum Jump:

User Panel Messages

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