0

I recently started learning to program in python, and just for fun I wrote this program, but I need some help.

It's possible that I've complicated things and it can be simplified somehow, but I'm just a beginner.

I have the following code:

import random
Genre = ["Power metal", "Melodic death metal", "Progressive metal", "Rock"]
Power_metal = ["Helloween", "Dragonforce", "Hammerfall"]
Melodic_death_metal = ["Kalmah", "Insomnium", "Ensiferum", "Wintersun"]
Progressive_metal = ["Dream theater", "Symphony X"]
Rock = ["Bon Jovi", "Nirvana"]

Helloween = ["Pumpkins united", "Halloween"]
Dragonforce = ["Heroes of our time", "Fury of the storm"]
Hammerfall = ["Hearts on fire", "Riders of the storm"]
Kalmah = ["Hades", "Seventh Swamphony"]
Insomnium = ["While we sleep", "Down with the sun"]
Ensiferum = ["Victory song", "Rum, woman, victory", "Lai la hei"]
Wintersun = ["Winter Madness", "Sons of winter and stars"]
Dream_theater = ["As I am", "The enemy inside"]
Symphony_X = ["Sea of lies", "Inferno"]
Bon_Jovi = ["It's my life", "Wanted dead or alive"]
Nirvana = ["Smells like teen spirit", "Come as you are"]


Music = input("\nSelect a music genre from available: %s:" % (Genre))
def bands(Music):
    while True:
        if Music == "Melodic death metal":
            return (Melodic_death_metal)
        elif Music == "Power metal":
            return (Power_metal)
        elif Music == "Progressive metal":
            return (Progressive_metal)
        elif Music == "Rock":
            return (Rock)
        else:
            Music = input("\nYou entered the wrong name. Please, enter again: %s:" % (Genre))

Band = input("\nReffering to chosen genre, I have bands such as: %s. Choose a specific band and I'll suggest a random song:\n" % bands(Music))
def fate(Band):
    while True:
        if Band == "Helloween":
            print("Suggested song: ", random.choice(Helloween))
            break
        elif Band == "Dragonforce":
            print("Suggested song: ", random.choice(Dragonforce))
            break
        elif Band == "Hammerfall":
            print("Suggested song: ", random.choice(Hammerfall))
            break
        elif Band == "Kalmah":
            print("Suggested song: ", random.choice(Kalmah))
            break
        elif Band == "Insomnium":
            print("Suggested song: ", random.choice(Insomnium))
            break
        elif Band == "Ensiferum":
            print("Suggested song: ", random.choice(Ensiferum))
            break
        elif Band == "Wintersun":
            print("Suggested song: ", random.choice(Wintersun))
            break
        elif Band == "Dream theater":
            print("Suggested song: ", random.choice(Dream_theater))
            break
        elif Band == "Symphony X":
            print("Suggested song: ", random.choice(Symphony_X))
            break
        elif Band == "Bon Jovi":
            print("Suggested song: ", random.choice(Bon_Jovi))
            break
        elif Band == "Nirvana":
            print("Suggested song: ", random.choice(Nirvana))
            break
        else:
            Band = input("\nYou entered the wrong name. Please, enter again from available here %s and I'll suggest random song:\n" % bands(Music))


fate(Band)

while True:
    next = input("\nWould you like me to suggest one more song from the band of your choice ?  Answer yes or no:\n")
    if next == "no":
        print("Enjoy your music")
        break
    elif next == "yes":
        fate(Band)  

What I would like to do is handle a certain situation that is in the last loop of the code. Before the while loop , we have defined function fate(Band)that generates a random list, depending on what the user has previously selected.

I would like to make the program suggest random songs from a generated list. Every time a user asks for another suggestion, I would like the program to propose a different song and not repeat songs already suggested. And in case you have already printed all the songs in the console, then the program says it has no more to offer.

I tried to use dic{},set() and also I was thinking about making an another defined function, but I have no idea at moment how to handle this situation.

Maybe you will try to help me.

Thanks in advance

Tomek
  • 3
  • 1
  • 1
    You can remove the printed items from the lists. To do that, you can use `shuffle()` and `pop()` methods of `list`. See this thread: https://stackoverflow.com/questions/10048069/what-is-the-most-pythonic-way-to-pop-a-random-element-from-a-list – Harun Yilmaz Nov 16 '21 at 09:31
  • 1
    This seems like a use-case where an embedded dict could make your life much easier. – Bram Vanroy Nov 16 '21 at 09:32
  • Alright thanks anyway. I'll take a look at thread and embedded dict. – Tomek Nov 16 '21 at 09:55

1 Answers1

0

Make your random list as a set.

Then for every song you extract from it, pop it from the set

mySet.remove(songName)  

Edit: Sorry - to be clear - you'll need to use

random.choice(tuple(yourSet)) 

to generate a random choice of a set.

Amiga500
  • 1,258
  • 1
  • 6
  • 11
  • Hmm, but random list is generated from defined function. This `set(fate(Band))` won't work. It says, expected type Iterable. Or maybe I don't understand where make this set. – Tomek Nov 16 '21 at 09:40
  • Helloween etc are what you'd make as sets – Amiga500 Nov 16 '21 at 10:01