Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Python code help for Write High score to file only to a HighScore.txt I have the write to a file wortking but I need this

Python code help for Write High score to file only to a HighScore.txt

I have the write to a file wortking but I need this to look at the total of the person playing and when exit and save the file to be looked at and see if the score being saved is higher than ther high score in the .txt file. if yes then just write the file only with the new high score number and name

I need to look for the file and create if does not find it.

The Try/Except block in python allows the programmer to catch expected errors in code. For example, FileNotFoundError will arise of the script attempts to read a file that does not exist.

In this class activity, enhance your Guessing Game with High Scores to handle the possibility of the highscores.txt file of not being available to your script. Below is the try/except format: example of what is needed------

try:

file = open("HighScore.txt", "r") line = file.readline()

except FileNotFoundError as err:

print("Oh no! Cannot find the file.") then create file using ----with open('HighScore.txt', 'a') as writeFile:

------below is my code--- Please type answer!

import random import csv from time import sleep

# correct number variable created global num global winCount # generates number at random comp_num = random.randint(1, 10) print('Hello! What is your name?') winCount = 0 num = 0

Name = input()

print('Hello, ' + Name + ', I am thinking of a number between 1 and 10.') # print('I am thinking of a number between 1 and 10... ')

# main game code def main(): # lives created guesses = 3 # correct number variable reset global num global winCount num = 0

while guesses >= 1: # player guesses guess = int(input('What is your guess? ')) if comp_num == guess: # if correct says well done input(' Well Done! You guessed Correctly! ') # player doesn't get told what the number is if there right winCount = winCount + 1 num = num + 1 break elif comp_num >= guess: # if guess is too low tells player # one live taken for incorrect guess guesses = guesses - 1 print(' Too low! ') # player is told how many lives they have left print('You guessed incorrectly. You have', guesses, ' guesses remaining. ') elif comp_num <= guess: # if guess is too high tells player # one live taken for incorrect guess guesses = guesses - 1 print(' Too high! ') # player is told how many lives they have left print('You guessed incorrectly. You have',guesses, ' guesses remaining. ')

def end(): global comp_num global Name # asks player if they want to play again play_again = input('Would you like to play again?[Y/N] ') while play_again.lower() == 'y': # if they do game resets and plays again if play_again.lower() == 'y': comp_num = random.randint(1, 10) print(' I\'m thinking of a number guess what it is... ') main() play_again = input('Would you like to play again?[Y/N] ') if play_again.lower() == 'n': break if play_again.lower() == 'n': # if they don't game ends print(winCount) if winCount > 0: fieldnames = ['Name', 'Score'] with open('HighScore.txt', 'a') as writeFile: writer = csv.writer(writeFile) writer = csv.DictWriter(writeFile, fieldnames=fieldnames) writer.writerow({'Name': Name, 'Score': winCount}) input(' Ok, Press enter to exit') exit()

main() if num != 1: # if player guesses incorrectly they get told the correct awnser print('The number I was thinking of was...', comp_num, '! ')

end()

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions