Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with code
#1
Hello wondering if someone could help, I'm kinda new to the whole coding thing and can't work out what I've done wrong. The code below should allow a user(already completed this part) to guess the name of a random song. they need to have 3 attempts.

Thanks for the support.

Song = "This is a song"

guess = input("What song is it?")

score = 0
guesscount = 0
guesscount += 1

while guess == guess:

    if guesscount == 3:
        print("You have had the maximum number of guesses, goodbye!")
        quit()

    elif guess == Song and guesscount == 1:
        print("Well done,you guessed correctly!")
        score += 3
        break

    elif guess == Song and guesscount == 2:
        print("Second time was the charm! You guessed correctly... this time.")
        score += 1
        break

    elif guess != Song:
        print("Your guess was incorrect!")
        guess == input("What song is it?")
        guesscount += 1
        continue
Reply
#2
You are making them lose for making three guesses (lines 11-13) without checking to see if the third guess is correct.

I would do this differently. I would have a loop that asks the question three times, and if the answer is correct, breaks out of the loop. Then after the loop, I would check the number of guesses made and the song name to determine the result.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Here's a better way (can be even better with dictionary):
song = "Mary had a little lamb".lower()

def guess_song(song, score):
    for guesscount in range(4): 
        if guesscount == 3: # range is zero based, so 0, 1, 2
            print("You have had the maximum number of guesses, goodbye!")
            break
        
        guess = input("Guess: {},  What song is it? ".format(guesscount+1)).lower()
        if not guess:
            print("You haven't a guess!")
            continue
        if guess == song:
            if guesscount == 0:
                print("Well done,you guessed correctly!")
                score += 3
            elif guesscount == 1:
                print("Second time was the charm! You guessed correctly... this time.")
                score += 2
            elif guesscount == 2:
                print('Better late than never')
                score += 1
            break
        else:
            print("Your guess was incorrect!")
            continue
    return score

if __name__ == '__main__':
    final_score = guess_song(song, 0)
    print('final score: {}'.format(final_score))
outout:
Output:
Guess: 1, What song is it? Peter had a little Goat Your guess was incorrect! Guess: 2, What song is it? pater had a big goat Your guess was incorrect! Guess: 3, What song is it? mary had a little lamb Better late than never final score: 1
Reply
#4
Here's what I recommend:

Song = "This is a song"
score = 0
guessStrings = {
    1: "Second time was the charm! You guessed correctly... this time.",
    2: "Well done,you guessed correctly!",
    3: "Took you long enough!"
}

for guesscount in range(1,4):
    guess = input("What song is it?")

    if guess == Song:
        if guesscount > 1:
            score += 1
        else: score += 3

        print(guessStrings.get(guesscount, "Missing response string"))
        break
    else:
        if guesscount == 3:
            print("You have had the maximum number of guesses, goodbye!")
        else: print("Your guess was incorrect!")
On line 3, I added a dictionary to store the responses you printed (plus one more for a successful third guess). This consolidates our responses for successful guesses into a single location. On line 17, the code retrieves the response based on the number of guesses with a default string ("Missing response string") if there isn't a string for that number of guesses.

On line 9, I changed your conditional for the while loop to:

for guesscount in range(1,4):
In general, you want to use for loops whenever possible as to avoid an infinite loop. Since you only want three guesses, "range(1,4)" will provide only three loops of the code. This also takes care of incrementing the guesscount which eliminates a few lines.

On line 10, I moved the input into the loop. This removes a duplicated line of code. We'll just reuse this one line each time.

On lines 12 through 15, I consolidated your scoring. Now, the code doesn't have to check the guesscount multiple times to determine the score.

On line 19, I added an else to account for instances when guess does not match Song. This eliminates a redundant conditional in the original. In the else block, we subsequently check for the guess limit. We don't have to call quit() to end the program. Because this uses a for loop which terminates at 3 (technically, it terminates when guesscount >= 4), the loop will automatically terminate. So, the check is merely to print the correct statement.
Reply
#5
Better logic structure
    if guess == song:
        if guess_count == 1:
            ....
        elif guess_count == 2:
            ....
        else:
            ....
    elif guess_count == 3:
        print("You have had the maximum number of guesses, goodbye!")
        quit()
    else:
        guess_count += 1
There is a place for further improvements - but that would be the first step
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#6
Thanks for the support would you be able to provide a link to instructions to complete this or if you have the time provide the solution. Cheers
Reply
#7
carlk see post 3, uses very similar logic
Reply
#8
Dude, you are drowning in help here. Show some effort.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#9
Thanks, volcano63 I've tried inputting however, I'm now receiving an error on line 12 stating the song is not defined

"raceback (most recent call last):
File "python", line 12, in <module>
NameError: name 'Song' is not defined
"
Any ideas

Regards
Reply
#10
Clearly there is no definition of Song in your code. Or at least not in a place where code at line 12 can access it.
Without seeing your code as it currently stands, we can but guess.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020