1

(Python beginner Problem)::: I want to make a variable that store my limit variable but I just want to decrement it while looping. it's like that i want to make life left for the user.

 import random

    limit = 5
    i = 1

    while i <= limit:
        decrement_00 = limit  #Problem1
        decrement_00 = decrease - 1
        num = random.randint(1,6)
        user = int(input("enter a number: "))
        if user == num:
            print("You're correct")
            break
        else:
            print("Try again.")
            print(f"You only have {decrement_00} left.")

        i = i + 1

2 Answers2

1

Solution: If you want to show the remaining iterations(limit), you can simply print (limit - i) instead of using extra variables.

Now comes to the problem in your code, you are printing decrement_00 to show to the user, just review the following two lines and you will understand the mistake.

decrement_00 = limit  #Problem1
decrement_00 = decrease - 1

Hint:

Ever initialized decrease?

will decrement_00's value change?

Jayabal
  • 3,619
  • 3
  • 24
  • 32
1

Try this :

import random

limit = 5
i = 1

while i <= limit:
    #decrement_00 = limit  #Problem1
    limit = limit - 1
    num = random.randint(1,6)
    user = int(input("enter a number: "))
    if user == num:
        print("You're correct")
        break
    else:
        print("Try again.")
        print(f"You only have {limit} left.")

    #i = i + 1
J.K
  • 1,178
  • 10
  • 13