![]() |
Read from a file, get user input in a while loop - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Read from a file, get user input in a while loop (/thread-13632.html) |
Read from a file, get user input in a while loop - lynden - Oct-24-2018 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: ^------- these are correct, please do not change, it just needs to print the first letter, not the whole line :)Thanks, Lynden RE: Read from a file, get user input in a while loop - ichabod801 - Oct-24-2018 To get the first character of a string, you just use an index: text[0]. I think you want your while loop to be while counter + wrong < 9: . If that doesn't work, please clarify what is wrong with the while loop.
RE: Read from a file, get user input in a while loop - lynden - Oct-25-2018 I tried your loop and the problem is still the same, I answer the song correctly, for example Maskoff_Future. I type that it for an input and it still comes up as wrong :) p.s im new to this forum so sorry if the tags are wrong. RE: Read from a file, get user input in a while loop - buran - Oct-25-2018 The problem is in this if condition if answer == songs[counter]: .Let's see what will happen when counter = 0 (i.e., first question/answer) on line 18 you will print a randomly selected song from the list. It could be any song of the list, e.g. let's say it is the third one. Here you want to print just the first letter I guess, but anyway. You also need to save this randomly selected song to a variable in order to be able to check user answer. Instead you compare the user answer with the first song in the list (i.e. the one with index 0). Even if you answered correctly if condition will be False. In next iteration of the loop counter will still be 0 and you will compare user input with the first song in the list. But randomly selected song would be different. There are other problems in your code and logic repetitive code when you read line by line from the file there are better ways to open file, e.g. using with context manageras it is now it's possible to get same song many times (i.e. ask same question multiple times) RE: Read from a file, get user input in a while loop - lynden - Oct-25-2018 Thanks alot, I'm really new to coding so how do you write this? Thanks for helping RE: Read from a file, get user input in a while loop - buran - Oct-25-2018 Two more issues: when reading song names from the file you will get extra new line character at the end of the string, so you need to strip that '\n' char or your answer and the song name will not match counter - you use counter to hold number of correct answers. yet it is part of the while condition. Do you really want to loop until you get 9 correct answers?not tested but something like this import random with open("Songs.txt", "r") as f: songs = [] for line in f: songs.append(line.strip()) # you can replace last 3 lines with single line as follows #songs = [line.strip() for line in f] random.shuffle(songs) # randomize songs list correct = 0 wrong = 0 # you can replace last 2 lines with single line as follows #correct, wrong = 0, 0 while songs: song = songs.pop() # get one song and remove it from the list answer = input("Guess the song!\n{}\n>> ".format(song[0]) if answer == song: print("Correct") correct += 1 else: print("Incorrect. It was {}".format(song)) wrong += 1 print('correct: {}, wrong: {}'.format(correct, wrong) RE: Read from a file, get user input in a while loop - lynden - Nov-04-2018 Thank you, but it is coming up with a syntax error on line 19 (the bracket) and I've tried to make it better but it keeps coming up as wrong. Thanks for helping a newbie :) RE: Read from a file, get user input in a while loop - buran - Nov-04-2018 (Nov-04-2018, 02:14 PM)lynden Wrote: Thank you, but it is coming up with a syntax error on line 19 (the bracket) and I've tried to make it better but it keeps coming up as wrong.Sorry, my mistake - there is missing closing parenthesis on line 18 answer = input("Guess the song!\n{}\n>> ".format(song[0])) RE: Read from a file, get user input in a while loop - lynden - Nov-05-2018 import random with open("Songs.txt", "r") as f: songs = [] for line in f: songs.append(line.strip()) random.shuffle(songs) correct = 0 wrong = 0 while songs: song = songs.pop() guess = input("Guess the song!\n{}\n>> ".format(song[0])) if guess == song: print("Correct") correct += 1 else: print("Incorrect. It was {}".format(song)) wrong += 1 print("correct: {}, wrong: {}".format(correct, wrong)) |