-2

I'm having some issues with a quiz I'm trying to create in Python. The issue I'm having is with the response.

The quiz should ask the user, to guess the song based on a certain number of letters given to them. however, when we type a response it doesn't seem to check its correct/ wrong and proceed onto the next question.

any help would be greatly appreciated.

  print ("Welcome - Guess The Song", username)
#This part of my code is allowing authorised users to enter the game. This does not allow other users into my game. It gives three attempts before not letting someone access the game.

import random
Song = 'Song Name:'
sname=['Reseaux Niska', 'Sale Niska', 'Ah Bon? Niska', 'Story X Niska', 'Amour X Niska','Tuba Life Niska', 'La Wewer Niska', 'Favelas Niska', 'Bella Maitre Gims',  'Sapes Comme Jamain Maitre Gims', 'Corazon Maitre Gims', 'Cameleon Maitre Gims', 'Est-Ce Que Tu M aimes Maitre Gims', 'Oulala Maitre Gims', 'Laissez Passer Maitre Gims', 'Tout Donner Maitre Gims', 'La Meme Maitre Gims', 'Je Te Pardonne Maitre Gims', 'Loin Maitre Gims', 'Mi GNA Maitre Gims', 'Fuegolando Maitre Gims','T es Partie Maitre Gims','Bonita Maitre Gims', 'Ou Aller Maitre Gims','Entre Nous C est Mort Maitre Gims','Sur Ma Route Black M','Genesis MMZ','Bubble MMZ','Loin Des Etoiles MMZ','Tout Pour Le Gang MMZ','Bulma MMZ', 'Ma Bulle MMZ', 'Comme Dans Un Reve MMZ']  

game_song = random.choice(sname)
print ((game_song)[:1 :1])

def guess_song(song, score):
  for guesscount in range(4):
    if guessscount == 2:
      print("Too Many Guesses LEAVE!")
      break

    guess = input("Guess: {} " .format(gusscount+1))

    if not guess:
      print("Are You Broken?")
      continue

    if guess == sname:
      if guesscount == 0:
        print("Well Done You Were Correct!")

        score += 1

      break

    else:
      print("WRONG JUST LEAVE!")
      continue

  return score

def _name_():
 if _name_ == "_main_":
   final_score = guess_song(song, 0)
   print("final score {}:" .format(final_score)) "
jwodder
  • 54,758
  • 12
  • 108
  • 124
Knightc
  • 1
  • 1
  • 2
    Can you try to remove unnecessary pieces of code and narrow down your questiona bit? Add details like saying what efforts you've made to debug the issue and the results you got. Remove details saying that you're new, we don't care if you started yesterday or have 20 years of experience. A question is a question. – Aneesh Durg Oct 29 '18 at 18:02
  • please improve your title. – eyllanesc Oct 29 '18 at 18:23

4 Answers4

0

Try this code:

import random


Song = 'Song Name:'
sname=['Reseaux Niska', 'Sale Niska', 'Ah Bon? Niska', 'Story X Niska', 'Amour X Niska','Tuba Life Niska', 'La Wewer Niska', 'Favelas Niska', 'Bella Maitre Gims',  'Sapes Comme Jamain Maitre Gims', 'Corazon Maitre Gims', 'Cameleon Maitre Gims', 'Est-Ce Que Tu M aimes Maitre Gims', 'Oulala Maitre Gims', 'Laissez Passer Maitre Gims', 'Tout Donner Maitre Gims', 'La Meme Maitre Gims', 'Je Te Pardonne Maitre Gims', 'Loin Maitre Gims', 'Mi GNA Maitre Gims', 'Fuegolando Maitre Gims','T es Partie Maitre Gims','Bonita Maitre Gims', 'Ou Aller Maitre Gims','Entre Nous C est Mort Maitre Gims','Sur Ma Route Black M','Genesis MMZ','Bubble MMZ','Loin Des Etoiles MMZ','Tout Pour Le Gang MMZ','Bulma MMZ', 'Ma Bulle MMZ', 'Comme Dans Un Reve MMZ']  

def get_random_song():
    song = random.choice(sname)
    print("Guess song: {0:s}".format(song[0]))
    return song


def guess_song(song):
    count = 0
    score = 0
    while count < 3:
        guess = input("Guess: ")
        if not guess:
            print("Are you broken?")
            continue

        if guess == song:
            print("Well Done You Were Correct!")
            score += 1
            song = get_random_song()
        else:
            print("WRONG JUST LEAVE!")
            count += 1

    return score

if __name__ == "__main__":
    song = get_random_song()
    final_score = guess_song(song)
    print("Final score: {0:d}".format(final_score))

There were many issues with your code starting with indentations and ending on logic itself.

