d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Anyone Familiar With Python?
Add Reply New Topic New Poll
Member
Posts: 15,099
Joined: Aug 5 2009
Gold: 177,462.00
Feb 20 2024 11:52pm
I need a script wrote up in python for a game that would be able to successfully guess a number that I randomly pick between 1 and 100.

must successfully guess my number in 10 guesses or less.

bonus if it can successfully do it with 7 guesses.

I can write the requirements in more detail if needed.

anyone able to help me with this?

I so far have got it working, but its becoming tedious and I am not very experienced and would like a complete one for reference.

if anyone is able to help me out with this, please message me :) thanks a ton!


so far basically i and using a yes/no : higher/lower type. first guess is 50, second guess is either 25 or 75. 3rd guess is either 12, 35, 60 or 90 etc, etc.

This post was edited by Terp on Feb 20 2024 11:56pm
Member
Posts: 72,628
Joined: Mar 20 2007
Gold: 7,623.00
Feb 21 2024 01:53am
Code
import random

def guess_number():
# Initialize the lower and upper bounds
lower_bound = 1
upper_bound = 100
attempts = 0

while True:
# Increment attempts
attempts += 1

# Guess the middle number
guess = (lower_bound + upper_bound) // 2
print(f"My guess is {guess}.")

# Check if the guess is correct
response = input("Is it correct? (yes/higher/lower): ").lower()

if response == "yes":
print(f"I guessed the number {guess} in {attempts} attempts!")
break
elif response == "higher":
lower_bound = guess + 1
elif response == "lower":
upper_bound = guess - 1
else:
print("Please enter a valid response (yes/higher/lower).")

# If the number hasn't been guessed in 10 attempts, exit the loop
if attempts >= 10:
print("I couldn't guess the number within 10 attempts.")
break

# Run the guessing game
guess_number()
Member
Posts: 1
Joined: Jun 10 2012
Gold: 0.00
Feb 21 2024 01:59am
Hey

Code

def guess_number():
low = 1
high = 100
print("Think of a number between 1 and 100, and I'll try to guess it.")
print("Respond with 'higher', 'lower', or 'correct' to my guesses.")

guesses = 0

while low <= high:
guess = (low + high) // 2
print(f"My guess is {guess}.")
feedback = input("Is your number higher, lower, or correct? ").lower()
guesses += 1

if feedback == 'higher':
low = guess + 1
elif feedback == 'lower':
high = guess - 1
elif feedback == 'correct':
print(f"Hooray! I've guessed your number in {guesses} tries!")
break
else:
print("Please enter 'higher', 'lower', or 'correct'.")

if low > high:
print("Hmm, it seems there might have been some confusion.")

guess_number()
Member
Posts: 51
Joined: Jan 12 2021
Gold: 0.21
Mar 11 2024 04:28am
Quote (ZeaLORDeaL @ Feb 21 2024 08:53am)
Code
import random

def guess_number():
# Initialize the lower and upper bounds
lower_bound = 1
upper_bound = 100
attempts = 0

while True:
# Increment attempts
attempts += 1

# Guess the middle number
guess = (lower_bound + upper_bound) // 2
print(f"My guess is {guess}.")

# Check if the guess is correct
response = input("Is it correct? (yes/higher/lower): ").lower()

if response == "yes":
print(f"I guessed the number {guess} in {attempts} attempts!")
break
elif response == "higher":
lower_bound = guess + 1
elif response == "lower":
upper_bound = guess - 1
else:
print("Please enter a valid response (yes/higher/lower).")

# If the number hasn't been guessed in 10 attempts, exit the loop
if attempts >= 10:
print("I couldn't guess the number within 10 attempts.")
break

# Run the guessing game
guess_number()


Why do you have "import random" ?
I cant see you using it anywhere in the code
Member
Posts: 51
Joined: Jan 12 2021
Gold: 0.21
Mar 11 2024 04:30am
Quote (Terp @ Feb 21 2024 06:52am)
I need a script wrote up in python for a game that would be able to successfully guess a number that I randomly pick between 1 and 100.

must successfully guess my number in 10 guesses or less.

