') if guess < maxPossible: maxPossible guess - 1 elif guess < thenumber: print('Player, guess higher. This is for re-usability of your program, and you can read more about it here: What does if _name_ = “_main_”: do?. 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. you have to guess a number between 1 to 100 and you have ten chances so lets start. For every number you guess, if you didn't guess the number correctly, you will be told whether your guess is greater than or less than the actual number the game has in mind. The rules of this game are straightforward. 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. You can play this game below (or open it in a new window ): Your goal is to guess the number the game has in mind. 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): 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. 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(). Do not forget to check out this playlist here which includes numerous projects based on HTML, CSS, and. For quality of life, after the user has clicked the Guess button, re-select the guess entry. Consider telling the user what their minimum and maximum guess is permitted to be. Get ready to learn something fun and exciting today. Consider using a spinbox instead of an unconstrained entry box for the guess, so that lower and upper limits are automatically enforced. To build this project we need HTML, CSS, and Javascript. In this tutorial, we take a look at how to build the ‘Guess the Number’ game. 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) Welcome to another new tutorial by Coding Artist. If (remaining_guesses = 0 and guess != num_range): # Restart the game if player is correct or out of guesses Print "Number of remaining guesses is %i" %(remaining_guesses) # New game, range from 0 - 100 by default #Globals, random guess and player's remaining guesses 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. |