Python Forum
Read from a file, get user input in a while loop
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Read from a file, get user input in a while loop
#1
Below is the code i have created for a music quiz game, firstly i need to know how to print the first letter of each song, and fix the while statement :)


--------------------------------------------------------------------
import random
songs = []
f = open("Songs.txt", "r")
wrong = 0
songs.append(f.readline())
songs.append(f.readline())
songs.append(f.readline())
songs.append(f.readline())
songs.append(f.readline())
songs.append(f.readline())
songs.append(f.readline())
songs.append(f.readline())
songs.append(f.readline())
songs.append(f.readline())

counter = 0
while counter <= 9:
    print(random.choice(songs))
    answer = input("Guess the song!")
    if answer == songs[counter]:
        print("Correct")
        counter += 1
    else:
        print("Incorrect")
        wrong +=  1
-----------------------------------------------------------
P.S These are the songs in my external file:
Output:
New_Patek_Lil_Uzi_Vert Despacito_Luis_Fonsi Rap_Devil_MGK Killshot_Eminem Lucky_You_Eminem_&_Joyner_Lucas Maskoff_Future Rockstar_Post_Malone I_Love_It_Kanye_West_&_Lil_Pump Its_Everyday_Bro_Jake_Paul Venom_Eminem
^------- these are correct, please do not change, it just needs to print the first letter, not the whole line :)

Thanks,
Lynden
Reply


Messages In This Thread
Read from a file, get user input in a while loop - by lynden - Oct-24-2018, 11:59 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Connecting to Remote Server to read contents of a file ChaitanyaSharma 1 479 May-03-2024, 07:23 AM
Last Post: Pedroski55
  Help with to check an Input list data with a data read from an external source sacharyya 3 669 Mar-09-2024, 12:33 PM
Last Post: Pedroski55
  Recommended way to read/create PDF file? Winfried 3 3,154 Nov-26-2023, 07:51 AM
Last Post: Pedroski55
  python Read each xlsx file and write it into csv with pipe delimiter mg24 4 1,904 Nov-09-2023, 10:56 AM
Last Post: mg24
  WHILE LOOP NOT RETURNING USER INPUT AFTER ZerroDivisionError! HELP! ayodele_martins1 7 1,340 Oct-01-2023, 07:36 PM
Last Post: ayodele_martins1
  read file txt on my pc to telegram bot api Tupa 0 1,312 Jul-06-2023, 01:52 AM
Last Post: Tupa
  parse/read from file seperated by dots giovanne 5 1,316 Jun-26-2023, 12:26 PM
Last Post: DeaD_EyE
  Formatting a date time string read from a csv file DosAtPython 5 1,684 Jun-19-2023, 02:12 PM
Last Post: DosAtPython
  How do I read and write a binary file in Python? blackears 6 8,536 Jun-06-2023, 06:37 PM
Last Post: rajeshgk
  restrict user input to numerical values MCL169 2 1,091 Apr-08-2023, 05:40 PM
Last Post: MCL169

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020