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


Messages In This Thread
My while loop won't loop - by JHem - Sep-06-2020, 05:53 PM
RE: My while loop won't loop - by Larz60+ - Sep-06-2020, 07:15 PM
RE: My while loop won't loop - by pyzyx3qwerty - Sep-10-2020, 04:07 PM
RE: My while loop won't loop - by perfringo - Sep-10-2020, 07:49 PM
RE: My while loop won't loop - by JHem - Sep-16-2020, 01:59 PM
RE: My while loop won't loop - by perfringo - Sep-16-2020, 02:17 PM
RE: My while loop won't loop - by JHem - Sep-23-2020, 10:48 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Loop to find the best combination/score KoinKoin 21 31,459 Jan-05-2023, 10:31 AM
Last Post: KoinKoin
  Many iterations for loop question adesimone 9 1,988 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,788 Nov-08-2022, 01:25 PM
Last Post: deanhystad
  while loop idddj 8 1,834 Oct-03-2022, 05:03 PM
Last Post: jefsummers
  Beginner Python Question: FIzz Buzz using while loop camoyn13 2 1,955 Sep-20-2022, 09:00 AM
Last Post: deanhystad
  Function combining file manipulation and loop Leyo 5 1,944 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,340 Dec-14-2021, 12:23 AM
Last Post: supuflounder
Big Grin for loop nadun 3 1,990 Nov-22-2021, 03:36 PM
Last Post: deanhystad
  How to compile following python loop program reinispl 3 2,039 Oct-27-2021, 01:57 PM
Last Post: DeaD_EyE
  Printing During a Loop apeltes 16 5,532 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