how do i fix these errors? Bass guitar tabs generatorHow to generate all permutations of a list in PythonHow to fix Python indentationHow to fix “ImportError: No module named …” error in Python?How do I log a Python error with debug information?pip broke. how to fix DistributionNotFound error?guitar tab to uke tab program helpHow to format a floating number to fixed width in PythonHow to fix “Attempted relative import in non-package” even with __init__.pyHow to fix: “UnicodeDecodeError: 'ascii' codec can't decode byte”How do I fix 'ImportError: cannot import name IncompleteRead'?
What would be the way to say "just saying" in German? (Not the literal translation)
Should I put programming books I wrote a few years ago on my resume?
How precise must a satellite's orbit be?
What is the logic behind charging tax _in the form of money_ for owning property when the property does not produce money?
Is it okay to have a sequel start immediately after the end of the first book?
Zig-zag function - coded solution
Strange outlet behavior
Why do radiation hardened IC packages often have long leads?
Equilibrium constant for a solid-solid equilibrium
Why did Intel abandon unified CPU cache?
Is there a set of positive integers of density 1 which contains no infinite arithmetic progression?
Is using 'echo' to display attacker-controlled data on the terminal dangerous?
Do empty drive bays need to be filled?
Command of files and size
Is it possible to have 2 different but equal size real number sets that have the same mean and standard deviation?
How to create a cubic equation that include sums of the roots of another cubic equation
What should I be wary of when insurer is taking a lot of time to decide whether car is repairable or a total loss?
Find the Missing Members of this Powered Sequence
Does the Nuka-Cola bottler actually generate nuka cola?
What should I discuss with my DM prior to my first game?
Housemarks (superimposed & combined letters, heraldry)
If there's something that implicates the president why is there then a national security issue? (John Dowd)
How far would a landing Airbus A380 go until it stops with no brakes?
Why ambiguous grammars are bad?
how do i fix these errors? Bass guitar tabs generator
How to generate all permutations of a list in PythonHow to fix Python indentationHow to fix “ImportError: No module named …” error in Python?How do I log a Python error with debug information?pip broke. how to fix DistributionNotFound error?guitar tab to uke tab program helpHow to format a floating number to fixed width in PythonHow to fix “Attempted relative import in non-package” even with __init__.pyHow to fix: “UnicodeDecodeError: 'ascii' codec can't decode byte”How do I fix 'ImportError: cannot import name IncompleteRead'?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I'm working on some kind of software that will generate bass guitar tabs (only using 4 strings and frets 0-12
i tried defining these globals everywhere but it does't work.
Also what is the error about caling the function?
Errors:
Traceback (most recent call last):
File"/home/olaf/Documents/Projects/BassComposer/main.py", line 69, in
generator(fret, string)
File "/home/olaf/Documents/Projects/BassComposer/main.py", line 62, in generator
tab3 += str(fret)
NameError: name'tab3'is not defined
from random import randint
try:
times =str(int(input("How many notes do you want to be created?nn")))
except:
print("nInvalid input!n")
print("nnGenerating " + times + " note(s) long basslinen...nn")
startString = randint(0, 3)
startFret = randint(0, 9)
string = startString
fret = startFret
notes = int(times)
def generator(fret, string):
global tab0
global tab1
global tab2
global tab3
x = randint(0,11)
y = randint(0,16)
if x in range(4, 9):
if x in range(4, 7) and fret in range(0,11):
fret += 1
if x in range(4, 5) and fret in range(0, 11):
fret += 1
if x in range(8,11) and fret in range(1,12):
fret -= 1
if x in range(10,11) and fret in range(1, 12):
fret -= 1
if y in range(11,16):
if y in range(11, 13) and string in range(0,2):
string += 1
if (y == 13) and string in range(0,2):
string += 1
if y in range(14, 16) and string in range(1, 3):
fret -= 1
if (y==16) and string in range(1, 3):
fret -= 1
if string == 0:
tab0 += str(fret)
tab1 += " "
tab2 += " "
tab3 += " "
if string == 1:
tab1 += str(fret)
tab0 += " "
tab2 += " "
tab3 += " "
if string == 2:
tab2 += str(fret)
tab0 += " "
tab1 += " "
tab3 += " "
if string == 3:
tab3 += str(fret)
tab0 += " "
tab1 += " "
tab2 += " "
for notes in range(0, notes):
generator(fret, string)
notes -= 1
I would be thankfull if someone told me what are these problems caused by and how to fix them
python
add a comment |
I'm working on some kind of software that will generate bass guitar tabs (only using 4 strings and frets 0-12
i tried defining these globals everywhere but it does't work.
Also what is the error about caling the function?
Errors:
Traceback (most recent call last):
File"/home/olaf/Documents/Projects/BassComposer/main.py", line 69, in
generator(fret, string)
File "/home/olaf/Documents/Projects/BassComposer/main.py", line 62, in generator
tab3 += str(fret)
NameError: name'tab3'is not defined
from random import randint
try:
times =str(int(input("How many notes do you want to be created?nn")))
except:
print("nInvalid input!n")
print("nnGenerating " + times + " note(s) long basslinen...nn")
startString = randint(0, 3)
startFret = randint(0, 9)
string = startString
fret = startFret
notes = int(times)
def generator(fret, string):
global tab0
global tab1
global tab2
global tab3
x = randint(0,11)
y = randint(0,16)
if x in range(4, 9):
if x in range(4, 7) and fret in range(0,11):
fret += 1
if x in range(4, 5) and fret in range(0, 11):
fret += 1
if x in range(8,11) and fret in range(1,12):
fret -= 1
if x in range(10,11) and fret in range(1, 12):
fret -= 1
if y in range(11,16):
if y in range(11, 13) and string in range(0,2):
string += 1
if (y == 13) and string in range(0,2):
string += 1
if y in range(14, 16) and string in range(1, 3):
fret -= 1
if (y==16) and string in range(1, 3):
fret -= 1
if string == 0:
tab0 += str(fret)
tab1 += " "
tab2 += " "
tab3 += " "
if string == 1:
tab1 += str(fret)
tab0 += " "
tab2 += " "
tab3 += " "
if string == 2:
tab2 += str(fret)
tab0 += " "
tab1 += " "
tab3 += " "
if string == 3:
tab3 += str(fret)
tab0 += " "
tab1 += " "
tab2 += " "
for notes in range(0, notes):
generator(fret, string)
notes -= 1
I would be thankfull if someone told me what are these problems caused by and how to fix them
python
3
globaldoesn't create variables. All it does is alter the scope of a variable, because assigning to a name in a function makes a variable a local without aglobalstatement to say otherwise. You haven't giventab1, etc. any value anywhere, so you can't add more to them either.
– Martijn Pieters♦
Mar 24 at 21:46
1
You must first set a variable before you can read it. Appending some string also needs to read first.
– Michael Butscher
Mar 24 at 21:46
add a comment |
I'm working on some kind of software that will generate bass guitar tabs (only using 4 strings and frets 0-12
i tried defining these globals everywhere but it does't work.
Also what is the error about caling the function?
Errors:
Traceback (most recent call last):
File"/home/olaf/Documents/Projects/BassComposer/main.py", line 69, in
generator(fret, string)
File "/home/olaf/Documents/Projects/BassComposer/main.py", line 62, in generator
tab3 += str(fret)
NameError: name'tab3'is not defined
from random import randint
try:
times =str(int(input("How many notes do you want to be created?nn")))
except:
print("nInvalid input!n")
print("nnGenerating " + times + " note(s) long basslinen...nn")
startString = randint(0, 3)
startFret = randint(0, 9)
string = startString
fret = startFret
notes = int(times)
def generator(fret, string):
global tab0
global tab1
global tab2
global tab3
x = randint(0,11)
y = randint(0,16)
if x in range(4, 9):
if x in range(4, 7) and fret in range(0,11):
fret += 1
if x in range(4, 5) and fret in range(0, 11):
fret += 1
if x in range(8,11) and fret in range(1,12):
fret -= 1
if x in range(10,11) and fret in range(1, 12):
fret -= 1
if y in range(11,16):
if y in range(11, 13) and string in range(0,2):
string += 1
if (y == 13) and string in range(0,2):
string += 1
if y in range(14, 16) and string in range(1, 3):
fret -= 1
if (y==16) and string in range(1, 3):
fret -= 1
if string == 0:
tab0 += str(fret)
tab1 += " "
tab2 += " "
tab3 += " "
if string == 1:
tab1 += str(fret)
tab0 += " "
tab2 += " "
tab3 += " "
if string == 2:
tab2 += str(fret)
tab0 += " "
tab1 += " "
tab3 += " "
if string == 3:
tab3 += str(fret)
tab0 += " "
tab1 += " "
tab2 += " "
for notes in range(0, notes):
generator(fret, string)
notes -= 1
I would be thankfull if someone told me what are these problems caused by and how to fix them
python
I'm working on some kind of software that will generate bass guitar tabs (only using 4 strings and frets 0-12
i tried defining these globals everywhere but it does't work.
Also what is the error about caling the function?
Errors:
Traceback (most recent call last):
File"/home/olaf/Documents/Projects/BassComposer/main.py", line 69, in
generator(fret, string)
File "/home/olaf/Documents/Projects/BassComposer/main.py", line 62, in generator
tab3 += str(fret)
NameError: name'tab3'is not defined
from random import randint
try:
times =str(int(input("How many notes do you want to be created?nn")))
except:
print("nInvalid input!n")
print("nnGenerating " + times + " note(s) long basslinen...nn")
startString = randint(0, 3)
startFret = randint(0, 9)
string = startString
fret = startFret
notes = int(times)
def generator(fret, string):
global tab0
global tab1
global tab2
global tab3
x = randint(0,11)
y = randint(0,16)
if x in range(4, 9):
if x in range(4, 7) and fret in range(0,11):
fret += 1
if x in range(4, 5) and fret in range(0, 11):
fret += 1
if x in range(8,11) and fret in range(1,12):
fret -= 1
if x in range(10,11) and fret in range(1, 12):
fret -= 1
if y in range(11,16):
if y in range(11, 13) and string in range(0,2):
string += 1
if (y == 13) and string in range(0,2):
string += 1
if y in range(14, 16) and string in range(1, 3):
fret -= 1
if (y==16) and string in range(1, 3):
fret -= 1
if string == 0:
tab0 += str(fret)
tab1 += " "
tab2 += " "
tab3 += " "
if string == 1:
tab1 += str(fret)
tab0 += " "
tab2 += " "
tab3 += " "
if string == 2:
tab2 += str(fret)
tab0 += " "
tab1 += " "
tab3 += " "
if string == 3:
tab3 += str(fret)
tab0 += " "
tab1 += " "
tab2 += " "
for notes in range(0, notes):
generator(fret, string)
notes -= 1
I would be thankfull if someone told me what are these problems caused by and how to fix them
python
python
edited Mar 25 at 3:15
Fukiyel
1,094319
1,094319
asked Mar 24 at 21:41
Olaf LesiukOlaf Lesiuk
81
81
3
globaldoesn't create variables. All it does is alter the scope of a variable, because assigning to a name in a function makes a variable a local without aglobalstatement to say otherwise. You haven't giventab1, etc. any value anywhere, so you can't add more to them either.
– Martijn Pieters♦
Mar 24 at 21:46
1
You must first set a variable before you can read it. Appending some string also needs to read first.
– Michael Butscher
Mar 24 at 21:46
add a comment |
3
globaldoesn't create variables. All it does is alter the scope of a variable, because assigning to a name in a function makes a variable a local without aglobalstatement to say otherwise. You haven't giventab1, etc. any value anywhere, so you can't add more to them either.
– Martijn Pieters♦
Mar 24 at 21:46
1
You must first set a variable before you can read it. Appending some string also needs to read first.
– Michael Butscher
Mar 24 at 21:46
3
3
global doesn't create variables. All it does is alter the scope of a variable, because assigning to a name in a function makes a variable a local without a global statement to say otherwise. You haven't given tab1, etc. any value anywhere, so you can't add more to them either.– Martijn Pieters♦
Mar 24 at 21:46
global doesn't create variables. All it does is alter the scope of a variable, because assigning to a name in a function makes a variable a local without a global statement to say otherwise. You haven't given tab1, etc. any value anywhere, so you can't add more to them either.– Martijn Pieters♦
Mar 24 at 21:46
1
1
You must first set a variable before you can read it. Appending some string also needs to read first.
– Michael Butscher
Mar 24 at 21:46
You must first set a variable before you can read it. Appending some string also needs to read first.
– Michael Butscher
Mar 24 at 21:46
add a comment |
1 Answer
1
active
oldest
votes
global is the statement that declares the scope of the variable, not creates it. If you want your code to just work, you should write this before the generator function:
tab0 = ""
tab1 = ""
tab2 = ""
tab3 = ""
...BUT!
This is very bad practice. In addition, your code has several another serious problems, that don't cause errors, but makes your code very unclear and hard to maintain. So let's fix them and make your code beautiful :)
- Global variables and global statement are very bad practice. You can never be sure that some global variable will be correct and have the value you expect to see. In 99.9% cases global variable usage is not an option and can be replaced by local variables. If you really need global statement, you can use some kind of configuration files. Your
generatorfunction changes the state of four global variables and doesn't store the result, and we can rewrite it with:
def generator(fret, string):
tab0 = ""
tab1 = ""
tab2 = ""
tab3 = ""
for notes in range(0, notes):
# All your another code
return tab0, tab1, tab2, tab3
...
tab0, tab1, tab2, tab3 = generator(...)
- You have four logically equal variables:
tab0, tab1, tab2, tab3. What if somebody will want to run this code for 6-tabs bass guitar? In reality our tabs are the part of the one entity - tab... err... table (I'm not a musician, sorry). So we can combine them into one entity - tab list!
tab_list = []
With it, our code:
tab0 += str(fret)
tab1 += " "
tab2 += " "
tab3 += " "
converts to:
tab_list.append((str(fret), None, None, None)) (don't worry about Nones, we can tranform them later)
Now we have this code:
if string == 0:
tab_list.append(str(fret), None, None, None)
if string == 1:
tab_list.append(None, str(fret), None, None)
if string == 2:
tab_list.append(None, None, str(fret), None)
if string == 3:
tab_list.append(None, None, None, str(fret))
See the pattern? :) The str(fret) index is equal to string value! So we compress it even more:
tabs_to_append = [None, None, None, None]
tabs_to_append[string] = str(fret)
tab_list.append(tabs_to_append)
And we compress 20 lines of code into 3!
- This part of code:
for notes in range(0, notes):
generator(fret, string)
notes -= 1
iterates for 0..notes, but! the index name is equal to range bound and changes inside. It is very, very, VERY bad practice! It this case it will work (and it is strange), but in the most cases it will have many unexpected results that will force you to debug them hour by hour. So we rewrite it as:
for _ in range(0, notes): # We don't really need this index
- Unnesessary logics:
I will explain this part on this piece of code:
1. if x in range(4, 9):
2. if x in range(4, 7) and fret in range(0,11):
3. fret += 1
4. if x in range(4, 5) and fret in range(0, 11):
5. fret += 1
You have the first if-statement that checks x is 4..9. But inside there is only one if-statement that checks x is 4..7. It means the first if-statement is useless so we can just remove it (or there is a logical error inside). In addition, both if-statement on lines 2 and 4 has fret in range(0, 11). We can move it to the first line and get this code:
1. if fret in range(0,11):
2. if x in range(4, 7):
3. fret += 1
4. if x in range(4, 5):
5. fret += 1
Now look at if-statement on line 4. range(4, 5) returns one item list [4]. So this line transforms to: if x == 4:. Now we can finally change our block of code:
1. if fret in range(0,11):
2. if x in range(5, 7):
3. fret += 1
4. elif x == 4:
5. fret += 2
You can transform your other blocks of logic code. I am sure it will be simplified a lot (maybe even be merged with the block we rewrited now).
And finally we have our new program:
from random import randint
try:
times = int(input("How many notes do you want to be created?nn"))
except:
print("nInvalid input!n")
print("nnGenerating " + str(times) + " note(s) long basslinen...nn")
startString = randint(0, 3)
startFret = randint(0, 9)
def generator(startFret, startString, times):
string = startString
fret = startFret
tab_list = []
for _ in range(times):
x = randint(0, 11)
y = randint(0, 16)
# Transformed
if fret in range(0,11):
if x in range(5, 7):
fret += 1
elif x == 4:
fret += 2
# Old, removed unnesessary if-statements
if x in range(8,11) and fret in range(1,12):
fret -= 1
if x in range(10,11):
fret -= 1
if y in range(11, 13) and string in range(0,2):
string += 1
if (y == 13):
string += 1
if y in range(14, 16) and string in range(1, 3):
fret -= 1
if (y==16):
fret -= 1
tabs_to_append = ["_", "_", "_", "_"]
tabs_to_append[string] = str(fret)
tab_list.append(tabs_to_append)
return tab_list
tabs = generator(startFret, startString, times)
# Converts list of time-tabs to strings of tabs-timeline
tabs_str = [
" ".join([
tabs[i][j]
for i in range(len(tabs))
])
for j in range(4)
]
for s in tabs_str:
print(s)
It can be enhanced more (of course, nearly every program can :) ), but I am extremely tired, sorry. I think I helped you a bit with my long-long digital papyrus.
P.S. Your generator returns tabs for only one tab. I think you have a logical error inside your generator code. I don't know what are you want to generate so I will not give you any advices.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55328851%2fhow-do-i-fix-these-errors-bass-guitar-tabs-generator%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
global is the statement that declares the scope of the variable, not creates it. If you want your code to just work, you should write this before the generator function:
tab0 = ""
tab1 = ""
tab2 = ""
tab3 = ""
...BUT!
This is very bad practice. In addition, your code has several another serious problems, that don't cause errors, but makes your code very unclear and hard to maintain. So let's fix them and make your code beautiful :)
- Global variables and global statement are very bad practice. You can never be sure that some global variable will be correct and have the value you expect to see. In 99.9% cases global variable usage is not an option and can be replaced by local variables. If you really need global statement, you can use some kind of configuration files. Your
generatorfunction changes the state of four global variables and doesn't store the result, and we can rewrite it with:
def generator(fret, string):
tab0 = ""
tab1 = ""
tab2 = ""
tab3 = ""
for notes in range(0, notes):
# All your another code
return tab0, tab1, tab2, tab3
...
tab0, tab1, tab2, tab3 = generator(...)
- You have four logically equal variables:
tab0, tab1, tab2, tab3. What if somebody will want to run this code for 6-tabs bass guitar? In reality our tabs are the part of the one entity - tab... err... table (I'm not a musician, sorry). So we can combine them into one entity - tab list!
tab_list = []
With it, our code:
tab0 += str(fret)
tab1 += " "
tab2 += " "
tab3 += " "
converts to:
tab_list.append((str(fret), None, None, None)) (don't worry about Nones, we can tranform them later)
Now we have this code:
if string == 0:
tab_list.append(str(fret), None, None, None)
if string == 1:
tab_list.append(None, str(fret), None, None)
if string == 2:
tab_list.append(None, None, str(fret), None)
if string == 3:
tab_list.append(None, None, None, str(fret))
See the pattern? :) The str(fret) index is equal to string value! So we compress it even more:
tabs_to_append = [None, None, None, None]
tabs_to_append[string] = str(fret)
tab_list.append(tabs_to_append)
And we compress 20 lines of code into 3!
- This part of code:
for notes in range(0, notes):
generator(fret, string)
notes -= 1
iterates for 0..notes, but! the index name is equal to range bound and changes inside. It is very, very, VERY bad practice! It this case it will work (and it is strange), but in the most cases it will have many unexpected results that will force you to debug them hour by hour. So we rewrite it as:
for _ in range(0, notes): # We don't really need this index
- Unnesessary logics:
I will explain this part on this piece of code:
1. if x in range(4, 9):
2. if x in range(4, 7) and fret in range(0,11):
3. fret += 1
4. if x in range(4, 5) and fret in range(0, 11):
5. fret += 1
You have the first if-statement that checks x is 4..9. But inside there is only one if-statement that checks x is 4..7. It means the first if-statement is useless so we can just remove it (or there is a logical error inside). In addition, both if-statement on lines 2 and 4 has fret in range(0, 11). We can move it to the first line and get this code:
1. if fret in range(0,11):
2. if x in range(4, 7):
3. fret += 1
4. if x in range(4, 5):
5. fret += 1
Now look at if-statement on line 4. range(4, 5) returns one item list [4]. So this line transforms to: if x == 4:. Now we can finally change our block of code:
1. if fret in range(0,11):
2. if x in range(5, 7):
3. fret += 1
4. elif x == 4:
5. fret += 2
You can transform your other blocks of logic code. I am sure it will be simplified a lot (maybe even be merged with the block we rewrited now).
And finally we have our new program:
from random import randint
try:
times = int(input("How many notes do you want to be created?nn"))
except:
print("nInvalid input!n")
print("nnGenerating " + str(times) + " note(s) long basslinen...nn")
startString = randint(0, 3)
startFret = randint(0, 9)
def generator(startFret, startString, times):
string = startString
fret = startFret
tab_list = []
for _ in range(times):
x = randint(0, 11)
y = randint(0, 16)
# Transformed
if fret in range(0,11):
if x in range(5, 7):
fret += 1
elif x == 4:
fret += 2
# Old, removed unnesessary if-statements
if x in range(8,11) and fret in range(1,12):
fret -= 1
if x in range(10,11):
fret -= 1
if y in range(11, 13) and string in range(0,2):
string += 1
if (y == 13):
string += 1
if y in range(14, 16) and string in range(1, 3):
fret -= 1
if (y==16):
fret -= 1
tabs_to_append = ["_", "_", "_", "_"]
tabs_to_append[string] = str(fret)
tab_list.append(tabs_to_append)
return tab_list
tabs = generator(startFret, startString, times)
# Converts list of time-tabs to strings of tabs-timeline
tabs_str = [
" ".join([
tabs[i][j]
for i in range(len(tabs))
])
for j in range(4)
]
for s in tabs_str:
print(s)
It can be enhanced more (of course, nearly every program can :) ), but I am extremely tired, sorry. I think I helped you a bit with my long-long digital papyrus.
P.S. Your generator returns tabs for only one tab. I think you have a logical error inside your generator code. I don't know what are you want to generate so I will not give you any advices.
add a comment |
global is the statement that declares the scope of the variable, not creates it. If you want your code to just work, you should write this before the generator function:
tab0 = ""
tab1 = ""
tab2 = ""
tab3 = ""
...BUT!
This is very bad practice. In addition, your code has several another serious problems, that don't cause errors, but makes your code very unclear and hard to maintain. So let's fix them and make your code beautiful :)
- Global variables and global statement are very bad practice. You can never be sure that some global variable will be correct and have the value you expect to see. In 99.9% cases global variable usage is not an option and can be replaced by local variables. If you really need global statement, you can use some kind of configuration files. Your
generatorfunction changes the state of four global variables and doesn't store the result, and we can rewrite it with:
def generator(fret, string):
tab0 = ""
tab1 = ""
tab2 = ""
tab3 = ""
for notes in range(0, notes):
# All your another code
return tab0, tab1, tab2, tab3
...
tab0, tab1, tab2, tab3 = generator(...)
- You have four logically equal variables:
tab0, tab1, tab2, tab3. What if somebody will want to run this code for 6-tabs bass guitar? In reality our tabs are the part of the one entity - tab... err... table (I'm not a musician, sorry). So we can combine them into one entity - tab list!
tab_list = []
With it, our code:
tab0 += str(fret)
tab1 += " "
tab2 += " "
tab3 += " "
converts to:
tab_list.append((str(fret), None, None, None)) (don't worry about Nones, we can tranform them later)
Now we have this code:
if string == 0:
tab_list.append(str(fret), None, None, None)
if string == 1:
tab_list.append(None, str(fret), None, None)
if string == 2:
tab_list.append(None, None, str(fret), None)
if string == 3:
tab_list.append(None, None, None, str(fret))
See the pattern? :) The str(fret) index is equal to string value! So we compress it even more:
tabs_to_append = [None, None, None, None]
tabs_to_append[string] = str(fret)
tab_list.append(tabs_to_append)
And we compress 20 lines of code into 3!
- This part of code:
for notes in range(0, notes):
generator(fret, string)
notes -= 1
iterates for 0..notes, but! the index name is equal to range bound and changes inside. It is very, very, VERY bad practice! It this case it will work (and it is strange), but in the most cases it will have many unexpected results that will force you to debug them hour by hour. So we rewrite it as:
for _ in range(0, notes): # We don't really need this index
- Unnesessary logics:
I will explain this part on this piece of code:
1. if x in range(4, 9):
2. if x in range(4, 7) and fret in range(0,11):
3. fret += 1
4. if x in range(4, 5) and fret in range(0, 11):
5. fret += 1
You have the first if-statement that checks x is 4..9. But inside there is only one if-statement that checks x is 4..7. It means the first if-statement is useless so we can just remove it (or there is a logical error inside). In addition, both if-statement on lines 2 and 4 has fret in range(0, 11). We can move it to the first line and get this code:
1. if fret in range(0,11):
2. if x in range(4, 7):
3. fret += 1
4. if x in range(4, 5):
5. fret += 1
Now look at if-statement on line 4. range(4, 5) returns one item list [4]. So this line transforms to: if x == 4:. Now we can finally change our block of code:
1. if fret in range(0,11):
2. if x in range(5, 7):
3. fret += 1
4. elif x == 4:
5. fret += 2
You can transform your other blocks of logic code. I am sure it will be simplified a lot (maybe even be merged with the block we rewrited now).
And finally we have our new program:
from random import randint
try:
times = int(input("How many notes do you want to be created?nn"))
except:
print("nInvalid input!n")
print("nnGenerating " + str(times) + " note(s) long basslinen...nn")
startString = randint(0, 3)
startFret = randint(0, 9)
def generator(startFret, startString, times):
string = startString
fret = startFret
tab_list = []
for _ in range(times):
x = randint(0, 11)
y = randint(0, 16)
# Transformed
if fret in range(0,11):
if x in range(5, 7):
fret += 1
elif x == 4:
fret += 2
# Old, removed unnesessary if-statements
if x in range(8,11) and fret in range(1,12):
fret -= 1
if x in range(10,11):
fret -= 1
if y in range(11, 13) and string in range(0,2):
string += 1
if (y == 13):
string += 1
if y in range(14, 16) and string in range(1, 3):
fret -= 1
if (y==16):
fret -= 1
tabs_to_append = ["_", "_", "_", "_"]
tabs_to_append[string] = str(fret)
tab_list.append(tabs_to_append)
return tab_list
tabs = generator(startFret, startString, times)
# Converts list of time-tabs to strings of tabs-timeline
tabs_str = [
" ".join([
tabs[i][j]
for i in range(len(tabs))
])
for j in range(4)
]
for s in tabs_str:
print(s)
It can be enhanced more (of course, nearly every program can :) ), but I am extremely tired, sorry. I think I helped you a bit with my long-long digital papyrus.
P.S. Your generator returns tabs for only one tab. I think you have a logical error inside your generator code. I don't know what are you want to generate so I will not give you any advices.
add a comment |
global is the statement that declares the scope of the variable, not creates it. If you want your code to just work, you should write this before the generator function:
tab0 = ""
tab1 = ""
tab2 = ""
tab3 = ""
...BUT!
This is very bad practice. In addition, your code has several another serious problems, that don't cause errors, but makes your code very unclear and hard to maintain. So let's fix them and make your code beautiful :)
- Global variables and global statement are very bad practice. You can never be sure that some global variable will be correct and have the value you expect to see. In 99.9% cases global variable usage is not an option and can be replaced by local variables. If you really need global statement, you can use some kind of configuration files. Your
generatorfunction changes the state of four global variables and doesn't store the result, and we can rewrite it with:
def generator(fret, string):
tab0 = ""
tab1 = ""
tab2 = ""
tab3 = ""
for notes in range(0, notes):
# All your another code
return tab0, tab1, tab2, tab3
...
tab0, tab1, tab2, tab3 = generator(...)
- You have four logically equal variables:
tab0, tab1, tab2, tab3. What if somebody will want to run this code for 6-tabs bass guitar? In reality our tabs are the part of the one entity - tab... err... table (I'm not a musician, sorry). So we can combine them into one entity - tab list!
tab_list = []
With it, our code:
tab0 += str(fret)
tab1 += " "
tab2 += " "
tab3 += " "
converts to:
tab_list.append((str(fret), None, None, None)) (don't worry about Nones, we can tranform them later)
Now we have this code:
if string == 0:
tab_list.append(str(fret), None, None, None)
if string == 1:
tab_list.append(None, str(fret), None, None)
if string == 2:
tab_list.append(None, None, str(fret), None)
if string == 3:
tab_list.append(None, None, None, str(fret))
See the pattern? :) The str(fret) index is equal to string value! So we compress it even more:
tabs_to_append = [None, None, None, None]
tabs_to_append[string] = str(fret)
tab_list.append(tabs_to_append)
And we compress 20 lines of code into 3!
- This part of code:
for notes in range(0, notes):
generator(fret, string)
notes -= 1
iterates for 0..notes, but! the index name is equal to range bound and changes inside. It is very, very, VERY bad practice! It this case it will work (and it is strange), but in the most cases it will have many unexpected results that will force you to debug them hour by hour. So we rewrite it as:
for _ in range(0, notes): # We don't really need this index
- Unnesessary logics:
I will explain this part on this piece of code:
1. if x in range(4, 9):
2. if x in range(4, 7) and fret in range(0,11):
3. fret += 1
4. if x in range(4, 5) and fret in range(0, 11):
5. fret += 1
You have the first if-statement that checks x is 4..9. But inside there is only one if-statement that checks x is 4..7. It means the first if-statement is useless so we can just remove it (or there is a logical error inside). In addition, both if-statement on lines 2 and 4 has fret in range(0, 11). We can move it to the first line and get this code:
1. if fret in range(0,11):
2. if x in range(4, 7):
3. fret += 1
4. if x in range(4, 5):
5. fret += 1
Now look at if-statement on line 4. range(4, 5) returns one item list [4]. So this line transforms to: if x == 4:. Now we can finally change our block of code:
1. if fret in range(0,11):
2. if x in range(5, 7):
3. fret += 1
4. elif x == 4:
5. fret += 2
You can transform your other blocks of logic code. I am sure it will be simplified a lot (maybe even be merged with the block we rewrited now).
And finally we have our new program:
from random import randint
try:
times = int(input("How many notes do you want to be created?nn"))
except:
print("nInvalid input!n")
print("nnGenerating " + str(times) + " note(s) long basslinen...nn")
startString = randint(0, 3)
startFret = randint(0, 9)
def generator(startFret, startString, times):
string = startString
fret = startFret
tab_list = []
for _ in range(times):
x = randint(0, 11)
y = randint(0, 16)
# Transformed
if fret in range(0,11):
if x in range(5, 7):
fret += 1
elif x == 4:
fret += 2
# Old, removed unnesessary if-statements
if x in range(8,11) and fret in range(1,12):
fret -= 1
if x in range(10,11):
fret -= 1
if y in range(11, 13) and string in range(0,2):
string += 1
if (y == 13):
string += 1
if y in range(14, 16) and string in range(1, 3):
fret -= 1
if (y==16):
fret -= 1
tabs_to_append = ["_", "_", "_", "_"]
tabs_to_append[string] = str(fret)
tab_list.append(tabs_to_append)
return tab_list
tabs = generator(startFret, startString, times)
# Converts list of time-tabs to strings of tabs-timeline
tabs_str = [
" ".join([
tabs[i][j]
for i in range(len(tabs))
])
for j in range(4)
]
for s in tabs_str:
print(s)
It can be enhanced more (of course, nearly every program can :) ), but I am extremely tired, sorry. I think I helped you a bit with my long-long digital papyrus.
P.S. Your generator returns tabs for only one tab. I think you have a logical error inside your generator code. I don't know what are you want to generate so I will not give you any advices.
global is the statement that declares the scope of the variable, not creates it. If you want your code to just work, you should write this before the generator function:
tab0 = ""
tab1 = ""
tab2 = ""
tab3 = ""
...BUT!
This is very bad practice. In addition, your code has several another serious problems, that don't cause errors, but makes your code very unclear and hard to maintain. So let's fix them and make your code beautiful :)
- Global variables and global statement are very bad practice. You can never be sure that some global variable will be correct and have the value you expect to see. In 99.9% cases global variable usage is not an option and can be replaced by local variables. If you really need global statement, you can use some kind of configuration files. Your
generatorfunction changes the state of four global variables and doesn't store the result, and we can rewrite it with:
def generator(fret, string):
tab0 = ""
tab1 = ""
tab2 = ""
tab3 = ""
for notes in range(0, notes):
# All your another code
return tab0, tab1, tab2, tab3
...
tab0, tab1, tab2, tab3 = generator(...)
- You have four logically equal variables:
tab0, tab1, tab2, tab3. What if somebody will want to run this code for 6-tabs bass guitar? In reality our tabs are the part of the one entity - tab... err... table (I'm not a musician, sorry). So we can combine them into one entity - tab list!
tab_list = []
With it, our code:
tab0 += str(fret)
tab1 += " "
tab2 += " "
tab3 += " "
converts to:
tab_list.append((str(fret), None, None, None)) (don't worry about Nones, we can tranform them later)
Now we have this code:
if string == 0:
tab_list.append(str(fret), None, None, None)
if string == 1:
tab_list.append(None, str(fret), None, None)
if string == 2:
tab_list.append(None, None, str(fret), None)
if string == 3:
tab_list.append(None, None, None, str(fret))
See the pattern? :) The str(fret) index is equal to string value! So we compress it even more:
tabs_to_append = [None, None, None, None]
tabs_to_append[string] = str(fret)
tab_list.append(tabs_to_append)
And we compress 20 lines of code into 3!
- This part of code:
for notes in range(0, notes):
generator(fret, string)
notes -= 1
iterates for 0..notes, but! the index name is equal to range bound and changes inside. It is very, very, VERY bad practice! It this case it will work (and it is strange), but in the most cases it will have many unexpected results that will force you to debug them hour by hour. So we rewrite it as:
for _ in range(0, notes): # We don't really need this index
- Unnesessary logics:
I will explain this part on this piece of code:
1. if x in range(4, 9):
2. if x in range(4, 7) and fret in range(0,11):
3. fret += 1
4. if x in range(4, 5) and fret in range(0, 11):
5. fret += 1
You have the first if-statement that checks x is 4..9. But inside there is only one if-statement that checks x is 4..7. It means the first if-statement is useless so we can just remove it (or there is a logical error inside). In addition, both if-statement on lines 2 and 4 has fret in range(0, 11). We can move it to the first line and get this code:
1. if fret in range(0,11):
2. if x in range(4, 7):
3. fret += 1
4. if x in range(4, 5):
5. fret += 1
Now look at if-statement on line 4. range(4, 5) returns one item list [4]. So this line transforms to: if x == 4:. Now we can finally change our block of code:
1. if fret in range(0,11):
2. if x in range(5, 7):
3. fret += 1
4. elif x == 4:
5. fret += 2
You can transform your other blocks of logic code. I am sure it will be simplified a lot (maybe even be merged with the block we rewrited now).
And finally we have our new program:
from random import randint
try:
times = int(input("How many notes do you want to be created?nn"))
except:
print("nInvalid input!n")
print("nnGenerating " + str(times) + " note(s) long basslinen...nn")
startString = randint(0, 3)
startFret = randint(0, 9)
def generator(startFret, startString, times):
string = startString
fret = startFret
tab_list = []
for _ in range(times):
x = randint(0, 11)
y = randint(0, 16)
# Transformed
if fret in range(0,11):
if x in range(5, 7):
fret += 1
elif x == 4:
fret += 2
# Old, removed unnesessary if-statements
if x in range(8,11) and fret in range(1,12):
fret -= 1
if x in range(10,11):
fret -= 1
if y in range(11, 13) and string in range(0,2):
string += 1
if (y == 13):
string += 1
if y in range(14, 16) and string in range(1, 3):
fret -= 1
if (y==16):
fret -= 1
tabs_to_append = ["_", "_", "_", "_"]
tabs_to_append[string] = str(fret)
tab_list.append(tabs_to_append)
return tab_list
tabs = generator(startFret, startString, times)
# Converts list of time-tabs to strings of tabs-timeline
tabs_str = [
" ".join([
tabs[i][j]
for i in range(len(tabs))
])
for j in range(4)
]
for s in tabs_str:
print(s)
It can be enhanced more (of course, nearly every program can :) ), but I am extremely tired, sorry. I think I helped you a bit with my long-long digital papyrus.
P.S. Your generator returns tabs for only one tab. I think you have a logical error inside your generator code. I don't know what are you want to generate so I will not give you any advices.
answered Mar 25 at 15:43
vurmuxvurmux
4,8822830
4,8822830
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55328851%2fhow-do-i-fix-these-errors-bass-guitar-tabs-generator%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
3
globaldoesn't create variables. All it does is alter the scope of a variable, because assigning to a name in a function makes a variable a local without aglobalstatement to say otherwise. You haven't giventab1, etc. any value anywhere, so you can't add more to them either.– Martijn Pieters♦
Mar 24 at 21:46
1
You must first set a variable before you can read it. Appending some string also needs to read first.
– Michael Butscher
Mar 24 at 21:46