i got this code online:
import random
Default highscores for difficulties
easyScore = 9999999999
mediumScore = 9999999999
hardScore = 9999999999
attempts = [0]
isPlaying = True
Setting the global variable dif to 1-3 to determine difficulty later on
def difficultysetting(input):
global dif #explain
if input.lower() == "e":
dif = 1
elif input.lower() == "m":
dif = 2
elif input.lower() == "h":
dif = 3
Returns the high score of whatever difficulty the user is playing on
def highscore():
if dif == 1:
return easyScore
elif dif == 2:
return mediumScore
elif dif == 3:
return hardScore
while isPlaying:
print(“Difficulties (E)asy, (M)edium, (H)ard”)
difficulty = difficultysetting(input("Choose a difficulty: "))
guess = -1 #explain
# Checking which difficulty user is playing on and generating a random number based on that
if dif == 1:
number = int(random.randint(1, 100))
elif dif == 2:
number = int(random.randint(1, 1000))
elif dif == 3:
number = int(random.randint(1, 10000))
print(number) # will remove
# Prompting user which numbers to guess between per difficulty
while guess != number:
while True:
try:
if dif == 1:
guess = int(input("Pick a number between 1-100: "))
elif dif == 2:
guess = int(input("Pick a number between 1-1000: "))
elif dif == 3:
guess = int(input("Pick a number between 1-10000: "))
break
except:
print("please enter a valid number")
# Lets user know if they've guessed to high or too low
if guess < number:
print("Higher")
attempts.append(guess)#adds into the attempt list
elif guess > number:
print("Lower")
attempts.append(guess)
else:
print("That's Correct!")
print("Guesses: ", len(attempts))
# Checking if new highscore is lower than old highscore and updating the variable if so
if dif == 1 and len(attempts) < easyScore:
easyScore = len(attempts)
elif dif == 2 and len(attempts) < mediumScore:
mediumScore = len(attempts)
elif dif == 3 and len(attempts) < hardScore:
hardScore = len(attempts)
# reseting list
attempts.clear()
attempts.append(0)
# Asking user if they'd like to play again and ending while loop if no
print("Highscore: ", str(highscore()))
playAgain = input("Do you want to play again? (Y/N): ")
if playAgain.lower() == "n":
isPlaying = False
//can anyone explain what does global dif mean? and is there any way to prevent the error when we enter a wrong data? i have only successfully done it for one part of it. Thank you