0

I've had this python exercise I've been trying to do for a while but it's not working.

In the exercise, I defined a function make_album that accepts two parameters: artistName and albumTitle.

The function should have a While loop that allows users to enter an album's artist and title. Once I have that information, I call the function and it returns a string with these information.

The While loop is meant to keep asking for the user's input until they type in "Quit" which is meant to break the loop and stop the code from running..

In the first of numerous trial and error, I did this:

def make_album(artistName, albumTitle):

    code_run = input("Enter 'CONTINUE' to continue code or 'QUIT' to quit code: ")

    respond = 'My favorite artist is ' + artistName + ' and their latest album is ' + albumTitle

   while code_run == 'continue':
       artistName = input("What is your favorite artist's name? ")
       albumTitle = input("What is their latest album? ")

       if code_run == 'quit':
          break

    return respond 


res = make_album(artistName, albumTitle)
print(res)

The problem is that when I call the function, the arguments in the function call is somehow "not defined". I don't know what the problem is.

I've tried just putting the input prompt in the function call itself as arguments. It worked but it did not run itself again automatically like it's meant to.

6
  • I recommend you take your favourite rubber duck and try to explain what your code does, step by step. Or you can run your code in debug mode to see what happens at each execution step. Coding is not about "numerous trial and error", instead it's about having a specific and well understood procedure and translate it in a language that the computer can understand, in this case python. Commented Sep 4, 2023 at 7:31
  • Once you enter the while loop you can never [programmatically] break because code_run doesn't change. Also, did you notice the NameError exception when you tried to run this code? Commented Sep 4, 2023 at 7:34
  • @DarkKnight that's the main problem. The NameError which I don't understand how to resolve. Commented Sep 4, 2023 at 7:53
  • Your summary of the exercise specification does not make sense to me. Is it possible that you have misunderstood the exercise specification? Commented Sep 4, 2023 at 10:24
  • @gboffi Where do you think I must have misunderstood it?? Commented Sep 4, 2023 at 11:09

1 Answer 1

0

Because you assigned the make_album function to a variable with undefined variables and tried to print then. The correct code looks like the following:

code_run = "continue" # start as "continue to make the loop work"

def make_album(artistName, albumTitle):
    return 'My favorite artist is ' + artistName + ' and their latest album is ' + albumTitle

while code_run == 'continue':
    code_run = input("Enter 'CONTINUE' to continue code or 'QUIT' to quit code: ")
    if code_run == 'quit':
        break
       
    artistName = input("What is your favorite artist's name? ")
    albumTitle = input("What is their latest album? ")

    res = make_album(artistName, albumTitle)
    print(res)

return keyword will let the function return an defined value specified by the parameters on yout function, this is a much cleaner. You code tried to call variables that were out of scope, and inside a function that never interages outside that block.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for trying to solve it but unfortunately, the code didn't work..
How? I ran both codes on my enviorement and adjusted to the question, is there any specific problem left?
Hi, I made a mistake while trying to correct the code myself. Your solution works perfectly. Thank you very much.
Cool, mark it as solved

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.