0

I need for the questioning part of the code to occur until the player has guessed the number.

I'm a bit of a beginner, and I'm writing a simple code for guessing a number. I've gotten to a point where I have everything I need, but I need it so the script runs until they've guessed the number. I'm not sure if I'm forgetting or if my book is outdated on how to do this. Thanks for any help!

randomnumber = random.randint(1,101)

questioning = input("Guess a number!: ")
answered_edited = int(questioning)
randmnew = int(randomnumber)
if answered_edited == randmnew:
print("You've won! Just don't get too ahead of yourself /n since this was 
only a simple game any child could play.")
elif answered_edited < randmnew:
print("It's greater than that you twat")
elif answered_edited > randmnew:
print("You're a little low shawty")

I would like the script to run and continually ask the question of what number until the player guesses

ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
  • 3
    This is basically just a variant of [Asking the user for input until they give a valid response](https://stackoverflow.com/q/23294658/364696). – ShadowRanger Aug 17 '19 at 03:09

3 Answers3

1

Alternative variation:

  • Wrapped in a function
    • type hints only work from python 3.5
  • Removed separate while loop variable
  • Replaced if-else with a dict
    • output is inside the while loop so the keys get evaluated with each new guess
    • dicts must have unique keys, if there are non-unique keys, the last key matched has its value returned.
    • In the case of output, there will always be two False keys and one True key; as with the if-else conditions, True is the one that matters.

Code:

import random

def game(low: int=1, high: int=100):

    guess, random_number = -1, random.randint(low, high+1)

    while guess != random_number:

        guess = int(input(f"Guess an integer from {low} to {high}!: "))

        output = {guess > random_number: "You're a little high, shawty...\n",
                  guess < random_number: "It's greater than that, you twat!\n",
                  guess == random_number: "You've won! Just don't get too ahead of yourself,\n"
                                          "since this is only a simple child's game."}

        print(output[True])

call game for (1, 100):

game()

call game with alternative interval:

game(1, 1000)
Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
0
 won=False
 randomnumber = random.randint(1,101)
 while won==False:
         questioning = input("Guess a number!: ")
         answered_edited = int(questioning)
         randmnew = int(randomnumber)
         if answered_edited == randmnew:
                   print("You've won! Just don't get too 
                   ahead of yourself /n since this was 
                   only a simple game any child could play.")
                   won=True
         elif answered_edited < randmnew:
                   print("It's greater than that you twat")
         elif answered_edited > randmnew:
                   print("You're a little low shawty")

You can write randomnumber=random. randint(1,101) into while loop if you want change the random number until than user win

ansev
  • 30,322
  • 5
  • 17
  • 31
0

Sounds like a perfect use case for a while-loop:

import random

random_number = random.randint(1, 101)

user_gussed_correctly = False

while not user_gussed_correctly:
    user_answer = int(input("Guess a number!: "))
    if user_answer < random_number:
        print("It's greater than that you twat!")
    elif user_answer > random_number:
        print("You're a little high shawty...")
    else:
        print("You've won! Just don't get too ahead of yourself\n"
              "since this was only a simple game any child could play.")
        user_gussed_correctly = True

Example Usage:

Guess a number!: 50
It's greater than that you twat!
Guess a number!: 75
It's greater than that you twat!
Guess a number!: 82
It's greater than that you twat!
Guess a number!: 91
It's greater than that you twat!
Guess a number!: 96
You're a little high shawty...
Guess a number!: 93
You've won! Just don't get too ahead of yourself
since this was only a simple game any child could play.
Sash Sinha
  • 18,743
  • 3
  • 23
  • 40