Issue
I'm trying to create a very simple guitar tab creator in python, I had the idea but i'm not sure how to execute it the way I want. For example, I have a list containing each guitar string as a different item in the list, and I want the the user to be able to input which fret they want to use and which note. How can I replace a specific char in a list item based on a index number.
This is what I have so far:
tab = ['E|------------', 'B|------------', 'G|------------', 'D|------------', 'A|------------', 'E|------------']
for line in tab:
print(line)
fret = input("Fret: ")
note = input("Note: ")
#if note == 'E':
(Stuck here)
The output I'm looking for is something like:
e|-------5-7-----7-|-8-----8-2-----2-|-0---------0-----|-----------------|
B|-----5-----5-----|---5-------3-----|---1---1-----1---|-0-1-1-----------|
G|---5---------5---|-----5-------2---|-----2---------2-|-0-2-2---2-------|
D|-7-------6-------|-5-------4-------|-3---------------|-----------------|
A|-----------------|-----------------|-----------------|-2-0-0---0--/8-7-|
E|-----------------|-----------------|-----------------|-----------------|
I am aware that there is much more needed to get the output i'm looking for, but I want to try to figure that stuff out by myself first, I'm just looking for a nudge in the right direction to get a list item changed.
Solution
Note sure how you want to construct the whole thing, but here are some ideas:
The main line is
"-".join(str(d.get(idx, "-")) for idx in range(1, 9))
which, given a dict d
having indices as keys and corresponding fret numbers as values, construct the string representation.
It uses the dict.get
method which allows for a default value of "-"
when nothing corresponds in the given dict.
from collections import namedtuple
def construct_string(indices, frets):
d = dict(zip(indices, frets))
out = "-".join(str(d.get(idx, "-")) for idx in range(1, 9))
return f"-{out}-"
String = namedtuple("Note", ["indices", "frets"])
strings = {
"e": String([4, 5, 8], [5, 7, 7]),
"B": String([3, 6], [5, 5]),
"G": String([2, 7], [5, 5]),
"D": String([1, 5], [7, 6]),
"A": String([], []),
"E": String([], []),
}
for string_name, string in strings.items():
string_line = construct_string(string.indices, string.frets)
print(f"{string_name}|{string_line}")
which prints:
e|-------5-7-----7-
B|-----5-----5-----
G|---5---------5---
D|-7-------6-------
A|-----------------
E|-----------------
Answered By - paime
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.