How to convert value to defined number? - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: How to convert value to defined number? (/thread-23019.html) |
How to convert value to defined number? - philipbergwerf - Dec-07-2019 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 handThe 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):
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 RE: How to convert value to defined number? - Larz60+ - Dec-07-2019 you break a string with split: https://docs.python.org/3.8/library/stdtypes.html#str.split conversion can be done using a dictionary: https://docs.python.org/3/tutorial/datastructures.html#dictionaries 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 >>> RE: How to convert value to defined number? - philipbergwerf - Dec-07-2019 Thank you Larz60+ for the reply! After a few hours of trying I need some more help. 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? RE: How to convert value to defined number? - Larz60+ - Dec-08-2019 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' >>> RE: How to convert value to defined number? - philipbergwerf - Dec-08-2019 # 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! RE: How to convert value to defined number? - philipbergwerf - Dec-08-2019 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. RE: How to convert value to defined number? - Larz60+ - Dec-08-2019 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() RE: How to convert value to defined number? - philipbergwerf - Dec-08-2019 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) |