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









share|improve this question




















  • 3




    Please show the full traceback. There's nothing strange about the error.
    – roganjosh
    Nov 10 at 21:04















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









share|improve this question




















  • 3




    Please show the full traceback. There's nothing strange about the error.
    – roganjosh
    Nov 10 at 21:04













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









share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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














  • 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












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.






share|improve this answer




























    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.






    share|improve this answer





















      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',
      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
      });


      }
      });














       

      draft saved


      draft discarded


















      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

























      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.






      share|improve this answer

























        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.






        share|improve this answer























          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.






          share|improve this answer












          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.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 10 at 21:07









          N Chauhan

          1,637214




          1,637214
























              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.






              share|improve this answer

























                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.






                share|improve this answer























                  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.






                  share|improve this answer












                  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.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 10 at 21:29









                  RoyM

                  746




                  746






























                       

                      draft saved


                      draft discarded



















































                       


                      draft saved


                      draft discarded














                      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





















































                      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







                      Popular posts from this blog

                      Bressuire

                      Vorschmack

                      Quarantine