r/pythonhelp • u/normalrowan • Mar 07 '21
HOMEWORK I need to create a number guessing game with a score.
#Imports
import random
import time
import os
def Main():
file = open("score.txt","r")
score = int(file.read())
file.close()
print("your current score is" , score)
time.sleep(1)
sides = int(input("How many sides do you wish your dice to have(Difficulty): "))
if sides == 1:
sides = 2
guess = int(input("What do you think the role will be: "))
time.sleep(1)
print("\n\n\nyour dice roles are: \n")
role = int(random.randint(1,sides))
time.sleep(1)
f = open("score.txt","w")
if role == guess:
print("your role is" , role , "and you are correct")
score = score + sides*2
score = str(score)
f.write("0"+ score)
f.close()
Main()
else:
print("your role is" , role , "and you are incorrect" )
if score != 0:
score = score - sides
score = str(score)
f.write("0"+ score)
f.close()
Main()
else:
f.write("0")
f.close()
Main()
path = os.getcwd() + "/score.txt"
if os.path.exists(path):
Main()
else:
file1 = open("score.txt","w")
file1.write("0")
file1.close
Main()So, It works well, as long as a file exists with an integer value inside, whenever i try to create the file with "0" inside it doesn't write "0" it creates the file and everything but it wont write 0. Please help.




