[Tutor] Trying to select an input from another Python file & output that input in the main game program
D Rochester
rh at saintbedes.net
Thu Jun 17 07:50:26 EDT 2021
Good afternoon,
I have spent many hours trying to solve this problem. I have 4 Python files
(arc_songdataload, game_menu, Noels_Music_Game & User_Signup) & 3 csv files
for this game. It's a song guessing game. I am using IDLE Python version
3.7.4.
The first file (Noels_Music_Game) is for authenticating the users and it
looks at a csv file called 'users'. I have added another player to this
file so that it now spans 4 columns with the 1st & 3rd entries being the
users and the 2nd & 4th entries being the passwords. This element of the
code works fine and gives access to the game using a simple IF statement
detailed below;
import csv
import random
import time
import sys
import game_menu # main game is stored here as a menu() function below.
# opens users data
userdata = open("users.csv", "r")# Will read the file
userdataRead = csv.reader(userdata)
userdataList = list(userdataRead)
# username is abc or aaa
# password is 123 or 2101
# call loop variable for login
loop = 1
print("You need to log in before you can proceed.")
# starts loop for logging in
while loop == 1:
username = input("Enter your username;\n")
print(username)
if username == userdataList[0][0] or username == userdataList[0][2]:
#print(username)
print("Verified user. Please enter your password")
while True:
password = input("Enter your password.\n:")
if password == userdataList[0][1] or password ==
userdataList[0][3]:
print(username,"You have been logged in. Welcome to the
game.")
loop = 0
break # breaks loop and starts program
else:
print("Wrong password.")
else:
print("No such user registered. Please enter a valid username.")
game_menu.menu()
After access is granted the game begins. I have an additional 2 csv files
called 'scores' & 'songs'. After completing the game the output should
detail the user that was playing but it doesn't, it always details the 1st
user ('abc'). I know it's because of the line of code detailed in the
'game_menu'; scoredataAdd.writerow([globalpoints, userdataList[0][0]]). The
latter element references the first point in the csv file but what I need
it to do is to be able to reference the correct user from the opening file.
I can make it do that by [0][2] but that's not how it should work, it
should be automatically pulled into the output.
The following code is the actual game;
# importing files we'll need later
import csv
import random
import time
import sys
# variables here
gameloop1 = 1
globalpoints = 0
chances = 2
rngnum = 0
# random number generator for random songs (rng)
def rng():
global rngnum
rngnum = int(random.randint(0,5))
def menu():
# declare variables global so we can use them in the function
global rngnum
global gameloop1
global globalpoints
global chances
# intro text
print("Welcome to the song guessing game.")
print("You will be randomly presented with the first letter of each
song title")
print("and each artist of the song.")
print("Each one you get correct will add 2 points onto your score, 1 if
you take 2 guesses.")
print("You get 2 chances to guess. Guessing incorrectly on the second
guess")
print("will end the game.")
print("At the end, your high score will be displayed.")
print("The game will now start.")
# loads song data
songdata = open("songs.csv", "r")
songdataRead = csv.reader(songdata)
songdataList = list(songdataRead)
#loads user data
userdata = open("users.csv", "r")
userdataRead = csv.reader(userdata)
userdataList = list(userdataRead)
# appends score data
scoredata = open("scores.csv", "a", newline="")
scoredataAdd = csv.writer(scoredata)
# actual game
while gameloop1 == 1:
rng()
print("What song is it?")
print(songdataList[rngnum][0][0] + " , by " +
songdataList[rngnum][1])
#print(rngnum)
userinputchoice = input(": ")
if userinputchoice == songdataList[rngnum][0]:
if chances == 2:
globalpoints += 3
print("You have got " + str(globalpoints) + " points.\n")
chances = 2
#elif chances == 1:
#globalpoints += 1
#print("You have got " + str(globalpoints) + " points.\n")
#chances = 2
else:
chances -= 1
print("You have " + str(chances) + " chances left.\n")
if chances == 0:
gameloop1 = 0
gameloop2 = 1
while gameloop2 == 1:
print("Guess again.")
userinputchoice2 = input(": ")
if userinputchoice2 == songdataList[rngnum][0]:
if chances == 1:
globalpoints += 1
print("You have got " + str(globalpoints) + "
points.\n")
chances = 2
gameloop2 = 0
else:
gameloop2 = 0
gameloop1 = 0
#print("You have " + str(chances) + " chances left.\n")
print("The game has ended.")
print("Well done, you have scored", str(globalpoints) + ".")
#adds score to table
scoredataAdd.writerow([globalpoints, userdataList[0][0]])
#userdataList[0][0]])# Remember to amend the userdataList here as you have
added another user
scoredata.close()
print("Top 5 scores nationally:\n")
# quickly opens up score data
scoreRead = open("scores.csv", "r")
scoreReadReader = csv.DictReader(scoreRead)
# sorts the list of values
newList = sorted(scoreReadReader, key=lambda row: int(row['Score']),
reverse=True)
# prints scores
print("Rank | Score | Name\n")
for i, r in enumerate(newList[0:50]):
print('{} | {} | {}'.format(str(int(i)+1), r['Score'], r['Name']))
I hope that everything that I have detailed here makes sense? I would
hugely appreciate your help.
Kindest regards,
David
--
<http://www.st-bedes.surrey.sch.uk>
Are you a former St Bede's student?
Join our growing Alumni network -
Click here to find out more and sign up
<https://networks.futurefirst.org.uk/former-student/sbs>
--
If you have received this email in error, or the contents are considered
inappropriate, please notify the sender or
postmaster at st-bedes.surrey.sch.uk
<mailto:postmaster at st-bedes.surrey.sch.uk>
St Bede's School reserves the
right to monitor all incoming and outgoing mail.
More information about the Tutor
mailing list