bonus if it can successfully do it with 7 guesses.

I can write the requirements in more detail if needed.

anyone able to help me with this?

I so far have got it working, but its becoming tedious and I am not very experienced and would like a complete one for reference.

if anyone is able to help me out with this, please message me :) thanks a ton!


so far basically i and using a yes/no : higher/lower type. first guess is 50, second guess is either 25 or 75. 3rd guess is either 12, 35, 60 or 90 etc, etc.


For guessing i would say its best to use binary search midpoint,
And then for the command fully its not that hard,
You can post your version and let us see what you have?
Member
Posts: 4
Joined: Jan 23 2022
Gold: 4,000.00
Mar 12 2024 07:08pm
Quote (EzyHawk @ Mar 11 2024 06:28am)
Why do you have "import random" ?
I cant see you using it anywhere in the code


Based on the comments in the code, it smells like ChatGPT.
Member
Posts: 51
Joined: Jan 12 2021
Gold: 0.21
Mar 14 2024 11:45am
Quote (Helic @ Mar 13 2024 02:08am)
Based on the comments in the code, it smells like ChatGPT.


Probably,
So many using it, not knowing it often causes more issues then helping.

ChatGPT is a decent tool to use as references if you already know coding etc.
Actually copied the msg from terp

ChatGpt:
Code
def guess_number():
print("Welcome to the Number Guessing Game!")
print("Think of a number between 1 and 100, and I'll try to guess it in 7 attempts or less.")
input("Press Enter when you're ready...")

low = 1
high = 100
attempts = 0

while True:
guess = (low + high) // 2
attempts += 1

print(f"My guess is {guess}.")
response = input("Is it correct? (yes/no): ").lower()

if response == "yes":
print(f"Hooray! I guessed your number ({guess}) in {attempts} attempts!")
break
elif response == "no":
attempts_left = 7 - attempts
if attempts_left == 0:
print("Oops! I've run out of attempts. Better luck next time!")
break
else:
print(f"Okay, is your number higher or lower than {guess}?")
direction = input("Type 'higher' or 'lower': ").lower()
if direction == "higher":
low = guess + 1
elif direction == "lower":
high = guess - 1
else:
print("Invalid input. Please type 'higher' or 'lower'.")

if __name__ == "__main__":
guess_number()


And CoPilot
Code
import random

def guess_number():
# Generate a random number between 1 and 100
num = random.randint(1, 100)
print("Think of a number between 1 and 100. I'll try to guess it!")

# Initialize the search range
lower_bound, upper_bound = 1, 100

# Set the maximum number of guesses
max_guesses = 10

for attempt in range(max_guesses):
# Make a guess
guess = (lower_bound + upper_bound) // 2

print(f"My guess is {guess}.")

# Ask for feedback
response = input("Is my guess correct? (yes/no): ").strip().lower()

if response == "yes":
print(f"I guessed it! Your number is {guess}.")
break
elif response == "no":
# Adjust the search range based on feedback
if guess < num:
lower_bound = guess + 1
else:
upper_bound = guess - 1
else:
print("Please answer with 'yes' or 'no'.")

else:
print(f"Oops! I couldn't guess your number in {max_guesses} tries. It was {num}.")

# Call the function to play the game
guess_number()


And this was with you's ai
Code
import random

def guess_number():
low = 1
high = 100
attempts = 0
guess = 50

while attempts < 7:
print(f"My guess is {guess}.")
response = input("Is it correct? (yes, higher, lower): ").lower()

if response == "yes":
print("I guessed it right! Thanks for playing.")
return
elif response == "higher":
low = guess + 1
guess = (low + high) // 2
elif response == "lower":
high = guess - 1
guess = (low + high) // 2
else:
print("Invalid input. Please enter 'yes', 'higher', or 'lower'.")

attempts += 1

print("I couldn't guess the number in 7 attempts. Maybe next time!")
return

number_to_guess = random.randint(1, 100)
print("Welcome to the number guessing game!")
print("Pick a number between 1 and 100 in your mind and let's see if I can guess it.")
print("Please provide feedback with 'yes', 'higher', or 'lower'.")

guess_number()
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll