简体   繁体   中英

Reading a random line from a word doc and printing it

So I have a small project with python.

  • A random song name and artist are chosen.
  • The artist and the first letter of each word in the song title are displayed.
  • The user has two chances to guess the name of the song.
  • If the user guesses the answer correctly the first time, they score 3 points. If the user guesses the answer correctly the second time they score 1 point. The game repeats.
  • The game ends when a player guesses the song name incorrectly the second time.

So far I've created a text document and put a few lines of song titles.

In my code I have used the following:

random_lines = random.choice(open("songs.txt").readlines())

This randomly picks a line in the code and does nothing with it.

I am asking where I go from here. I need to display the first letters of each word on the line. I then need a counter or some sort to add chances. I also need to write something that will check to see if they have it correct and add to a score counter.

OK, now just continue with your plan, it's good. Now you have to get the first letter from each word in line. You can do that with:

res = []
for i in line.split():
  res.append(i[0])

There you are, you have the first letter of every word in the list res. Now you need to check if the user entered the title correctly. Maybe the best idea would be to keep everything lower-cased (in your file and in the user input) for easier checking. Now you just have to transform the user input to lower-case. You can do it with:

user_entry = input('Song title:')
if user_entry.lower() == line.lower():
  score += 3
else:
  user_entry_2 = input('Song title:')
  if user_entry_2.lower() == line.lower():
    score += 1
  else:
    print('Game over.')
    sys.exit()

You should make this into a function ad call it in a loop until user misses. The function could return the current score which you could print out (in that case you should remove sys.exit() call)

I hope this is clear enough. If not, write the question in the comments :)

Assuming your random choice string contains the data in the format {songname} - {artist}

Then you first need to get the song name and the artist as a separate strings.
Print the first letters and ask for input.

After which you need to compare the strings and do some logic with the points.

points = 0;
while(1):
    random_line = 'Song - artist' #change this with your random string
    song, artist = random_line.split('-')

    print("{0} - {1}".format(song.strip()[:2], artist.strip()[:2]))


    for i in range(0,3):
        if (i == 2):
            print('You died with {} points'.format(points))
            exit(0)
        elif(random_line.lower() == input('Gues the song: ').lower()):
            points += 2 - i
            print('correct guess. points: ' + str(points))
            break
        else:
            print('Try again')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM