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
  • Number Guessing Game Games Write program in C++. For this game, the computer will select a...

    Number Guessing Game Games Write program in C++. For this game, the computer will select a random number between 1 and 100 (inclusive). The computer will then ask the (human) player to guess the number the computer has selected. After the player’s guess is input to the computer, the computer will output one of three responses, depending on the relationship of the number the player guessed to the number the computer selected: “Your guess was too low.” “Your guess was...

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

  • Required in JAVA language Write a program that enables a user to play number guessing games....

    Required in JAVA language Write a program that enables a user to play number guessing games. The following is a nutshell description of a number guessing game. + a random number is generated + loop prompting the user to enter guesses until the user guesses the number or hits the maximum number of allowed guesses or enters the "quit" sentinel value The rest of this specification documents number guessing games in more detail. The documentation uses manifest constants that can...

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

  • Java Guessing Game Class The class will generate a random number of 1 to 15, and...

    Java Guessing Game Class The class will generate a random number of 1 to 15, and then check to see if the user guessed the number correctly. If the number is incorrect, the user should have the chance to guess again, until they guess the right number or they guess 10 times. The class method should keep track of the number of guesses the user has had, and return this value.   The class should have one constructors, a default and...

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