How to loop an input variable a fixed number of times based on what the input variable is

Viewed 99

I am making a guessing game, and I want the option to input how many tries you have. The number that you put will be the amount of times you will have to guess the random number. Problem is, I do not know how to put this in the game. How do I implement this? I have tried for loops, but it seems to not reach it no matter where I put it. Nothing seems to work, and I don't want to end up breaking my code. Help!

from random import randint
from time import sleep as wait

while True:
    while True:
        try:
            # Player chooses what the random numbers will be in between.
            range_ = int(input("Enter the range that your numbers are going to be between. (ex. 50): "))

            # Cannot put a 0, a 1, or a negative number.
            if range_ == 0:
                print("\nundefined cannot <0>\n")
                continue
            if range_ == 1:
                print("\nundefined, cannot add 1 to the range.\n")
                continue
            if range_ < 0:
                print("\nNo negatives!\n")
                continue

            # Player chooses how many tries they want.
            tries = int(input("Enter the amount of tries you want: "))

            # Cannot put a 0 or a negative number.
            if tries == 0:
                print("\nundefined, cannot <0> @ tries::int 0\n")
                continue
            if tries < 0:
                print("\nWhy would you want less than 0 tries?\n")
                continue

            break
        except ValueError:
            print("\nEnter a whole number please!\n")
            continue

    ai = randint(1, range_ + 1)

    while True:

        player = int(input("Enter number: "))

        if player > range_:
            print("Please put a number inside of the range.")
            continue

        if player < ai:
            print("\nBigger!\n")
            continue

        if player > ai:
            print("\nSmaller!\n")
            continue

        if player == ai:
            print("You win!\n")
            continue

        if player != int:
            print("Put a whole number!")
            continue

        if player == 0:
            print("\n\nno\n\n")
            wait(5)
            break

        if player == "":
            continue

        again = str(input("Do you want to play again? (yes/no): ")).lower()

        if again != "yes":
            print("Goodbye!")
            wait(3)
            break
        else:
            continue
2 Answers

You should check the state of the "again" condition at the beginning of your code:

again = "yes"
while again == "yes":
    while True:
        try:
            # Player chooses what the random numbers will be in between.
            range_ = int(input("Enter the range that your numbers are going to be between. (ex. 50): "))
            # Cannot put a 0, a 1, or a negative number.

As well as keep track of the number of attempts that the user has made:

ai = randint(1, range_)
    tries_count = 0
    while again == "yes":
        if tries_count <= tries:

Keeping track of attempts with something like:

if player > ai and player != 0:
                print("\nSmaller!\n")
                tries_count += 1
                continue

Try using for loops -

n = int(input('How many times you want to run the code'))
for i in range(n):
    # your code

You will need to add this at the start of your code after the import statements.

Related