Sep-06-2020, 05:53 PM
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...
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:

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) breakBut 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?
