Python Forum

Full Version: How to convert value to defined number?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am making a program that prints a piano-keyboard-diagram on the screen based on the input. I want to use text input using the 'music tracker' standard.

The example below is a basic C chord.
# User input
rightNotes = "C4 E4 G4 C5"
leftNotes = "C2 C3"
# Convert user input to numbers
rightHandNumbers = []  # Notes for the right hand
leftHandNumbers = []  # Notes for the left hand
The rightNotes and leftNotes need to be converted to numbers to rightHandNumbers and leftHandNumbers. The numbers are the piano-key-numbers(1-88). C4=(key)40, E4=44, G4=47, C5=52, C2=16, C3=28

I don't know how to do this(I think this is how to solve the problem):
  1. Making a function that cuts the rightNotes string by spaces into different values.
  2. Making a function that converts the separated values into the given numbers.
Does someone have idea's?

The conversion table looks like this for only the central octave:
C4=40
c4=41
D4=42
d4=43
E4=44
F4=45
f4=46
G4=47
g4=48
A4=49
a4=50
B4=51
you break a string with split: https://docs.python.org/3.8/library/stdt...#str.split
conversion can be done using a dictionary: https://docs.python.org/3/tutorial/datas...ctionaries
examples:
split:
>>> mystr = "Time is never lost - it's just replaced"
>>> mylist = mystr.split()
>>> mylist
>>> mystr = "Time is never lost - it's just misplaced"
>>> mylist = mystr.split()
>>> mylist
['Time', 'is', 'never', 'lost', '-', "it's", 'just', 'misplaced']
>>> mylist[3]
'lost'
>>>
dictionary:
>>> fruit_color = {
...     'apple': 'red',
...     'orange': 'orange',
...     'banana': 'yellow'
... }
>>> 
>>> print(f"An apple is {fruit_color['apple']}")
An apple is red
>>>
Thank you Larz60+ for the reply!

After a few hours of trying I need some more help. Wall

musicTextPitch = "c4 e4 g4 c5"
Which need to be seperated, converted and added to this list:
musicNumberedPitch = [40, 44, 47, 52]

The conversion list/dictionary is:
{'c4':40, 'e4':44, 'g4':47, 'c5':52}
How do I glue this together?
Always post your code.
You're going to have to convert each member of your split list to lowercase before looking up in the dictionary:
>>> mystr = "Time is never lost - it's just replaced"
>>> mylist = mystr.split()
>>> mylist[3].lower()
'lost'
>>>
# User input
rightNotes = "c4 e4 g4 c5"
# Convert user input to numbers
text2Numbers = {'c4': 40, 'e4': 44, 'g4': 47, 'c5': 52}
rightNotesList = rightNotes.split()
print(rightNotesList)
# Question: how to transport and convert the text from rightNotesList from text2Numbers in the
# rightNumbers list?
rightNumbers = []
print('converted numbers:')
print(rightNumbers)
# Desired result in this example:
# [40, 44, 47, 52]
# If rightNotes(user input) is for example 'c4' the output should be '[40]'.
After I understand this I can make the full dictionary for all musical pitches!
Update:
# User input
rightNotes = "c4 e4 g4 c5"
# Convert user input to numbers
text2Numbers = {'c4': 40, 'e4': 44, 'g4': 47, 'c5': 52}
rightNumbers = rightNotes.split() # -> list conversion using text2Numbers, How do I do that?
# Desired result in this example:
# [40, 44, 47, 52] Because in text2Numbers 'c4':40 etc...
I simplified the question, I hope the question is clear.
you need to use a loop (untested):
print("Converted numbers:", end="")
for note in rightNoteList:
    print(f" right note: {note}, value: {text2Numbers[note]}", end="")
print()
Yeah the problem is solved! Thankyou Larz60+!

the solution was this:
# User input
rightNotes = "c4 e4 g4 c5"
# Convert user input to numbers
text2Numbers = {'c4': 40, 'e4': 44, 'g4': 47, 'c5': 52}
rightNumbers = [] # input for my program!
for pitch in rightNotes.split():
    rightNumbers.append(text2Numbers[pitch])
print(rightNumbers)