Question

Write a program that allows the user to play a guessing game. The game will choose a secret number, a positive integer lessHello! we are using Python to write this program. we are supposed to use loops in this assignment. I would greatly appreciate the help! Thank you!

0 0
Add a comment Improve this question Transcribed image text
Answer #1

CODE with appropriate comments :

print ("Welcome to the guessing game. You have ten tries to guess my number") # priting Welcome message

guessesTaken = 0 # number of guesses taken
secretnumber = 1458 # thesecret number

while guessesTaken < 10: # outer loop to control number of guesses taken
guess = int(input('Please enter your guess : '))

while guess < 1 or guess > 9999: # looping until a valid input number is entered
print("Your guess must be between 0001 and 9999") # prompting to enter a valid number
guess = int(input("Please enter a valid guess : ")) # taking input number (valid one)
  
guessesTaken = guessesTaken + 1 # counting valid guesses taken
  
if guess < secretnumber: # checking if the number is samller than the secret number
print ('Your guess is too low.\nGuesses so far : ' + str(guessesTaken)) # prompting that the input is too low
if guess > secretnumber: # checking if the number is greater than the secret number
print ('Your guess is too high.\nGuesses so far : ' + str(guessesTaken)) # prompting that the input is too high
if guess == secretnumber: # if number entered is equal to the secret number, breking the loop (next line)
break


if guess == secretnumber: # if the guess was right
if guessesTaken == 1: # and if it was the first try
print('That\'s correct, congratulations you guessed it on the first try') # priting appropriate winning message
else: # and if it was NOT the first try
print('That\'s correct, congratulations you guessed it in ' + str(guessesTaken) + ' guesses') # priting appropriate winning message
if guess != secretnumber: # if the guess was not right (ini 10 attempts)
print('Game over, you ran out of guesses') # priting appropriate losing message

SCREENSHOTS:

1.

main.py 1 print (Welcome to the guessing game. You have ten tries to guess my number) # priting Welcome message 3 guesses T

2. Winning in the 1st try

main.py 1 print (Welcome to the guessing game. You have ten tries to guess my number) # priting Welcome message 3 guesses T

3. Winning in the 2nd attempt

main.py 1 print (Welcome to the guessing game. You have ten tries to guess my number) # priting Welcome message 3 guesses T

Add a comment
Know the answer?
Add Answer to:
Hello! we are using Python to write this program. we are supposed to use loops in...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a Java application program that plays a number guessing game with the user. In the...

    Write a Java application program that plays a number guessing game with the user. In the starter code that you are given to help you begin the project, you will find the following lines of code: Random generator = args.length == 0 ? new Random() :                    new Random(Integer.parseInt(args[0])); int secret = generator.nextInt(100); You must keep these lines of code in your program. If you delete these lines, or if you change thse lines in any way, then your program...

  • Using C++ language P2-4 Write a program that randomly generates an integer and then prompts the...

    Using C++ language P2-4 Write a program that randomly generates an integer and then prompts the user to guess the number. If the user guesses the number correctly, the program outputs an appropriate message such as "You win!" and terminates. Otherwise, the program checks and tell the user whether the guessed number is less or greater than the target number and then prompts him for another try. However, the program gives the user five tries only to guess the correct...

  • For this lab you will write a Java program using a loop that will play a...

    For this lab you will write a Java program using a loop that will play a simple Guess The Number game. Th gener e program will randomly loop where it prompt the user for a ate an integer between 1 and 200 (including both 1 and 200 as possible choices) and will enter a r has guessed the correct number, the program will end with a message indicating how many guesses it took to get the right answer and a...

  • This program will implement a simple guessing game... Write a program that will generate a random...

    This program will implement a simple guessing game... Write a program that will generate a random number between 1 and 50 and then have the user guess the number. The program should tell the user whether they have guessed too high or too low and allow them to continue to guess until they get the number or enter a 0 to quit. When they guess the number it should tell them how many guesses it took. At the end, the...

  • Lab #6 Number Guessing Game – Loops and Nested Loops Requirements: Design, develop, test, and submit a program for a Number Guessing game. The program will select a random number between 0 and 100, al...

    Lab #6 Number Guessing Game – Loops and Nested Loops Requirements: Design, develop, test, and submit a program for a Number Guessing game. The program will select a random number between 0 and 100, allow the user to try up to 10 times to guess the number, and tell the user if each guess is too high, too low, or correct. The actual game portion must be a function...you can use more than one function in your program. The amount...

  • Do in Python please provide screenshots aswell. Here are the requirements for your game: 1. The...

    Do in Python please provide screenshots aswell. Here are the requirements for your game: 1. The computer must select a word at random from the list of avail- able words that was provided in words.txt. The functions for loading the word list and selecting a random word have already been provided for you in ps3 hangman.py. 2. The game must be interactive; the flow of the game should go as follows: • At the start of the game, let the...

  • Can you add code comments where required throughout this Python Program, Guess My Number Program, and...

    Can you add code comments where required throughout this Python Program, Guess My Number Program, and describe this program as if you were presenting it to the class. What it does and everything step by step. import random def menu(): #function for getting the user input on what he wants to do print("\n\n1. You guess the number\n2. You type a number and see if the computer can guess it\n3. Exit") while True: #using try-except for the choice will handle the...

  • Python Program Python: Number Guessing Game Write a Python function called "Guess.py" to create a number...

    Python Program Python: Number Guessing Game Write a Python function called "Guess.py" to create a number guessing game: 1. Function has one input for the number to Guess and one output of the number of attempts needed to guess the value (assume input number will always be between 1 and 1000). 2. If no number was given when the function was called, use random.randint(a,b) to generate a random number between a=1 and b=1000. You will also need to add an...

  • I have to use java programs using netbeans. this course is introduction to java programming so...

    I have to use java programs using netbeans. this course is introduction to java programming so i have to write it in a simple way using till chapter 6 (arrays) you can use (loops , methods , arrays) You will implement a menu-based system for Hangman Game. Hangman is a popular game that allows one player to choose a word and another player to guess it one letter at a time. Implement your system to perform the following tasks: Design...

  • Write a program to play "guess my number". The program should generate a random number between...

    Write a program to play "guess my number". The program should generate a random number between 0 and 100 and then prompt the user to guess the number. If the user guesses incorrectly the program gives the user a hint using the words 'higher' or 'lower' (as shown in the sample output). It prints a message 'Yes - it is' if the guessed number is same as the random number generated. ANSWER IN C LANGUAGE. ====================== Sample Input / Output:...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT