1

I wanted to create a guessing game to get more comfortable programming, The user has up to 100 guesses(yes more than enough). If the number is too high or too low it have them type in a new input, if its correct it will print correct.Now I simply want to have it setup to where I ask them would they like to play again. I think I have an idea of to set it up, by separating them into two functions? I am aware that is not currently a function but should put this as a fucntion and then put my question as an if statement in its own function?

import random


randNum = random.randrange(1,21)
numguesses = 0

while numguesses < 100:
        numguesses = numguesses + 1
        userguess = int(input("What is your guess [1 through 20]?"))
        if userguess < 1:
            print("Too Low")
            print("Please enter a valid guess [1-20]!")  
        elif userguess > 20:
                print("Too High")
        elif userguess == randNum:
            print("Correct")
            print("you used",numguesses,"number of guesses")

4 Answers4

1

Here's a simple way to do as you asked.I made a function and when you get the thing correct it asks if you want to play again and if you enter "yes" then it resets the vars and runs the loop again. If you enter anything but "yes" then it breaks the loop which ends the program.

import random

def main():
    randNum = random.randrange(1,21)
    numguesses = 0

    while numguesses < 100:
            numguesses = numguesses + 1
            userguess = int(input("What is your guess [1 through 20]?"))
            if userguess < 1:
                print("Too Low")
                print("Please enter a valid guess [1-20]!")
            elif userguess > 20:
                    print("Too High")
            elif userguess == randNum:
                print("Correct")
                print("you used",numguesses,"number of guesses")
                x = input("would you like to play again?")
                if x == "yes":
                    main()
                else:
                    break

main()
thesonyman101
  • 791
  • 7
  • 16
  • 1
    Yeah. A few things to note, however. You should give a brief of explanation of what it is you are suggesting to do. Would be important to also note that, as is, in the original solution, they never actually break even if the answer is correct. Also, I'm pretty sure OP is using Python 3, so your `raw_input` should be `input` or at least specify the usage per version difference. – idjaw Oct 30 '16 at 20:55
  • Ok thanks for the comment I'll fix my answer – thesonyman101 Oct 30 '16 at 20:57
0

Here is another way to do

import random

randNum = random.randrange(1,21)
numguesses = 0
maxGuess = 100

print("Guessing number Game - max attempts: " + str(maxGuess))
while True:
    numguesses +=1
    userguess = int(input("What is your guess [1 through 20]? "))
    if userguess < randNum:
        print("Too Low")
    elif userguess > randNum:
        print("Too High")
    else:
        print("Correct. You used ",numguesses," number of guesses")
        break    
    if maxGuess==numguesses:
        print("Maximum attempts reached. Correct answer: " + str(randNum))
        break
Arwan Khoiruddin
  • 436
  • 3
  • 13
0
import random


randNum = random.randrange(1, 21)
guess = 0

response = ['too low', 'invalid guess', 'too hight', 'correct']


def respond(guess):
    do_break = None # is assigned True if user gets correct answer

    if guess < randNum:
        print(response[0])

    elif guess > randNum:
        print(response[2])

    elif guess < 1:
        print(response[1])

    elif guess == randNum:
        print(response[3])

        do_continue = input('do you want to continue? yes or no')

        if do_continue == 'yes':
            # if player wants to play again start loop again
            Guess()
        else:
            # if player does'nt want to play end game
            do_break = True # tells program to break the loop

    # same as ''if do_break == True''
    if do_break:
        #returns instructions for loop to end
        return True


def Guess(guess=guess):
    # while loops only have accesse to variables of direct parent
    # which is why i directly assigned the guess variable to the Fucntion
    while guess < 100:
        guess -= 1

        user_guess = int(input('What is your guess [1 through 20]?'))

        # here the respond function is called then checked for a return
        # statement (note i don't know wheter this is good practice or not)
        if respond(user_guess):
            # gets instructions from respond function to end loop then ends it
            break

Guess()
Nex_Lite
  • 358
  • 2
  • 7
-1

Yet another way with two while loops

answer = 'yes'
while answer == 'yes':
    while numguesses < 100:
        numguesses = numguesses + 1
        userguess = int(input("What is your guess [1 through 20]?"))
        if userguess < 1:
            print("Too Low")
            print("Please enter a valid guess [1-20]!")
        elif userguess > 20:
            print("Too High")
        elif userguess == randNum:
            print("Correct")
            print("you used",numguesses,"number of guesses")
            break  #Stop while loop if user guest, hop to the first loop with answer var
    answer = raw_input("Would you like to continue? yes or no\n>")
Taporp
  • 78
  • 1
  • 7