Guess the numbe rcode7/29/2023 ![]() ![]() Allows you to set the initial random number range by altering the lLow and lHigh starting values in the code. These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. Doesn't error if the user accidentally (or deliberately) enters text instead of a number in the Input Box, or clicks 'Cancel' or the 'X' at top right to exit the Input Box. Displays the information about how to exit if you want. Import the random module to use the ed() and random. Question: 8.15LAB: Guess the random number Given the code that reads a list of integers, complete the numberguess () function, which should choose a random number between 1 and 100 by calling random.randint () and then output if the guessed number is too low, too high, or correct. ') if guess < maxPossible: maxPossible guess - 1 elif guess < thenumber: print('Player, guess higher. If your guess is too high or too low, you'll get a hint. Guides the user on what range to guess in by constantly updating that information in the InputBox header. 8.15 LAB: Guess the random number Given the code that reads a list of integers, complete the numberguess() function, which should choose a random number between 1 and 100 by calling random.randint() and then output if the guessed number is too low, too high, or correct. 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. Guess the number - App Lab - Guess the Number Guess the magic number in the my hat. Im overwriting the numberToGuess variable and calling checkNumber again to run different code paths inside our checkNumber function. I'm not suggesting that you necessarily use all aspects of this code, but offer it for possible options. So here is another alternative to consider or use coding ideas from. I would rather say to keep them both in mind and choose the one that best suits the situation and/or your coding comfort/style. I would not take as blanket guidance the statement: "Avoid IF when you need multiple selection options on a single comparison."īoth have pros and cons. As a user, I found not having any idea of how big the number might be, or any obvious way to exit the game if I got sick of it somewhat annoying.ģ. (It saves a few characters of typing as well as the wasted conversion process )Ģ. This can be done by using a simple counter for how many times the user has made an attempt. Therefore, I would recommend using Long in the first place. guess int (input ()) As Greg Hewgill mentioned in the comments, rather than saying this: print ('if you gussed less than 6 times you won') You should actually count up and tell the user if they won. Whilst 'Integer' is a valid variable type, vba converts Integer variables to Long variables before working with them. ![]()
0 Comments
Leave a Reply.AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |