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

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? yeso: ')) 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()

In my messed up Python code, Firstly, how do I correct for user input errors at this point in the code? How do I

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? yeso:) 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(

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!