Python Forum
increment variable in while loop
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
increment variable in while loop
#1
hello, i have a question please, why the while loop dosen't store the updated integer?
Output:
guess the number: 4 better luck next time the random number is : 8 round 2 ! guess the number: 5 better luck next time the random number is : 8 round 2 ! # supposed to be round 3 guess the number:
as you can see it looped in the second round but it didn't store the new variable in round 3 so it stayed round 2

def normalgame():
    print(" see if you  are lucky or not")
    print(" system will generated a random number between 0 and 10 try to guess the generated number")
    print(" if you want to quit type 911")
    while True:
        Dice = random.randint(0, 10)
        playernumchoise = int(input("guess the number: "))
        round = 1
        round += 1
        if playernumchoise == 911:
            print("Thank you for playing :)")
            break
        if playernumchoise > 10:
            print('please enter a number between 0 and 10')
        elif playernumchoise == Dice:
            print("you are so lucky")
            print("the random number is :", Dice)
        elif playernumchoise == Dice + 1 or playernumchoise == Dice - 1:
            print("too close :)")
            print("the random number is :", Dice)
        elif playernumchoise > Dice + 1 or Dice - 1 < playernumchoise < Dice + 1 or playernumchoise < Dice - 1:
            print("better luck next time")
            print("the random number is :", Dice)

        print("round", round, "!")
thank you
Reply
#2
With every loop in while you have:
round = 1
round += 1
So round can't be anything else than 2. You should move round = 1 out of while 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
#3
in every iteration of the loop you first set round to 1 (line 8) and then immediately increase it by 1 (on line 9)
And round is poor choice for variable name as round() is built-in function, so you override it and will no longer be able to use it.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
(Jan-20-2019, 12:25 PM)perfringo Wrote: With every loop in while you have:
round = 1
round += 1
So round can't be anything else than 2. You should move round = 1 out of while loop.

THANK you sir!, i forgot this little information (even i asked for the reasoning behind that in one of my threads xD)

buran: oh i didn't know round is a built in function ill change the name thank you
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Variable definitions inside loop / could be better? gugarciap 2 373 Jan-09-2024, 11:11 PM
Last Post: deanhystad
  How to create a variable only for use inside the scope of a while loop? Radical 10 1,523 Nov-07-2023, 09:49 AM
Last Post: buran
  help to increment a third list hermine 7 1,269 Nov-29-2022, 04:19 PM
Last Post: perfringo
  mysql id auto increment not working tantony 10 2,304 Oct-18-2022, 11:43 PM
Last Post: Pedroski55
  Nested for loops - help with iterating a variable outside of the main loop dm222 4 1,531 Aug-17-2022, 10:17 PM
Last Post: deanhystad
  loop (create variable where name is dependent on another variable) brianhclo 1 1,101 Aug-05-2022, 07:46 AM
Last Post: bowlofred
  Multiple Loop Statements in a Variable Dexty 1 1,175 May-23-2022, 08:53 AM
Last Post: bowlofred
Big Grin Variable flag vs code outside of for loop?(Disregard) cubangt 2 1,129 Mar-16-2022, 08:54 PM
Last Post: cubangt
  How to save specific variable in for loop in to the database? ilknurg 1 1,109 Mar-09-2022, 10:32 PM
Last Post: cubangt
  How to add for loop values in variable paulo79 1 1,410 Mar-09-2022, 07:20 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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