![]() ![]() Then, you just substitute the 6 for ATTEMPTS_FOR_LOST in the conditional snippet I showed a little above. At the top of the file, import the random module: import random Use the random module's randint() method to generate a random number between 1 and 50: guessrange 50 answer random. var num Math. So, the user guessed immediatly without doing nothing. Open the file using any text editor, such as Visual Studio or Atom. Just initialize it with : var guess 0 However be careful there's a possibility that num is initialize to 0. That is done like this: ATTEMPTS_FOR_LOSE = 6 Create a new file called number-guessing-game.py. Then, to take this step further and reduce magic numbers, create a constant at the top of your code that defines the number of attempts at which the user has lost. Then, at the end of your code and after the loop, you just need to set up some simple conditionals that check the attempts variable: if attempts < 6: This number is guessed by you via multiple attempts. The number is formed with digits from 0 to 9 each digit appears once at most. Then, every time the code loops (they enter an answer), you just increment the counter: num = random.randint(1, 100) Guess The Number is a game where you must use your logic in order to guess a 4-digit secret number selected by the computer at the beginning of the game. This can be done by using a simple counter for how many times the user has made an attempt. You should actually count up and tell the user if they won. Why are you splitting this on to two different lines? You can just merge the int() onto the line above it, and pass input() into it like this: guess = int(input())Īs Greg Hewgill mentioned in the comments, rather than saying this: print('if you gussed less than 6 times you won') This is not a very extensible design, again, I'd recommend creating a function that allows you to create custom games, like this: def number_guessing_game(low, high, rounds): By adding 1 answer will be in the range of 1-100. This will result in a number between 0 to 99. random () will generate a number between 0 and 1 so we multiply the generated number by 100. (This has been implemented below, for reference.) Generate a random number between 1 and 100 using the Math. The easiest way to do this would be to use a for. ![]() ![]() Rather than printing a message saying that if the user got below a certain amount of tries, they win, you can implement it into the code. Print("Guess a number between 1 and 100.") To set up something like this in your code, you'd change your code to something like this. User_integer = input("Enter an integer: ") you are now going to guess number guessing game guess number guess restart Built on Code Studio We thank our donors, partners, our extended team, our video cast, and our education advisors for their support in creating Code Studio. What you need to do is set up a try- except block, like this: try: What do you suppose happens if the user enters something like "abc"? Right now, as it stands, you're just converting any user input to a integer, using the int function. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |