mercredi 24 juillet 2019

Random number function: how do I set different upper limits for a random number function to adjust difficulty in my guessing game

I created a random number guessing game, and it works fairly well except for two problems. One, I tried to add a function that will allow the user to select difficulty and I couldn't figure it out. Two, when the user wins the game and selects "Y" to continue the game again, it doesn't generate a new random number.

After I tested the code without a difficulty selection, I tried adding four levels by replacing the upper limit of eleven in random_number = random.randint(1, 11) with upper_limit and defining upper_limit as various integers depending on what level (E, M, D, or G) the player chooses.

import random
print("""
Loop Practice
Exercise 1""")
hot = """
Guess is hot!"""
cold = """
Guess is cold... :("""
#upper_limit = int()
#difficulty = ""
#if difficulty.upper() == "E":
    #upper_limit = 11
#elif difficulty.upper() == "M":
    #upper_limit = 26
#elif difficulty.upper() == "D":
    #upper_limit = 500
#elif difficulty.upper() == "G":
    #upper_limit = 1000000

random_number = random.randint(1, 11)
guesses_left = 3
play = True
while True:
    while guesses_left > 0:
        #difficulty = input("Please select a difficulty: (E)asy, (M)edium, (D)ifficult, (G)od Mode")

        guess = int(input("Guess a number between 1 and 10: "))
        if guess == random_number:
            print("Congratulations! You guessed correctly.")
            retry = input("Try again? Y/N ")
            if retry.upper() == "Y":
                guesses_left = 3
                continue
            elif retry == "N":
                break

        elif guess != random_number:
            if guess > random_number:
                if (guess - random_number) < 4:
                    print(hot)
                else:
                    print(cold)
                print("Your guess is too high.")
            elif guess < random_number:
                if (random_number - guess) < 4:
                    print(hot)
                else:
                    print(cold)
                print("Your guess is too low.")
            print("Guess again sucker")
            guesses_left -= 1
            continue
    else:
        print("Sorry, you only had three guesses.")
        retry = input("Try again? Y/N ")
        if retry.upper() == "Y":
            guesses_left = 3
            continue

        elif retry == "N":
            break

When I tried the above code without #, I expected to be able to input the desired level but got an error message.

Traceback (most recent call last):
  File "/Users/ttt/PycharmProjects/HelloWorld2/Practice.py", line 36, in <module>
    random_number = random.randint(1, upper_limit)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/random.py", line 222, in randint
    return self.randrange(a, b+1)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/random.py", line 200, in randrange
    raise ValueError("empty range for randrange() (%d,%d, %d)" % (istart, istop, width))
ValueError: empty range for randrange() (1,1, 0)

When I tried the above code with #, I was able to play the game but it used the same number each round.

Aucun commentaire:

Enregistrer un commentaire