Ben
Ben

Reputation: 39

What is making this print a random letter after the first letter?

I'm trying to make a program that will print an artist and the first letter of their song and the user has to guess the song. I can print the artist and then the first letter of the song but it also prints a random letter and then the first letter again after i type the correct song in. Why is this?

artist = ["NF","Jetta","Sickick","Kodaline","Eminem"]

#          0     1        2         3         4

song = ["Destiny","ZOO","Intro","Brother","Venom"]

#           0       1      2       3         4


import random

for x in range(0, 1):

    random_number = random.randint(0,4)

first_let = []

for x in range(0,len(song)):

    artist = artist[random_number]
    print (artist)
    letter = song[random_number][0]
    print(letter)

    guess = input() 
    if guess == song:
        print("Well Done!")


attempts_left = 2

if attempts_left == 1:

    print("You have one attempt left!")

    exit

else:

    attempts_left == 0

    print("Unlucky, maybe next time.")

    exit

I want it to say whether the guess is wrong or right but i just receive an error.

Upvotes: 2

Views: 37

Answers (2)

Lord Elrond
Lord Elrond

Reputation: 16032

I'm not exactly sure why your code prints the extra letter, but I see a few problems, the most notable being that the "attempts" will never actually count down, since your code gets stuck in the for loop.

The following code should fix all your problems, and make your code both easier to read and modify (if you want change anything later):

import random
groups = [                  # sorting artists like this simplifies the rest of the code
    ("NF","Destiny"),
    ("Jetta", "ZOO"),
    ("Sickick", "Intro"),
    ("Kodaline","Brother"),
    ("Eminem","Venom")
]

random.shuffle(groups)

attempts = 3
for group in groups:
    artist = group[0]
    letter = group[1][0]
    song = group[1]
    print(letter)
    guess = input(f"Guess the name of {artist}'s song >>")
    if guess == song:
        print("Correct!")
        continue
    elif attempts == 0:
        print("game over!")
        break
    else:
        attempts -= 1
        print(f"You have {attempts} attempts left!")

Upvotes: 0

amrs-tech
amrs-tech

Reputation: 483

You can try the following code, it would work and it is simple to understand.

artist = ["NF","Jetta","Sickick","Kodaline","Eminem"]

#          0     1        2         3         4

song = ["Destiny","ZOO","Intro","Brother","Venom"]

#           0       1      2       3         4


import random
attempt = 2
r_num = random.randint(0,4)
for i in range(2):
    artist_name = artist[r_num]
    song_name = song[r_num]

    print('Artist: ',artist_name)
    print('Song first letter: ',song_name[0])
    guess = input('guess the song: ')
    if guess.lower() == song_name.lower():
        print('Well done!')
        break
    else:
        attempt -= 1
        print('You have 1 attempt left')
    if attempt == 0:
        print('0 attempts, sorry')
        break

Hope it helps in your case.

Upvotes: 1

Related Questions