Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

in Python, I have a code that writes user input to a text file each time it has completed running...I run the program again and

in Python, I have a code that writes user input to a text file each time it has completed running...I run the program again and it retrieves the text file information from the previous run. The problem is I don't know how I would get the program to stop asking the questions below if the user has already said yes once since I have to run the code anew each time. If they say yes, they get 100 points but I never want it to ask them that particular question again if they have said yes a previous time the code was ran if they are an existing user. I have no idea how to get around this or fix it. Otherwise, they can just keep getting points added each time for a goal they already passed.

EDITED: okay here is the entire function.. I bolded the original part. the variable a is assigned 100 for the number of points for a 'yes' input from user. bg stands for bench press goal. bsg stands for back squat goal. mpg stands for military press goal and dlg stands for dead lift goal. Hope this helps

def gain_weight(): #gain weight function try: if (os.path.exists('exapp.txt')): #existing user fin=open('exapp.txt', 'r')#fout if writing into file, fin for reading, f casually. May use any variable fin.readline() #ignoring first line as it was written gain or lose cw=int(fin.readline()[:-1]) #current weight iw=int(fin.readline()[:-1]) #ideal weight cb=int(fin.readline()[:-1]) #current max bench ib=int(fin.readline()[:-1]) #ideal bench cbs=int(fin.readline()[:-1]) #current max back squat ibs=int(fin.readline()[:-1]) #ideal max back squat cmp=int(fin.readline()[:-1]) #current military press imp=int(fin.readline()[:-1]) #ideal military press cdl=int(fin.readline()[:-1]) #current dead lift max idl=int(fin.readline()[:-1]) #ideal dead lift max

points=int(fin.readline()[:-1]) #points fin.close()

print(' Welcome back!')

print(' Your last entered weight: '+str(cw)) print('Your ideal weight: '+str(iw)) print('Your original bench press max: '+str(cb)) print('Your goal bench press max: '+str(ib)) print('Your original max back squat: '+str(cbs)) print('Your goal max back squat: '+str(ibs)) print('Your original military press: '+str(cmp)) print('Your goal max military press: '+str(imp)) print('Your original max dead lift: '+str(cdl)) print('Your goal max dead lift: '+str(idl)) print('Your current points: '+str(points)) #incorporate ability to include max bench etc... and earn points for exceeding that max rep bp=int(input('How many bench press reps did you perform?: ')) bs=int(input('How many back squat reps did you perform?: ')) mp=int(input('How many military press reps did you perform?: ')) dl=int(input('How many deadlifts did you perform?: '))

a=100 bg=str(input('Did you reach your max bench press goal of {0}?: yes/no'.format(ib))) if ('y' in bg.lower()): points+=a bsg=str(input('Did you reach your max back squat goal of {0}?: yes/no'.format(ibs))) if ('y' in bsg.lower()): points+=a mpg=str(input('Did you reach your max military press goal of {0}?: yes/no'.format(imp))) if ('y' in mpg.lower()): points+=a dlg=str(input('Did you reach your max dead lift goal today of {0}?: yes/no'.format(idl))) if ('y' in dlg.lower()): points+=a

summ=bp+bs+mp+dl

#increase points points=points+int(summ*0.2) print('You have earned {0} points today!'.format(points)) #new weight nw=int(input('Please enter your current weight in pounds: '))

while(nw<0): print('Please enter valid details') nw=int(input('Current weight in pounds?: ')) if(points>=1000 or nw>iw): print('Congratulations, You have completed your goal.') print('You are entitled to our free sample, please visit our website') print('and enter the coupon code EXERCISEAPP to redeem your free sample.') name=str(input(' Please enter your full name for the free sample: ')) address=str(input(' Please enter complete address, separated by commas: ')) zipcode=str(input(' Please enter the zip code: ')) fout=open('userdetails.txt','a')#fout if writing into file, fin for reading, f casually fout.write(name+' ') fout.write(address+' ') fout.write(zipcode+' ') fout.close() print(' Thank you for using the application! Your address details have been stored. You can now start off as a new user again!') os.remove('exapp.txt') else: f=open('exapp.txt','w')#fout if writing into file, fin for reading, f casually f.write('gain ') f.write(str(nw)+' ') f.write(str(iw)+' ') f.write(str(cb)+' ') f.write(str(ib)+' ') f.write(str(cbs)+' ') f.write(str(ibs)+' ') f.write(str(cdl)+' ') f.write(str(idl)+' ') f.write(str(cmp)+' ') f.write(str(imp)+' ') f.write(str(points)+' ') f.close() print(' Your new details have been stored! Workout everyday and stay healthy by eating nutritious meals!') print('Don\'t forget to log in often! Goodbye!')

else: #new user points=0 f=open('exapp.txt','w')#fout if writing into file, fin for reading, f casually f.write('gain ') cw=int(input('Please enter your current weight in pounds: ')) iw=int(input('Please enter your ideal weight in pounds: ')) while(iw print('Please enter a goal weight higher than your current weight.') cw=int(input('Please enter your current weight in pounds: ')) iw=int(input('Please enter your ideal weight in pounds: ')) cb=int(input('Please enter the beginning maximum weight you can bench press: ')) ib=int(input('Please enter your goal max bench press weight: ')) while(ib print ('Please enter a goal weight higher than your current weight.') cb=int(input('Please enter the current maximum weight you can bench press: ')) ib=int(input('Please enter your goal max bench press weight: ')) cbs=int(input('Please enter the beginning maximum weight you can back squat: ')) ibs=int(input('Please enter your goal max back squat weight: ')) while(ibs print ('Please enter a goal weight higher than your current weight.') cbs=int(input('Please enter the current maximum weight you can back squat: ')) ibs=int(input('Please enter your goal max back squat weight: ')) cmp=int(input('Please enter the beginnning maximum weight you can military press: ')) imp=int(input('Please enter your goal max military press: ')) while(imp print ('Please enter a goal weight higher than your current weight.') cmp=int(input('Please enter the current maximum weight you can military press: ')) imp=int(input('Please enter your goal max military press: ')) cdl=int(input('Please enter the beginning maximum weight you can deadlift: ')) idl=int(input('Please enter your goal max deadlift weight: ')) while(idl print ('Please enter a goal weight higher than your current weight.') cdl=int(input('Please enter the current maximum weight you can deadlift: ')) idl=int(input('Please enter your goal max deadlift weight: '))

f.write(str(cw)+' ') #fout if writing into file, fin for reading, f casually f.write(str(iw)+' ') f.write(str(cb)+' ') f.write(str(ib)+' ') f.write(str(cbs)+' ') f.write(str(ibs)+' ') f.write(str(cdl)+' ') f.write(str(idl)+' ') f.write(str(cmp)+' ') f.write(str(imp)+' ') f.write(str(points)+' ') f.close() print('Thank you! Your details have been saved.') print('Work out frequently and earn points and reward yourself with free samples!') print('The more you work out, the more points you will earn.') print('But dont forget to enter entries in the app frequently!') print('Exiting, have a great day!') return except: print('Some error occured, please contact admin') traceback.print_exc() return

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