1 year ago

#376718

test-img

Fer

Variables are not incrementing and unable to sort list

I have been attempting to make a practice program to score points for correct answers in a quiz. The score isn't changing or rather holding the values, but resetting to zero.

Additionally, The score table is displaying the list of players from the csv file but not sorted.

I have looked and have tried to use score = score+1 and global score but neither seemed to work.

Any suggestions are much appreciated.


import csv, operator
import random
from sys import argv
from operator import itemgetter
from itertools import islice
count = 0
guess = (" ")
paper = (' ')
username = ' '
x = 0




#username and password validation
def authentication():
    login = False
    global username
    with open('users.csv', 'r') as csv_file:
        reader = csv.reader(csv_file)
username = input("Enter your username: ")
        password = input("Enter your password: ")
        for row in reader:
              if row[0]== username and row[1] == password:
                login = True
                break
              else:
                  login = False
    if login == False:
        print("Incorrect. Game over")
        #exit()
    else:
        print("You are logged in. Start the game!")
        
        
authentication()
    
def game():
    global guess
    global paper
    with open('newspapers.csv', 'r') as gfile:
        reader = csv.reader(gfile)
        newspaper = list(reader)
        random_paper = random.choice(newspaper)
#print(random_paper)
  #  for row in reader:
        paper = random_paper[0]
        country = random_paper[1]
        abb = paper.split()
        letters = [word[0] for word in abb]
        print ("".join(letters), ' ', country)
        guess = input("What is the newspaper? ")
        scoring()


def scoring():
    global count
    global guess
    global paper
    x = 0
    score = 0
    count = 0
    while count < 2:
        if guess == paper and count == 0:
            print("Correct!")
            x += 3
            count = 0
            print("Score: ", x)
            game()
        elif guess == paper and count == 1:
            print("Correct!")
            x +=1
            count =0
            print("Score: ", x)
            game()
        else:
            print("Incorrect")
            count = count + 1
            guess = input("What is the newspaper? ")
            if count == 2:
                print("Game over")
                break
            else:
                print()
    print("Your score is: ", x)
                

game()

def highscores():
    global username
    global x
    rank = 0
    newrow = (username, x)
    with open('Scores.csv', 'a',newline='') as sfile:
        writer = csv.writer(sfile)
        writer.writerow(newrow)
        

    with open('Scores.csv', 'r') as sfile:
        show_scores = csv.reader(sfile)
        #should sort list but isn't
        sortedList = sorted(show_scores, key=operator.itemgetter(1), reverse=True)
        for eachline in islice(sortedList, 1, 6):
            print(eachline)
        


highscores()```    

python

csv

sorting

variables

increment

0 Answers

Your Answer

Accepted video resources