0

This is my code, i feel like the problem is in the *** bit. The error is just that it instead of outputting "well done", it outputs "sorry its wrong") Even when the input is correct

x = random.randint(0,3)

artisttextfile = open("artist.txt" , "r")
randomartist = artisttextfile.readlines()
print("The name of the artist is called " + randomartist[x])
artisttextfile.close()

songtextfile= open("songs.txt" , "r")
randomsong = songtextfile.readlines()
string=(randomsong[x])
songtextfile.close()
print("The first letter of the song is " + string[0])

answer=input("What is the name of the song?")

if answer == string:  # this line
  print("Well done")
else:
  print("sorry its wrong")

print("Try again")
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • Please show what `list(answer)` and `list(string)` contains – Bill Lynch Oct 13 '20 at 22:02
  • Why `if answer == string` has multiple `*` sign? Also please include the two files `songs.txt` and `artist.txt` so that we can check them. – Saeed Oct 13 '20 at 22:02
  • 1
    The lines read from the file will end with newline characters (except possibly the last line). The string read via `input()` will not have a newline, thus will never match. `if answer == string.rstrip()` would be one solution. – jasonharper Oct 13 '20 at 22:07
  • 1
    Does this answer your question? [How to read a file without newlines?](https://stackoverflow.com/questions/12330522/how-to-read-a-file-without-newlines) – Random Davis Oct 13 '20 at 22:09

0 Answers0