7/4/2023 0 Comments Guess the numbe rcode![]() I wont give you a complete python code, but I will try to guide you. This is for re-usability of your program, and you can read more about it here: What does if _name_ = “_main_”: do?. userGuess input(Guess the number between 1 and 10: ). Youll learn how to convert values to different. ![]() You can tell the user how many guesses they have left in the function that controls the game.įinally, you should have the part of the program that always runs in an if _name_ = "_main_": block. This is a good game to code because it uses random numbers, loops, and input from the user in a short program. Also, try not to write functions this long.Īs for your range100() and range1000() functions, I would combine those into one function like this: def get_num(min, max): as executable Python code: import random number random.randint(1, 100) attempts 0 count no of attempts to guess the number guess 0 while guess. Now inside the container, using the h3> tag, we will add a heading to the number guessing game. Once the number is correctly guessed, that function just returns to its caller, who can call it again if the user wants to play again. Create the following div container and add the code inside the body tag for the number guessing game. This way, only the function that plays the game needs to know the number of moves left. ![]() I would create a function to run the program, one to input the number, and one to do the comparison and output prompts about being too large or small. That function inputs the guess, compares the guess, and plays the entire game. You have a function called input_guess(). Print "The correct answer was %i" %(num_range)Įlif (remaining_guesses = 0 and guess = num_range):į = simplegui.create_frame("Guess the number", 200, 200)į.add_button("Range is ", range100, 200)į.add_button("Range is ", range1000, 200) If (remaining_guesses = 0 and guess != num_range): Variables are basically containers for values (such. # Restart the game if player is correct or out of guesses This section of the code sets up the variables and constants we need to store the data our program will use. ![]() Print "Number of remaining guesses is %i" %(remaining_guesses) # New game, range from 0 - 100 by default ') if guess < maxPossible: maxPossible guess - 1 elif guess < thenumber: print('Player, guess higher. I dont understand how to make it so when you guess the number correctly it will display a message ( you have guessed correctly) for each number separately. #Globals, random guess and player's remaining guesses import random thenumber random.randint(1, 4) guess 0 print(thenumber) minPossible 0 maxPossible 100 while guess thenumber: guess int(input('Please enter a number: ')) if guess > thenumber: print('Player, guess lower. I'm asking you to help me figure out where I could make it simpler. Right now the program is doing what it's supposed to do, my only concern is that it might be too long for nothing. I am aware that globals are a bad idea, but that is what I have learned so far in my course. ![]()
0 Comments
Leave a Reply. |