Long story short if you want to do something forever units condition is meet use while loops. At the beginning, you set score and count to initial values. Then if someone has guessed correctly you add score and pick a new song. If someone provided wrong answer increase wrong guess/lives count - count. This process goes one till you will guess wrong 3 times.

You could add also logic which prevents choosing the same song. But lets say it's your homework. Hint try to remove chosen element from sname

Raoslaw Szamszur
  • 1,723
  • 12
  • 21
0

Before I answer your question, a quick pointer. Don't write code like this. This is like spaghetti and very difficult to follow the flow in your code

I've made minimal modifications to your code to make it slightly more readable

import random
import sys

def set_up_user():
  username =input('Enter User Name')
  password = ""
  attempt = 0
  flag = 0
  while(attempt!=3):
    password = input("Enter Password")
    if(password=="Rossiya"):
      flag = 1
      break
    else:
      attempt=attempt+1
      if(attempt==3):
        print ("You Have Tried The Maximum Amount Of Time To Enter A Password")
        quit() 
  if(flag==1):
    print ("Welcome - Guess The Song", username)
  #This part of my code is allowing authorised users to enter the game. This does not allow other users into my game. It gives three attempts before not letting someone access the game.


def get_song():
  song = 'Song Name:'
  sname=['Reseaux Niska', 'Sale Niska', 'Ah Bon? Niska', 'Story X Niska', 'Amour X Niska','Tuba Life Niska', 'La Wewer Niska', 'Favelas Niska', 'Bella Maitre Gims',  'Sapes Comme Jamain Maitre Gims', 'Corazon Maitre Gims', 'Cameleon Maitre Gims', 'Est-Ce Que Tu M aimes Maitre Gims', 'Oulala Maitre Gims', 'Laissez Passer Maitre Gims', 'Tout Donner Maitre Gims', 'La Meme Maitre Gims', 'Je Te Pardonne Maitre Gims', 'Loin Maitre Gims', 'Mi GNA Maitre Gims', 'Fuegolando Maitre Gims','T es Partie Maitre Gims','Bonita Maitre Gims', 'Ou Aller Maitre Gims','Entre Nous C est Mort Maitre Gims','Sur Ma Route Black M','Genesis MMZ','Bubble MMZ','Loin Des Etoiles MMZ','Tout Pour Le Gang MMZ','Bulma MMZ', 'Ma Bulle MMZ', 'Comme Dans Un Reve MMZ']  
  game_song = random.choice(sname)
  print (game_song)
  return sname, game_song


def guess_song(song, score):
  for guesscount in range(4):
    if guesscount == 2:
      print("Too Many Guesses LEAVE!")
      break

    guess = input("Guess:")

    if not guess:
      print("Are You Broken?")
      continue

    if guess == song:
      if guesscount == 0:
        print("Well Done You Were Correct!")

        score += 1

      break

    else:
      print("WRONG JUST LEAVE!")
      continue

  return score

def main():   
  print("here")
  set_up_user()
  sname, game_song = get_song()
  final_score = guess_song(game_song, 0)
  print("final score {}:" .format(final_score)) 

if __name__ == "__main__":
  main()

You have several problems in your old code

  1. Lots and lots of typos. gusscount and guessscount
  2. That's not how the main sentinel works. You check for __name__ and __main__. not single _. They have to be double __.
  3. Use functions. Don't mix functions and global scope freely. Nobody including you can read your code
Srini
  • 1,619
  • 1
  • 19
  • 34
0

Instead of providing a working copy of code that does what you ask for I am going to point you to places that need correction. Lets start with the below:

  1. Correct the typos in variable names, usually when you run the python program the compiler will complain about variables that it cant find or are named inconsistently. Eg: guesscount.
  2. It should be name, think about what it means for main and also think about what you are aiming to achieve with name function, try to reason about why it is needed or not needed.
  3. To print only the first letter of a string string[:1] or string[0] is enough, think about what you were trying to achieve with (game_song)[:1 :1]. Also, it is best to precede this with a description of what it is, for example something like : print("First Letter of the song : %s" % (game_song[0])). Also think about slicing vs. indexing and what would be different -> Slicing a list in Python without generating a copy
  4. Think about this part: if guess == sname: if guesscount == 0: print("Well Done You Were Correct!") score += 1 What are you trying to achieve here. Do you want the user to guess the song name correctly on the first attempt itself or do you want to print extra kudos to the user if and only if he/she guesses it right in the first attempt. Also, instead of break think about whether we can use a return statement
    1. Think about unicode. Try to reason as to why this might be different between Python2 and Python3
0

Isn't this last years UK NEA. I remember being given this task for my assessment last year.

Anyway,

   guess = input("Guess: {} " .format(gusscount+1))

I believe you have written gusscount on this line when it should have been guesscount

Im not the most advanced with python so this may not really help.

S.S
  • 1
  • 3