Posts: 3
Threads: 1
Joined: Oct 2018
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
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
|
Posts: 4,220
Threads: 97
Joined: Sep 2016
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.
Posts: 12,036
Threads: 486
Joined: Sep 2016
Here's a better way (can be even better with dictionary):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
song = "Mary had a little lamb" .lower()
def guess_song(song, score):
for guesscount in range ( 4 ):
if guesscount = = 3 :
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
Posts: 443
Threads: 1
Joined: Sep 2018
Oct-06-2018, 03:29 PM
(This post was last modified: Oct-06-2018, 03:29 PM by stullis.)
Here's what I recommend:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
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:
1 |
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.
Posts: 566
Threads: 10
Joined: Apr 2017
Better logic structure
1 2 3 4 5 6 7 8 9 10 11 12 |
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.
Posts: 3
Threads: 1
Joined: Oct 2018
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
Posts: 12,036
Threads: 486
Joined: Sep 2016
carlk see post 3, uses very similar logic
Posts: 4,220
Threads: 97
Joined: Sep 2016
Dude, you are drowning in help here. Show some effort.
Posts: 3
Threads: 1
Joined: Oct 2018
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
Posts: 1,150
Threads: 42
Joined: Sep 2016
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.
|