I'm still quite new to Python so I apologise for that. Anyways I have successfully programmed a number guessing game where the player must guess the correct number from 1 to 100 within the specified number of guesses, as generated by a dice roll(for e.g, rolling a 4 will give the player 4 tries). All goes well with the program, however, I want to be able to save the game statistics to a text file, preferably called ‘statistics.txt’, at the end of each game, formatted to look something like this:
Username | status | number of guesses
Here is the code for the program I made:
import random
###Is this where I should place an open("statisitics.txt", "a") function?###
print("Welcome to the guessing game! What is your name?: ")
userName = input()
print("Hello " +userName+"! \nLets see if you can guess the number between"
"\n1 and 100! What you roll on the dice becomes"
"\nyour amount of guesses!")
theNumber = random.randint(1, 100)
diceNum = random.randint(1,6)
guess_limit = diceNum
guess_count = 0
out_of_guess = False
loopcounter = 0
userRoll = input("\nPress enter to begin the dice roll!: ")
if userRoll == "":
print("You have "+str(diceNum)+" guesse(s)! Use them wisely!")
else:
print("You have " + str(diceNum) + " guesse(s)! Use them wisely!")
while loopcounter < diceNum:
print("Make a guess: ")
guessNumber = input()
guessNumber = int(guessNumber)
loopcounter = loopcounter + 1
if guessNumber < theNumber:
print("Higher!")
elif guessNumber > theNumber:
print("Lower!")
else:
loopcounter = str(loopcounter)
print("YOU WIN " +userName.upper() + "!!! You guessed the number " +loopcounter+ " times!")
break
if guessNumber != theNumber:
theNumber = str(theNumber)
print("You lose! The number was " +theNumber +"! Better luck next time!")
###Or is this where I should place an open("statisitics.txt", "a") function?###
Apologies again if this seems confusing to read. Any advice would be greatly appreciated.