Ok, so I got my code to move from \"if\" statement to \"if\" statement if the in
ID: 3637296 • Letter: O
Question
Ok, so I got my code to move from "if" statement to "if" statement if the input is a correct response, but now I can not get it to return the user to the original "if" question if they input anything but what was asked for. The code just goes to the next "if" statement if the user inputs an incorrect response. I tried using things like "break" and "return", but they just end the program all together after an incorrect response. Note that I am using the "else" command at the end of my "if" statement to account for any invalid inputs. Here is my code:
material = input('Enter Material Type (St, Al, CI): ', 's');
if material=='St'
q = 0.2854;
elseif material=='Al'
q = 0.0975;
elseif material=='CI'
q = 0.2601;
else
disp('Incorrect input. Try again');
end
Pod = input('Enter pully outside diameter: ');
Pw = input('Enter pully width: ');
Hod = input('Enter hub outside diameter: ');
Hw = input('Enter hub width: ');
hd = input('Enter hole diameter: ');
raw = input('Is raw material a casting (c) or cut from a bar (b)?: ','s');
if raw=='b'
rd = input('Enter bar raw diameter: ');
rl = input('Enter bar raw length: ');
Vr = (pi)*(rd)^2*(1/4)*(rl);
elseif raw=='c'
rpde = 0.25;
rple = 0.25;
rhde = 0.25;
rhle = 0.25;
Vr = (pi)*(Pod)^2*(1/4)*(Pw) + (pi)*(Hod)^2*(1/4)*(Hw) + rpde + rple + rhde + rhle;
else
disp('Incorrect input. Try again');
end
cs = input('Which belt cross-section is used (HA, HB, HC, or HD)?: ','s');
if cs=='HA'
GA = 32;
TW = 0.490;
GD = 0.490;
elseif cs=='HB'
GA = 32;
TW = 0.630;
GD = 0.580;
elseif cs=='HC'
GA = 34;
TW = 0.879;
GD = 0.780;
elseif cs=='HD'
GA = 34;
TW = 1.259;
GD = 1.060;
else
disp('Incorrect input. Try again');
end
V = 2*((pi)*(Pod)^2*(1/4)*((Pw-TW)/2))-(pi)*(hd)^2*(1/4)*(Pw + Hw)+ (2*(1/3)*(pi))*((Pod/2)^2 + ((Pod-2*GD)/2)^2 + ((Pod-2*GD)/2)^2*(Pod/2))*(GD*tan(GA/2)) + ((pi)*((Pod-2*GD)/2)^2*(TW-2*GD*tan(GA/2))) + (pi)*(Hod)^2*(1/4)*(Hw);
mr = Vr*q
mp = V*q
Explanation / Answer
Put the user input and if portion in a while loop. So, unless a valid value has been entered, the program will keep on asking the user to enter the value. When a valid value is entered, the control moves to one of if/elseif blocks and assigns valid=1 which will help the program to get out of the while loop and continue doing other calculations. Valid=0; While Valid == 0 material = input('Enter Material Type (St, Al, CI): ', 's'); if material=='St' q = 0.2854; Valid=1; elseif material=='Al' q = 0.0975; Valid=1; elseif material=='CI' q = 0.2601; Valid=1; else disp('Incorrect input. Try again'); Valid=0; end end
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.