In my messed up Python code, Firstly, how do I correct for user input errors at
ID: 3883367 • Letter: I
Question
In my messed up Python code,
Firstly, how do I correct for user input errors at this point in the code? How do I loop back to are you a new user and do you wish to gain or lose weight questions respectively if the user enters anything other than the yes or no regarding a new user and 1 or 2 regarding gain weight or lose weight? ALSO, how do I correctly apply/use the else statement which "opens the existing file" if they are NOT a new user?
import operator
import os
import traceback
def main():
print('Welcome to your exercise app!')
choice=str(input('Are you a new user? yes/no: '))
if(choice.lower()=='yes' or choice.lower()=='y'):
#delete the previously created file.
if(os.path.exists('exapp.txt')):
os.remove ('exapp.txt')
while choice.lower() != 'yes' or choice.lower() != 'y':
choice= str(input('Please enter yes or no only'))
if(choice.lower()=='yes' or choice.lower()=='y'):
if(os.path.exists('exapp.txt')):
os.remove ('exapp.txt')
goal=int(input(' Please enter your fitness choice 1. Gain Weight 2. Lose Weight: '))
while goal==1 or goal==2:
if(goal==1):
gain_weight()
elif(goal==2):
lose_weight()
else:
print('Please enter 1 or 2 only')
else:
#open existing file
try:
f=open('exapp.txt','r')
goal=f.readline()[:-1]
f.close()
if(goal=='gain'):
gain_weight()
elif(goal=='lose'):
lose_weight()
else:
print('No previous user data found. Please start as a new user.')
os.remove('exapp.txt')
except FileNotFoundError:
Print('No precious user data found. Please start as a new user.')
main()
1 import operator 2 import os 3 import traceback 4 5 def main): print('Welcome to your exercise appl') 9 10 choice-str(input 'Are you a new user? yes/no:) if(choice.lower()'yes or choice.lowe)'y 12 13 14 #delete the previously created file if(os.path.exists('exapp.txt')): os.remove (exapp.txt') 16 17 18 19 while choice.lower() != 'yes' or choice.lower() != 'y': choice str (input( Please enter yes or no only')) if(choice, lower():= 'yes' or choice. lower()-'y'): 21 if(os.path.exists(exapp.txt')): 23 24 25 os.remove ('exapp.txt') goal int(input (nPlease enter your fitness choice n1. Gain WeightIn2. Lose Weight: )) while goal=-1 or goal-2: 27 2 29 30 31 32 if(goal--1): elif (goal-2): else: gain_weight() lose weight() print ( Please enter 1 or 2 only') else: 34 35 36 37 #open existing file! try: f-open('exapp.txt','r') goal-f.readline():-1] f.close(Explanation / Answer
Hi I've removed errors from your python code:
Python Code:
import operator
import os
import traceback
def gain_weight():#your weight function
print("Your weight function")
def lose_weight():#your lose weight function
print("lose weight function")
def main():
print('Welcome to your exercise app!')
choice=str(input('Are you a new user? yes/no: '))
if(choice.lower()=='yes' or choice.lower()=='y'):
#delete the previously created file.
if(os.path.exists('exapp.txt')):
os.remove ('exapp.txt')
while choice.lower() == 'yes' or choice.lower() == 'y':
print(choice)
if(choice.lower()=='yes' or choice.lower()=='y'):
if(os.path.exists('exapp.txt')):
os.remove ('exapp.txt')
goal=int(input(' Please enter your fitness choice 1. Gain Weight 2. Lose Weight: '))
if(goal==1):
gain_weight()
elif(goal==2):
lose_weight()
else:
print('Please enter 1 or 2 only')
choice= str(input('Want to run again (yes or no) '))#if yes then loop back to menu
else:
#open existing file
my_file="exapp.txt"
if os.path.isfile(my_file):
try:
f=open('exapp.txt','r')
goal=f.readline()[:-1]
f.close()
if(goal=='gain'):
gain_weight()
elif(goal=='lose'):
lose_weight()
else:
print('No previous user data found. Please start as a new user.')
os.remove('exapp.txt')
except FileNotFoundError:
Print('No precious user data found. Please start as a new user.')
else:
print("File dosent exist")
main()#restart again if file dosent exist
main()
I hope this solves your problem
Comment if you have any problem in above code
And please give it a thumbs up if it solved your problem :)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.