Python Forum
How to convert value to defined number?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to convert value to defined number?
#1
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
Reply
#2
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
>>>
Reply
#3
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?
Reply
#4
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'
>>>
Reply
#5
# 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!
Reply
#6
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.
Reply
#7
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()
Reply
#8
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)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  convert % to a number Nickd12 2 2,010 Sep-18-2020, 12:18 AM
Last Post: deanhystad
  python library not defined in user defined function johnEmScott 2 3,773 May-30-2020, 04:14 AM
Last Post: DT2000
  Find Average of User Input Defined number of Scores DustinKlent 1 4,204 Oct-25-2019, 12:40 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020