Complete Guessing Game with Non-Volatile Storage in Python

"Multi Stage Guessing Game and File Storage to store current level"
Create a three stage guessing game which stores current stage of user in hard disk, in each stage the user must guess the secret number in five attempts. Range of Secret number should be increased at stage like 1 to 10 in stage in 1, 1 to 20 in stage 2 and 1 to 30 in stage 3. If the user correctly guessed the secret number, the system should display "your guess is correct" and moves to next stage; otherwise, it should tell the user whether the guessed number is higher or lower than the secret number and proceed to the next attempt. If the user fails to guess correctly after five attempts, the game should end with the message "Game Over, You Lose the Game." If user successfully guessed in all the three stages, display "You won the Game with scored points", then points scored at each level and stops the game.

Note : If user lefts or stops the game after completing any stage, it should starts from next stage when user runs the program again.

At each stage the points will be awarded to user based on following condition

Solved in points
first attempt100
second attempt75
third attempt50
fourth attempt25
fifth attempt10

Hint : Permanent  storage (File) is needed for this.
Objective : To learn the usage of Files

Try your Solution

Strongly recommended to Solve it on your own, Don't directly go to the solution given below.

#write your code here

Program or Solution

				
				
					
#hashed lines are comment lines used to illustrate the program
#import the built in method randint to generate an automatic secret number
from random import randint
#os package for remove files at last
import os
#sequence type to store points
points = [100,75,50,25,10]
#Open the file and check for next stage
f = open('guess.txt','a+')
f.seek(0,0)
content = f.read()
#check whether user completed any stage(s) already
if content != "":
    #split level and points at each level
level,points_content = content.split(":")
level = int(level)
    #store points of each level
points_scored = list(map(int,points_content.rstrip(',').split(",")))
else: #if no stage(s) completed
level = 1
points_scored = list()
f.close()
#loop for 3stages
while level <= 3:
#complexity range for each stage
end_value = level * 10
#secret number generation
secret_number = randint(1,end_value)
#looping statment for 5 attempts at each stage
for counter in range(0,5):
#gets the guessing number as user input
guess_number = int(input("Guess the secret number between 1 and {} ({} attempts left):".format(end_value,5-counter)))
#check guessing number is correct or higher or lower
if secret_number == guess_number:
print("Your Guess is Correct, You Won the level {} with {} points".format(level,points[counter]))
points_scored.append(points[counter])
level = level + 1

#write the current level to file
content = str(level) + ":"
for p in points_scored:
content += str(p) + ","
f = open('guess.txt','w')
f.write(content)
f.close()
break
elif guess_number < secret_number:
print("Your Guess is lower than secret number")
else:
print("Your Guess is higher than secret number")
else:
print("Game Over, You Loose the Game, secret number is {}".format(secret_number))
#remove if user lose the game
os.remove('guess.txt')
break
else:
print("Congratz, You Won the Game with {} !!!".format(sum(points_scored)))
for i in range(0,3):
print("level {} points {}".format(i+1, points_scored[i]))
#remove if user wins the game
os.remove('guess.txt')

Program Explanation

Watch Previous levels for the guessing game logic, in addition 

to store the level and points scored by user in hard disk we are using text file 'guess.txt.,

note:

split(':') split the string into list, colon (:) is the separator here

rstrip(',') removes comma(,) at the end of the string

open(), read(), seek(), write(), and close() are the file functions.

seek(0,0) moves the file pointer to beginning of the file.

open('guess.txt','a+') opens file in append mode (write without delete) and read mode.

Comments