python保存分数_在Python中存储分数

这篇博客介绍了一个Python游戏,玩家需猜测形状的面积并积累分数。博主遇到的问题是,当更新用户分数时,分数文件被覆盖导致进度丢失。代码展示了登录验证、菜单选择及分数更新等功能,但存在分数累加错误。博主寻求解决方案,以便实现用户进度的持久保存。
摘要由CSDN通过智能技术生成

到目前为止,我做了一个简短的游戏,在这个游戏中,你必须猜测所讨论形状的面积。到目前为止,只有三角形有效,正确答案是B。在

我试图将用户的进度存储到一个文本文件中,但是当我调用一个函数来更新分数时,它会覆盖该文件,这样分数就被删除了!我怎样才能绕过这个问题呢?如果我做不到,有没有其他方法可以做到这一点,因为我希望用户能够从一个级别转到另一个级别?在

我的代码:User_Points = '0'

def LoginScreen():

print("Welcome to Area Trainer")

print("Please enter the username for your account")

global user_name

user_name = str(input())

save = open(user_name + '.txt', 'w')

save.write(str(User_Points))

PasswordCheck= True

while PasswordCheck:

user_password = input("Type in your password: ")

if len(user_password) < 8:

print("Your password must be 8 characters long")

elif not any(i.isdigit() for i in user_password):

print("You need a number in your password")

elif not any(i.isupper() for i in user_password):

print("You need a capital letter in your password")

elif not any(i.islower() for i in user_password):

print("You need a lowercase letter in your password")

else:

PasswordCheck = False

def MenuTriangle():

global User_Points

User_Points = ''

print('''Here is a triangle with a height of 12 cm and a width of 29 cm

/\ | *Not to scale.

/ \ |

/ \ | 12 cm

/ \ |

29 cm

You must find out the area and select the correct answer from these options''')

print('''A) 175

B) 174

C) 2000

D) 199

''')

user_input = input().upper()

if user_input == "A":

print("I'm sorry this is incorrect, but you still have a chance to get 1 point!")

MenuTriangle2()

elif user_input == "C":

print("I'm sorry this is incorrect, but you still have a chance to get 1 point!")

MenuTriangle2()

elif user_input == "D":

print("I'm sorry this is incorrect, but you still have a chance to get 1 point!")

MenuTriangle2()

elif user_input == "B":

print("Congratulations! You got it right; someone's a smart cookie. Here have two points!")

reading = open(user_name + '.txt')

score = reading.read()

score = score + '2'

print("Your score is", score)

save = open(user_name + '.txt', 'a')

save.write(str(score))

MenuStart()

def MenuStart():

print("Welcome to the mathematical area game!")

print("In this game you will be required to calculate the area of multiple shapes.")

print("To do this you must know how to calculate the area of different shapes with increasing difficulty.")

print('''Please select a shape you want to play,

A) Triangle

B) Square

C) Circle''')

user_input = input().upper()

if user_input == "A":

print("You have chosen to calculate the area of a triangle!")

MenuTriangle()

elif user_input == "B":

print("You have chosen to calculate the area of a square!")

MenuSquare()

elif user_input == "C":

print("You have chosen the calculate the area of a circle!")

MenuCircle()

else:

print("Oops! I didn't understand that >:")

MenuStart()

LoginScreen()

MenuStart()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值