Question

Python Program

Python: Number Guessing Game Write a Python function called Guess.py to create a number guessing game: 1. Function has one

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

import random
def guess(n):#method to count and return the number of guess
count=0
while True:
g = input("Enter guess number:(quit to exit)")
if g== 'quit':#if quit is entered
return 0#if he quits
m = int(g)
count=count+1
if(m==n):
print("Just rigth! You guessed it correctly")
return count
elif(m<n):
print("Lower")
else:
print("higher")
a=1
b=1000
n = random.randint(a,b)#generating a random number between 1 and 1000
print("Number of guesses: "+str(guess(n)))

output1:

Enter guess number:(quit to exit)10
Lower
Enter guess number:(quit to exit)200
Lower
Enter guess number:(quit to exit)500
Lower
Enter guess number:(quit to exit)5000
higher
Enter guess number:(quit to exit)2500
higher
Enter guess number:(quit to exit)1000
higher
Enter guess number:(quit to exit)700
Lower
Enter guess number:(quit to exit)800
Lower
Enter guess number:(quit to exit)900
Lower
Enter guess number:(quit to exit)950
Lower
Enter guess number:(quit to exit)980
Lower
Enter guess number:(quit to exit)990
Lower
Enter guess number:(quit to exit)995
Lower
Enter guess number:(quit to exit)998
Just rigth! You guessed it correctly
Number of guesses: 14


output2:

Enter guess number:(quit to exit)10
Lower
Enter guess number:(quit to exit)quit
Number of guesses: 0


main.py 1 import random 2. def guess(n): #method to count and return the number of guess count=0 while True: g - input(Enter

Add a comment
Answer #2

Answer:

import random
def guessGame(num=-1):
    if num == -1:
        num = random.randint(1, 100)
    print(num)
    guess = 0
    while True:
        guess = guess + 1
        n = input("Guess the number or enter quit: ").strip()
        if n == "quit":
            break
        else:
            n = int(n)
            if n == num:
                print("You got it in "+str(guess)+" tries!")
                break
            elif n > num:
                print("Your selected number is High")
            elif n < num:
                print("Your selected number is Low")
Add a comment
Know the answer?
Add Answer to:
Python Program Python: Number Guessing Game Write a Python function called "Guess.py" to create a number...
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
  • on python i need to code a guessing game. After the player has guessed the random...

    on python i need to code a guessing game. After the player has guessed the random number correctly, prompt the user to enter their name. Record the names in a list along with how many tries it took them to reach the unknown number. Record the score for each name in another list. When the game is quit, show who won with the least amount of tries. this is what i have so far: #Guess The Number HW assignment import...

  • FIX CODE-- import random number=random.randint ('1', 'another number') print("Hello, CIS 101") print("Let's play a guessing Game!called...

    FIX CODE-- import random number=random.randint ('1', 'another number') print("Hello, CIS 101") print("Let's play a guessing Game!called guess my number.") print("The rules are: ") print("I think of a number and you'll have to guess it.") guess = random.randint(1, 5) print("You will have " + str(guess) + " guesses.") YourNumber = ??? unusedguesses=int(guess) while unusedguesses < guess:     if int(YourNumber) == number:         print("YAY, You have guessed my number")     else:         unusedguesses=int(guess)-1         if unusedguesses == 0:              break         else:             print("Try again!") print("The number was ", str(number))

  • 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...

  • python Exercise: ex9.py Write a program that plays a number and you guess the number. The...

    python Exercise: ex9.py Write a program that plays a number and you guess the number. The program helps you with a response 1t the number you guessed is higher or lower than the mystery number. To generate a random number, we need to import some routines. guessing game. It generates a random mystery import random mysteryNumber random.randint (1, 100) The random.randint (1, 100) creates a random integer between 1 and 100. Here is a sample output: Let's play the guessing...

  • python code for guessing game users enter the number the computer will guess 8:38 PM 100...

    python code for guessing game users enter the number the computer will guess 8:38 PM 100 nstructure.com 1. Number guessing game : (5 points) 1) Requirements: Your program will ask the user to enter a number between 1 and 1000. Then it will try to guess the correct number with the guaranteed minimal average number of guesses. Which search algorithm should you use? 2) Expected Output: Enter a number between 1 and 1000? 784 Computer guesses: 500 Type 'l' if...

  • Swift program: Develop an app that is a Number Guessing Game, (ex: one that allows a...

    Swift program: Develop an app that is a Number Guessing Game, (ex: one that allows a player to guess a number between a high and a low number, say between 1 and 10) with the system guiding the player by indicating whether the number is too high or too low after an incorrect guess. This process is repeated until the player correctly guesses the number or simply quit your app. For simplicity purposes, assume the magic number to be guessed...

  • 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...

  • 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...

  • For a C program hangman game: Create the function int play_game [play_game ( Game *g )]...

    For a C program hangman game: Create the function int play_game [play_game ( Game *g )] for a C program hangman game. (The existing code for other functions and the program is below, along with what the function needs to do) (Also the link to program files (hangman.h and library file) is below the existing code section. You can use that to check if the code works) What int play_game needs to do mostly involves calling other functions you've already...

  • C++ Guessing game. Create a project titled Lab4_Guess with a single file titled guess.cpp Program the...

    C++ Guessing game. Create a project titled Lab4_Guess with a single file titled guess.cpp Program the following game. The computer selects a random number between 1 and 100 and asks the user to guess the number. If the user guesses incorrectly, the computer advises to try larger or smaller number. The guessing repeats until the user guesses the number. The dialog should look as follows (user input is shown in bold): Guess a number between 1 and 100: 50 try...

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