Home > OS >  how to add scores every time the if statement is true?
how to add scores every time the if statement is true?

Time:06-15

I want to add a scoring system on my code. Every time that the player gets a correct answer, a 5 point score will be added to the player's overall score. This is my code below:

def easyLevel():
n = 2
while n <= 7:
    pattern = random.choice(string.ascii_lowercase)
    for i in range(n-1): 
        pattern = pattern   " "   random.choice(string.ascii_lowercase)
        
    print("The pattern is: ")
    print(pattern)

    easyAns = str(input("What was the pattern?: "))

    if easyAns == pattern:
        n = n   1
        print("That's correct!")
    else:
        print("Sorry, that's incorrect.")
        break

How can I save the scores in a file? Along with the last pattern that the player answered correctly.

Also, is there a way I could print the pattern and score outside of this function? if so, how can I do that?

Would appreciate your help. Thank you!

CodePudding user response:

What if you use a global variable to store the score?

import random
import string

score = 0 # set the global variable score to 0

def easyLevel():
    global score #this tells python that when we assign to score in this function, we want to change the global one, not the local one
    n = 2
    while n <= 7:
        pattern = random.choice(string.ascii_lowercase)
        for i in range(n-1): 
            pattern = pattern   " "   random.choice(string.ascii_lowercase)
            
        print("The pattern is: ")
        print(pattern)
    
        easyAns = str(input("What was the pattern?: "))
    
        if easyAns == pattern:
            n = n   1
            print("That's correct!")
            score = score   5 # so when we change score in here, it changes the global variable score
        else:
            print("Sorry, that's incorrect.")
            break

An alternative would be to use the score as a local variable, and return it when done. This would however not negate the need for a global score variable in some capacity.

Here's some more info from Stack: Using global variables in a function

Happy coding!

CodePudding user response:

add a variable to storing the score, then after the loop, save the last pattern and the score to a csv file

import random
import string
import csv

def easyLevel():
    n = 2
    score = 0
    last_pattern = None
    # start game
    while n <= 7:
        # make new pattern
        pattern = random.choice(string.ascii_lowercase)
        for i in range(n-1): 
            pattern = pattern   " "   random.choice(string.ascii_lowercase)

        print("The pattern is: ")
        print(pattern)
        
        # get input
        easyAns = input("What was the pattern?: ")

        if easyAns == pattern:
            n  = 1
            score  = 5
            print("That's correct!")
            # update last pattern
            last_pattern = pattern
        else:
            print("Sorry, that's incorrect.")
            break
        
    return last_pattern, score

game = easyLevel() # returns last_pattern and score
last_pattern, score = game

# show result
print("Last pattern :", last_pattern, ", Score :", score)  

# save score and last pattern in file
with open('data.csv', 'a') as file:
    csv_wrtier = csv.writer(file)
    csv_wrtier.writerow([last_pattern, score])

  • Related