Ok So Basically my program is a number guessing game include stuff such as number of rounds, will identify if that number has been said or not already, if the number is out of the range (1 and 100). but after making a function (checks if its an integer )so i don't repeat my code in while loops my code seems to generate a new number to guess after every guess inputed and when typed in the same number again it will notice and return saying that the number has already been entered before but then will say if the number is to high or not which i dont want please help Thanks so much.
# Higher / Lower version 4
# Basic program works has loop but does not ask for num of rounds or store best score and says if number was already entered.
import random
# List for already said inputs
tried_num = []
# Score
best_score = 25
# Rounds
rounds_played = 0
valid_round = False
# Checks numeric input is valid
def int_check(question,min,max):
valid = False
while not valid:
try:
response = int(input(question))
if response < min or response > max:
print("\n Whoops! Please enter an INTEGER between {} and {}".format(min,max))
else:
valid = True
return(response)
except ValueError:
print("You did not enter an INTEGER. Please try again")
rounds_wanted = int_check("How many rounds would you like to play? (1 - 10): ",1,10)
while rounds_played < rounds_wanted:
# Generate random integer
gen_num = random.randrange(1,100)
print(gen_num)
# Guess
num_guesses = 0
guess = int_check("Guess the number between 1 and 100: ",1,100)
if guess not in tried_num:
num_guesses += 1
tried_num.append(guess)
else:
print("You already guessed that number, Try a differnet one \n")
# Num of guesses till round ends
if num_guesses > 26:
print("You Lose! You have used up your 25 guesses, the number was %s" % gen_num)
elif guess > gen_num:
print("Your guess was to high , Try a lower number \n")
elif guess < gen_num:
print("Your guess was to low , Try a higher number \n")
elif num_guesses == 1:
rounds_played = rounds_played + 1
print("Congratulations you have won, you guessed the number in 1 Guess!!")
else:
rounds_played = rounds_played + 1
print("Congratulations you have won, you guessed the number in %s guesses \n" % num_guesses)
# Update Best Score if number of guesses is lower
if num_guesses < best_score:
best_score = num_guesses
if best_score < 26 :
print("Your best score was: %s!" % best_score)
Aucun commentaire:
Enregistrer un commentaire