I get very strange error while im trying to finish my project
up vote
0
down vote
favorite
This code is meant to pick an artist from internal file (text file), read it and pick randomly from the internal file the artist (that are in the text file in array style, e.g., "carrot apple banana"), therefore I added .txt to the chosen artist so the program will open artist's file with songs and pick a random song.
import random
loop = False
counter = 0
points = 0
max_level = 10
while loop == False:
for lines in open("pick.txt").readlines():
art = lines.split()
artist = random.choice(art)
for i in open((artist) + ".txt"):
song = i.split()
song_name = random.choice(song)
print("the song begins with :" , song_name , "this song is by :" , artist)
answer = input("Enter full name of the song : ")
if answer == song_name:
points = points +3
print("correct")
counter = counter +1
print(counter)
elif answer != song_name:
print("WRONG !!! n try again")
dec = input("Please enter the full name of the song :")
if dec == song_name:
points = points +2
print("correct")
counter = counter +1
print(counter)
elif dec != song_name:
print("smh n WRONG")
counter = counter +1
print(counter)
elif counter >= max_level:
print("The End")
quit()
else:
print("error")
input()
Afterwards when I run the code in python shell, there is a random chance that I get this error, either straight away or later on:
raise IndexError('Cannot choose from an empty sequence') from None
IndexError: Cannot choose from an empty sequence
python random
add a comment |
up vote
0
down vote
favorite
This code is meant to pick an artist from internal file (text file), read it and pick randomly from the internal file the artist (that are in the text file in array style, e.g., "carrot apple banana"), therefore I added .txt to the chosen artist so the program will open artist's file with songs and pick a random song.
import random
loop = False
counter = 0
points = 0
max_level = 10
while loop == False:
for lines in open("pick.txt").readlines():
art = lines.split()
artist = random.choice(art)
for i in open((artist) + ".txt"):
song = i.split()
song_name = random.choice(song)
print("the song begins with :" , song_name , "this song is by :" , artist)
answer = input("Enter full name of the song : ")
if answer == song_name:
points = points +3
print("correct")
counter = counter +1
print(counter)
elif answer != song_name:
print("WRONG !!! n try again")
dec = input("Please enter the full name of the song :")
if dec == song_name:
points = points +2
print("correct")
counter = counter +1
print(counter)
elif dec != song_name:
print("smh n WRONG")
counter = counter +1
print(counter)
elif counter >= max_level:
print("The End")
quit()
else:
print("error")
input()
Afterwards when I run the code in python shell, there is a random chance that I get this error, either straight away or later on:
raise IndexError('Cannot choose from an empty sequence') from None
IndexError: Cannot choose from an empty sequence
python random
3
Please show the full traceback. There's nothing strange about the error.
– roganjosh
Nov 10 at 21:04
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
This code is meant to pick an artist from internal file (text file), read it and pick randomly from the internal file the artist (that are in the text file in array style, e.g., "carrot apple banana"), therefore I added .txt to the chosen artist so the program will open artist's file with songs and pick a random song.
import random
loop = False
counter = 0
points = 0
max_level = 10
while loop == False:
for lines in open("pick.txt").readlines():
art = lines.split()
artist = random.choice(art)
for i in open((artist) + ".txt"):
song = i.split()
song_name = random.choice(song)
print("the song begins with :" , song_name , "this song is by :" , artist)
answer = input("Enter full name of the song : ")
if answer == song_name:
points = points +3
print("correct")
counter = counter +1
print(counter)
elif answer != song_name:
print("WRONG !!! n try again")
dec = input("Please enter the full name of the song :")
if dec == song_name:
points = points +2
print("correct")
counter = counter +1
print(counter)
elif dec != song_name:
print("smh n WRONG")
counter = counter +1
print(counter)
elif counter >= max_level:
print("The End")
quit()
else:
print("error")
input()
Afterwards when I run the code in python shell, there is a random chance that I get this error, either straight away or later on:
raise IndexError('Cannot choose from an empty sequence') from None
IndexError: Cannot choose from an empty sequence
python random
This code is meant to pick an artist from internal file (text file), read it and pick randomly from the internal file the artist (that are in the text file in array style, e.g., "carrot apple banana"), therefore I added .txt to the chosen artist so the program will open artist's file with songs and pick a random song.
import random
loop = False
counter = 0
points = 0
max_level = 10
while loop == False:
for lines in open("pick.txt").readlines():
art = lines.split()
artist = random.choice(art)
for i in open((artist) + ".txt"):
song = i.split()
song_name = random.choice(song)
print("the song begins with :" , song_name , "this song is by :" , artist)
answer = input("Enter full name of the song : ")
if answer == song_name:
points = points +3
print("correct")
counter = counter +1
print(counter)
elif answer != song_name:
print("WRONG !!! n try again")
dec = input("Please enter the full name of the song :")
if dec == song_name:
points = points +2
print("correct")
counter = counter +1
print(counter)
elif dec != song_name:
print("smh n WRONG")
counter = counter +1
print(counter)
elif counter >= max_level:
print("The End")
quit()
else:
print("error")
input()
Afterwards when I run the code in python shell, there is a random chance that I get this error, either straight away or later on:
raise IndexError('Cannot choose from an empty sequence') from None
IndexError: Cannot choose from an empty sequence
python random
python random
edited Nov 10 at 22:05
rm-
4,13922350
4,13922350
asked Nov 10 at 20:57
Jakoob
31
31
3
Please show the full traceback. There's nothing strange about the error.
– roganjosh
Nov 10 at 21:04
add a comment |
3
Please show the full traceback. There's nothing strange about the error.
– roganjosh
Nov 10 at 21:04
3
3
Please show the full traceback. There's nothing strange about the error.
– roganjosh
Nov 10 at 21:04
Please show the full traceback. There's nothing strange about the error.
– roganjosh
Nov 10 at 21:04
add a comment |
2 Answers
2
active
oldest
votes
up vote
0
down vote
accepted
That error is from the random
module by the looks of it. This is probably when you’re reading the file and there is a line that is blank.
A cause is usually the last line of the file which is often a blank newline/EOF.
Just add a check when reading your file(s):
for line in open(artist + '.txt', 'r'):
if line.strip():
song = line.strip().split()
song_name = random.choice(song)
An empty line will have a ‘truthiness’ value of 0 or False
so a line with content returnsTrue
.
add a comment |
up vote
0
down vote
Your error probably arises from an empty line in one of your text-files.
I was able to duplicate your error with your exact code and the following text-files:
pick.txt with only the word adele,
and adele.txt with hello-from-the-other-side and two new lines.
You can test this in prelude:
>>> for i in open("adele.txt"):
... song = i.split()
...
>>> song
On another note, you seem to be iterating throughout the lines in a textfile before you do anything with the data in the lines. That can't possibly make sense. I would advice you to add things in a list as you go, and then select from this list instead.
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
That error is from the random
module by the looks of it. This is probably when you’re reading the file and there is a line that is blank.
A cause is usually the last line of the file which is often a blank newline/EOF.
Just add a check when reading your file(s):
for line in open(artist + '.txt', 'r'):
if line.strip():
song = line.strip().split()
song_name = random.choice(song)
An empty line will have a ‘truthiness’ value of 0 or False
so a line with content returnsTrue
.
add a comment |
up vote
0
down vote
accepted
That error is from the random
module by the looks of it. This is probably when you’re reading the file and there is a line that is blank.
A cause is usually the last line of the file which is often a blank newline/EOF.
Just add a check when reading your file(s):
for line in open(artist + '.txt', 'r'):
if line.strip():
song = line.strip().split()
song_name = random.choice(song)
An empty line will have a ‘truthiness’ value of 0 or False
so a line with content returnsTrue
.
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
That error is from the random
module by the looks of it. This is probably when you’re reading the file and there is a line that is blank.
A cause is usually the last line of the file which is often a blank newline/EOF.
Just add a check when reading your file(s):
for line in open(artist + '.txt', 'r'):
if line.strip():
song = line.strip().split()
song_name = random.choice(song)
An empty line will have a ‘truthiness’ value of 0 or False
so a line with content returnsTrue
.
That error is from the random
module by the looks of it. This is probably when you’re reading the file and there is a line that is blank.
A cause is usually the last line of the file which is often a blank newline/EOF.
Just add a check when reading your file(s):
for line in open(artist + '.txt', 'r'):
if line.strip():
song = line.strip().split()
song_name = random.choice(song)
An empty line will have a ‘truthiness’ value of 0 or False
so a line with content returnsTrue
.
answered Nov 10 at 21:07
N Chauhan
1,637214
1,637214
add a comment |
add a comment |
up vote
0
down vote
Your error probably arises from an empty line in one of your text-files.
I was able to duplicate your error with your exact code and the following text-files:
pick.txt with only the word adele,
and adele.txt with hello-from-the-other-side and two new lines.
You can test this in prelude:
>>> for i in open("adele.txt"):
... song = i.split()
...
>>> song
On another note, you seem to be iterating throughout the lines in a textfile before you do anything with the data in the lines. That can't possibly make sense. I would advice you to add things in a list as you go, and then select from this list instead.
add a comment |
up vote
0
down vote
Your error probably arises from an empty line in one of your text-files.
I was able to duplicate your error with your exact code and the following text-files:
pick.txt with only the word adele,
and adele.txt with hello-from-the-other-side and two new lines.
You can test this in prelude:
>>> for i in open("adele.txt"):
... song = i.split()
...
>>> song
On another note, you seem to be iterating throughout the lines in a textfile before you do anything with the data in the lines. That can't possibly make sense. I would advice you to add things in a list as you go, and then select from this list instead.
add a comment |
up vote
0
down vote
up vote
0
down vote
Your error probably arises from an empty line in one of your text-files.
I was able to duplicate your error with your exact code and the following text-files:
pick.txt with only the word adele,
and adele.txt with hello-from-the-other-side and two new lines.
You can test this in prelude:
>>> for i in open("adele.txt"):
... song = i.split()
...
>>> song
On another note, you seem to be iterating throughout the lines in a textfile before you do anything with the data in the lines. That can't possibly make sense. I would advice you to add things in a list as you go, and then select from this list instead.
Your error probably arises from an empty line in one of your text-files.
I was able to duplicate your error with your exact code and the following text-files:
pick.txt with only the word adele,
and adele.txt with hello-from-the-other-side and two new lines.
You can test this in prelude:
>>> for i in open("adele.txt"):
... song = i.split()
...
>>> song
On another note, you seem to be iterating throughout the lines in a textfile before you do anything with the data in the lines. That can't possibly make sense. I would advice you to add things in a list as you go, and then select from this list instead.
answered Nov 10 at 21:29
RoyM
746
746
add a comment |
add a comment |
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%2f53243336%2fi-get-very-strange-error-while-im-trying-to-finish-my-project%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
Please show the full traceback. There's nothing strange about the error.
– roganjosh
Nov 10 at 21:04