To try to solve the question above I tried by creating a list with the range and then a list with the variables in it. Then to check if the variables are in the list i used an if loop however it is not working and only printing out "out of range...". I have also tried a while loop and it would just repeat the total 7 times. I do not know how to fix it and would please like an answer. Thank you
def main():
a = [0, 20, 40, 60, 80, 100, 120]
try:
userPass = int(input("Enter pass credits: "))
userDefer = int(input("Enter defer credits: "))
userFail = int(input("Enter fail credits: "))
except:
print("Invalid number.")
total = userFail userPass userDefer
aValues = [ userPass, userFail, userDefer ]
if aValues in a:
print(total)
else:
print("Out of range. Try again")
if total > 120:
print("Total incorrect")
repeat()
else:
if userPass >= 120:
print("This student has progressed.")
repeat()
elif userPass >= 100 and total == 120:
print("This student is trailing.")
repeat()
elif userFail <= 60 and total == 120:
print("This student did not progress(module retreiver).")
repeat()
elif userPass <= userFail:
print("This students program outcome is exclude.")
repeat()
else:
print("incorrect. Try again.")
repeat()
def repeat():
choice = int(input("If you would like to quit type 1 or test another student write 2: "))
if choice != 1 and choice != 2:
repeat()
else:
while choice == 2:
main()
while choice == 1:
break
print("Thank you.")
main()
CodePudding user response:
Testing whether one set of numbers is in another is a set operation. So use set
and subtract all of the valid values from your input. If any values remain, they are not valid.
a = set([0, 20, 40, 60, 80, 100, 120])
if set([userPass, userFail, userDefer]) - a:
print("Out of range. Try again")
Interestingly, you can also use a range
object. It is built to check for containment efficiently
a = range(0, 121, 20)
if any(val in a, for val not in (userPass, userFail, userDefer)):
print("Out of range. Try again")
CodePudding user response:
You need to change the first if.
if aValues[0] in a and aValues[1] in a and aValues[2] in a :
print(total)
if you need to put more values you can do this:
total=0
for i in range(len(aValues)):
if(aValues[i] in a):
total=total aValues[i]
else:
print('Incorrect Value')
print(total)