Skip to content
Advertisement

how to properly write multiple if statements

trying to condense if statements and want to know if there is a proper why of writing this. Also this is my first post as well as being new to programming

JavaScript

Advertisement

Answer

First, you should review the logic to get marks: in your case, it prompts once, and if the entered mark is outside the range, it prompts again. However, if an invalid value is entered at the second time of asking, it simply accepts that value. Instead, you should use a while loop so that it keeps asking until a valid value is returned. You also probably want to account for the case where the entered number is not an integer. You can do this by trying to cast it to an int() and then catching the resulting ValueError.

Now, repeated code can be wrapped in a function, and then you just call that function:

JavaScript

Then, do

JavaScript
Advertisement