Speak now
Please Wait Image Converting Into Text...
Embark on a journey of knowledge! Take the quiz and earn valuable credits.
Challenge yourself and boost your learning! Start the quiz now to earn credits.
Unlock your potential! Begin the quiz, answer questions, and accumulate credits along the way.
General Tech Learning Aids/Tools 2 years ago
Posted on 16 Aug 2022, this text provides information on Learning Aids/Tools related to General Tech. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.
Turn Your Knowledge into Earnings.
I've got a small little project for a coursera course and we have to design a "Guess The Number Game". We've also been instructed to give 7 guesses if the number is 1-100 and 10 guesses if it is 1-1000. My code is below. I'm having some issues with my if statements. When guesses == 0, it still makes the user enter another input before telling him the game is over and starting a new game. I need it to say game over and reset when guesses == 0, without making the user guess again. Where am I going wrong here? I've tried setting elif number_of_guesses == 0 as well, with the same issue. Thanks in advance!
# template for "Guess the number" mini-project # input will come from buttons and an input field # all output for the game will be printed in the console import simplegui import random # helper function to start and restart the game def new_game(): # initialize global variables used in your code here global secret_number global number_of_guesses secret_number = random.randrange(0, 101) print secret_number number_of_guesses = 7 print "" print "New Game! Current range is set to 0 - 100!" # define event handlers for control panel def range100(): # button that changes the range to [0,100) and starts a new game global secret_number global number_of_guesses secret_number = random.randrange(0, 101) number_of_guesses = 7 print secret_number print "New Game! Current range is set to 0 - 1000!" def range1000(): # button that changes the range to [0,1000) and starts a new game global secret_number global number_of_guesses secret_number = random.randrange(0, 1001) number_of_guesses = 10 print secret_number print "New Game! Current range is set to 0 - 1000!" def input_guess(guess): # main game logic goes here global number_of_guesses guess = int(guess) print "" print "Guess was: ", guess print "" if guess > secret_number and number_of_guesses > 0: print "Lower!" print "" number_of_guesses -= 1 print "Guesses Remaining: ", number_of_guesses print "" elif guess < secret_number and number_of_guesses > 0: print "Higher!" print "" number_of_guesses -= 1 print "Guesses Remaining: ", number_of_guesses print "" elif number_of_guesses > 0 and guess == secret_number: print "Correct!" new_game() elif number_of_guesses <= 1: print "Game Over! Out Of Guesses!" new_game else: print "S**t, something went wrong here." # create frame frame = simplegui.create_frame("Guess The Number", 175, 175) # register event handlers for control elements and start frame frame_input = frame.add_input("Your Guess: ", input_guess, 100) range_100 = frame.add_button("Range 1 - 100", range100, 100) range_1000 = frame.add_button("Range 1- 1000", range1000, 100) frame.start() # call new_game REPLY 0 views 0 likes 0 shares Facebook Twitter Linked In WhatsApp
I would refactor your code quite a bit, for example place Game Over in it's own function.
The reason you ask another question is you are decrementing the number of guesses after input, but not checking this before getting the next guess from the user. One quick fix would be to check the number of guesses after validating their answer:
def game_over(): print "Game Over! Out Of Guesses!" new_game();
def input_guess(guess): # main game logic goes here global number_of_guesses guess = int(guess) print "" print "Guess was: ", guess print ""
if guess > secret_number: print "Lower!" print "" number_of_guesses -= 1 elif guess < secret_number: print "Higher!" print "" number_of_guesses -= 1 elif number_of_guesses > 0 and guess == secret_number: print "Correct!" new_game() if number_of_guesses == 0: game_over(); else: print "Guesses Remaining: ", number_of_guesses print "" else: print "S**t, something went wrong here."
Still very messy, and untested but hopefully the logic makes sense
No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.
General Tech 9 Answers
General Tech 7 Answers
General Tech 3 Answers
General Tech 2 Answers
Ready to take your education and career to the next level? Register today and join our growing community of learners and professionals.