2

I am practicing my Python skills. I am currently on week 4 and stuck on doing a specific task. If a user enter the same string twice in my self.songs input I want to print a message saying "You can't enter the same song twice. Try again".

How can I do this? Also, is there a way for the user the use commas instead of white space to separate each string while inputing their favourite songs?

  class User:
    def __init__(self):
        self.name = ''
        self.songs = ()
        self.movies = ()
        self.sports = ()

    def tops(self):
        self.name = input("What's you're full name?:")
        while True:
            self.songs = input('Hi ' + self.name + ', what is your top 5 favorite songs?:').split()
            if len(self.songs) > 5:
                print('You entered more than 5 songs. Try again')
            elif len(self.songs) < 5:
                print('You entered less than 5 songs. Try again')
            elif len(self.songs) == 5:
                for song in self.songs:
                    print(song)
                confirm_input = input('Do you confirm?')
                if confirm_input == 'Yes':
                    print(type(self.songs))
                    print(self.songs)
                    break
                elif confirm_input == 'No':
                    continue


quizUser = User()
quizUser.tops()
2
  • confirm_input = input('Type Yes to confirm'), So, the user knows to type Yes instead of the usual y :) Commented Nov 28, 2020 at 11:38
  • Not to criticize the question or answers. However sometimes taking a different approach can be useful. If you refactored to ask titles in a loop until 5, you’d easily achieve non-duplication by checking in the list. Plus, you could enter You Can't Always Get What You Want, which is currently impossible here. Commented Nov 28, 2020 at 16:28

2 Answers 2

2

You may use a set to get unique element, then compare its length with the answer length

if len(set(self.songs)) != len(self.songs):
    print("You can't enter the same song twice. Try again")    
elif len(self.songs) > 5:
    print('You entered more than 5 songs. Try again')
elif len(self.songs) < 5:
    print('You entered less than 5 songs. Try again')
...
Sign up to request clarification or add additional context in comments.

Comments

1

You can use set to get no same elements, and compare it's length with the normal list by using !=.

add this to your code:

elif len(set(self.songs)) != len(self.songs):
    print('You can\'t enter the same song twice. Try again')

by the way, \' means escaping '.

The whole code:

class User:
        def __init__(self):
            self.name = ''
            self.songs = ()
            self.movies = ()
            self.sports = ()
    
        def tops(self):
            self.name = input("What's you're full name?:")
            while True:
                self.songs = input('Hi ' + self.name + ', what is your top 5 favorite songs?:').split()
                if len(self.songs) > 5:
                    print('You entered more than 5 songs. Try again')
                elif len(self.songs) < 5:
                    print('You entered less than 5 songs. Try again')
                elif len(self.songs) != len(set(self.songs)):
                    print('You can\'t enter the same song twice. Try again')
                elif len(self.songs) == 5:
                    for song in self.songs:
                        print(song)
                    confirm_input = input('Do you confirm?')
                    if confirm_input == 'Yes':
                        print(type(self.songs))
                        print(self.songs)
                        break
                    elif confirm_input == 'No':
                        continue
    
    
quizUser = User()
quizUser.tops()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.