Need help with Python Synth

I am trying to make a song generator where the user can add notes and lengths to a list through inputs. The input part is working but I am not able to get the things that are being added to the list to be formatted correctly. This is the code below.


generatedSong=[]

while True:
  noteAndLength=input("Please pick a note and note length in this format: ([note], [note length]): ")
  generatedSong.append(noteAndLength)
  keepGoing=input("Would you like to add another note? ")
  if keepGoing=="No" or keepGoing=="no":
    break
  else:
    continue

print(generatedSong)



If I were to add the notes a and b with full note lengths to the list, it should look like this:

[('a', 1), ('b', 1)]


But when I am appending the notes to the list it ends up looking like this:

["('a', 1)", "('b', 1)"]

I basically just need to figure out how to not turn it all into a string/remove the quotations that I have in bold.

I guess that means you want a tuple containing note and length? input() will just return the string the user has entered, so you’ll need to write a function that will parse the string and return a tuple.

A very rough approach might look like this:

import re
s = '(a, 1)'
parser = re.compile(r'\(([a-z]+),\s?([\d\.]+)\)')
m = parser.match(s)
if m is not None:
    t = (m.group(1), float(m.group(2)))
    # Add t to the song list
else:
    # Whatever the user typed didn't match the expression, raise
    # an exception, write an error message, or something.

The regular expression doesn’t enforce that the number contains only one decimal point or that the letter represents a valid note, you might want to refine that a bit and add error handling in case of invalid input. :slightly_smiling_face:

As a side note, when posting code please use a markdown code block (enclosed in triple backticks, ```). That makes it much easier to read and preserves indentation, which is critical for Python.

So I am trying to make a python synth program that takes the users inputs for notes in terms of letters and then makes a song file from it.


generatedSong=[]

while True:
  noteAndLength=input("Please pick a note or enter '1' if you would like to stop! ")
  if noteAndLength!='1':
    generatedSong.append(noteAndLength)
  else:
    break


pysynth.make_wav(generatedSong, fn="Awesome Song.wav")

This is my code that I am using. It keeps throwing me and error that I don’t understand:

Please pick a note or enter '1' if you would like to stop! c
Please pick a note or enter '1' if you would like to stop! 1
Writing to file Awesome Song.wav
[1/2]
Traceback (most recent call last):
  File "main.py", line 17, in <module>
    pysynth.make_wav(generatedSong, fn="Awesome Song.wav")
  File "/usr/local/lib/python3.6/site-packages/pysynth.py", line 148, in make_wav
    if x[1] < 0:
IndexError: string index out of range
python3 exited with code 1...

Does anyone know what this means?

The IndexError means that x doesn’t have an element no. 1, for strings the elements are characters. Indices start at 0, so it might be empty or contain just one character.

That said, if x was longer x[1] would return a character, and the comparison would fail like this:

>>> 'a' < 0
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: '<' not supported between instances of 'str' and 'int'

That tells me x is probably not supposed to be a string.

What you should do: Check the pysynth documentation for what kind of input pysynth.make_wav() accepts, and make sure your generatedSong matches that. If you have trouble understanding the description ask about the specific problems.