Alexander Fraser
Alexander Fraser

Reputation: 1

Python While Loop with multiple conditions using nested if statements

The following While loop obtains a randomly generated number and compares it to a user generated number. If the first guess is correct, tit allows the user to use to the name they entered in another module. If however the first guess is incorrect, but the second guess is, it is supposed to output a hardcoded name. If the second guess is incorrect, it should inform the user that all guesses were incorrect and that they do not have superpowers. The problem is I can get the program to work for the if and else statements but not the elif. Please help.

def getUserName():
    print('Welcome to the Superhero Name Game v 2.0')
    print('Copyright \N{COPYRIGHT SIGN}2018. Alex Fraser')

    userName=input('Please enter the superhero name you wish to use. ')
    return userName

def getUserGuess():
    import random
    x = random.randint(1,10)
    userGuess = int(input('Please enter a number between 1 and 10. '))
    return x, userGuess

def superName(n, r, g):
    guessCount = 1
    while guessCount < 3:
        if g == r and guessCount == 1:
            #hooray
            print(f'Congrats! You can use your chosen name. Your superhero name is {n}')
            return
        elif g == r and guessCount == 2:
            #meh good effort
            print('Your superhero name is Captain Marvel.')
            return
        else:
            getUserGuess()
            print(r,g)
        guessCount += 1
    print('All your guesses were incorrect. Sorry you do not have super powers')
    print(f'The number you were looking for was {r}')


n = getUserName()    
r, g = getUserGuess()
print(r,g)
superName(n,r,g)

Upvotes: 0

Views: 8140

Answers (4)

Alexander Fraser
Alexander Fraser

Reputation: 1

Thanks to @ytpillai for the solution. With a slight modification to limit the number of guesses to 3. Regardless of whether guess 3 is correct or not the user is to get the same message.

def getUserName():
    print('Welcome to the Superhero Name Game v 2.0')
    print('Copyright \N{COPYRIGHT SIGN}2018. Alex Fraser')

    userName=input('Please enter the superhero name you wish to use. ')
    return userName

GUESS_COUNT_LIMIT = 2
def getUserGuess():
    return int(input('What is your guess? '))
def superName(n, r, g):
    guessCount = 1
    if g == r:
        print(f'Congrats! You can use your hero name. Your superhero name is {n}')
        return
    g = getUserGuess()
    if g == r:
        print('Your superhero name is Captain Marvel')
        return

    while g != r and guessCount < GUESS_COUNT_LIMIT:
        g = getUserGuess()

        if g == r:
            print('All your guesses were incorrect. Sorry you do not have super powers')
            return
        guessCount +=1 



    print('All your guesses were incorrect. Sorry you do not have super powers')


import random
superName(getUserName(), random.randint(1, 10),getUserGuess())

Upvotes: 0

Yann Vernier
Yann Vernier

Reputation: 15887

You're iterating over a limited number of tries. I feel it's more natural to convert this into a search-style for:

def superName(n, r):    # Note, we ask for all attempts, no initial guess
    for guessCount in (1,2):
        r,g = getUserGuess()
        print(r,g)
        if g == r:
            if guessCount == 1:
                #hooray
                print(f'Congrats! You can use your chosen name. Your superhero name is {n}')
                return
            elif guessCount == 2:
                #meh good effort
                print('Your superhero name is Captain Marvel.')
                return
            # Note: that could've been an else
            # We have covered every case of guessCount
    else:    # Not necessary since we return instead of break
        print('All your guesses were incorrect. Sorry you do not have super powers')
        print(f'The number you were looking for was {r}')

We can go a step further and iterate over the messages instead:

def superName(n, r):    # Note, we ask for all attempts, no initial guess
    for successmessage in (
            f'Congrats! You can use your chosen name. Your superhero name is {n}',
            'Your superhero name is Captain Marvel.' ):
        r,g = getUserGuess()
        print(r,g)
        if g == r:
            print(successmessage)
            break   # We've found the appropriate message
    else:    # Not necessary if we return instead of break
        print('All your guesses were incorrect. Sorry you do not have super powers')
        print(f'The number you were looking for was {r}')

I noticed the getUserGuess calls didn't actually change g. You'll probably want to reconsider that (this revision changes r too, which is likely also not what you want). This would explain why you never see the second success message; you entered a second guess but the program checked the first guess again.

Upvotes: 0

Adam Smith
Adam Smith

Reputation: 54213

Your else clause doesn't make sense to be where it is. It's syntactically valid, but logically doesn't make sense. What you've written is:

while you haven't guessed three times:
  check if it's a correct guess on the first try. If so, use the user's choice name
  check if it's a correct guess on the second try. If so, assign the user a name.
  for any other guess, tell the user they've failed and break out of the while.

You want the logic for "tell the user they've failed" to only trigger after the while loop ends, since the while loop is enforcing the "do it three times" thing.

while guess_count < 3:
    if g == r and guess_count == 1:
        # hooray
        return
    elif g == r and guess_count == 2:
        # meh
        return
    else:
        # this is just one incorrect guess -- you should probably
        # prompt the user to guess another number to change the value of g
    guess_count += 1
# boo, you failed to guess

Upvotes: 0

rassa45
rassa45

Reputation: 3550

You don't need to break out of an if/elif/else conditional. These are NOT loops. elif and else will only run if the elif and if conditions above them fail. All you are doing with your break statements is breaking out of your while loop.

Upvotes: 0

Related Questions