Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
3.5 kB
7
Indexable
Never
low = 1
high = 1000

print("Welcome to the game of Team A")
print(f"Please think of a number between {low} and {high}:")

input("Press any button to continue: ")

guesses = 1

while True:
    print(f"\nGuessing in the range of {low} and {high}")
    guess = low + (high - low) // 2
    
    # Ask the user if the guessed number is too high, too low, or correct
    user_input = input(f"My guess is {guess}. "
                       "\nShould I guess higher or lower? Enter 'higher' or 'lower', or 'c' if my guess is correct. "
                       "\nOr press 'g' to guess manually: ")
    
    if user_input.casefold() == 'higher': # If user inputs "higher", guess higher
        low = guess + 1
    
    elif user_input.casefold() == 'lower': # If user inputs "lower", guess lower
        high = guess - 1
    
    elif user_input.casefold() == 'c': # If user inputs "c", guess is correct
        print(f"I got it in {guesses} guesses")
        
        # Ask the user if they want to play again
        play_again = input("Do you want to play again? (yes/no)").casefold()
        
        if play_again == "yes":
            # Reset the game if user wants to play again
            low = 1
            high = 1000
            print(f"Please think of a number between {low} and {high}:")
            input("Press any button to continue: ")
            guesses = 1
            continue
        else:
            # End the game if user does not want to play again
            print("Thank you for playing!")
            break
    
    elif user_input.casefold() == 'g': # If user inputs "g", let them guess manually
        manual_guess = int(input("Enter your guess: "))
        
        if manual_guess < low or manual_guess > high: # Check if the manual guess is within the range
            print(f"Your guess should be between {low} and {high}")
            continue
        else:
            manual_input = input(f"Is {manual_guess} your number? (yes/no)")
            
            if manual_input.casefold() == "yes":
                print(f"I got it in {guesses} guesses")
                
                # Ask the user if they want to play again
                play_again = input("Do you want to play again? (yes/no)").casefold()
                
                if play_again == "yes":
                    # Reset the game if user wants to play again
                    low = 1
                    high = 1000
                    print(f"Please think of a number between {low} and {high}:")
                    input("Press any button to continue: ")
                    guesses = 1
                    continue
                else:
                    # End the game if user does not want to play again
                    print("Thank you for playing!")
                    break
            elif manual_input.casefold() == "no":
                # If the manual guess is not correct, tell the user whether the number is higher or lower
                higher_or_lower = "lower" if manual_guess < guess else "higher"
                print(f"Your number is {higher_or_lower}.")
                guesses += 1
                continue
            else:
                print("Invalid input. Please enter 'yes' or 'no'")
                continue
    else:
        print("Please enter only 'higher', 'lower', 'c', or 'g' ")
        continue
    guesses += 1