0

currently I am getting to grips with Python and I am trying to produce a small script, however I am having issues with an IF statement, ideally I would like it so if the user inputs an "N" or 'n' for "No" then I like this sentence to be displayed "Thank you for using FinalGrade. Goodbye." However, it only loops back and reacts as if I had entered "Y", allowing another student to be inputted.

Heres my code:

results = []
cont = 'y' or 'Y'

print ("\n")
print ("-----------------------------------------------------------------------------")
print ("\n")
Institution = str(input("Please Enter the Name of Your Insitution: "))

while cont=='y' or 'Y':
   print ("\n")
   print ("---------------------------------NEW STUDENT---------------------------------")
   print ("\n")
   Year = str(input("Please Enter the Year of the Student (For Example, 'Year 1 / 2 / 3 / 4'): "))
   print ("\n")
   print ("-----------------------------------------------------------------------------")
   print ("\n")
   Student = str(input("Student Full Name: "))
   print ("\n")
   Grade1 = int(input("Enter Student's First Term Grade: "))
   Grade2 = int(input("Enter Student's Second Term Grade: "))
   Grade3 = int(input("Enter Student's Third Term Grade: "))
   Grade4 = int(input("Enter Student's Fourth Term Grade: "))

   average =  (Grade1+Grade2+Grade3+Grade4)/4

   print ("\n")
   print ("-----------------------------------------------------------------------------")
   print ("\n")
   print ("Total Grade Average: %G" % (average))

   passed_or_failed = "PASSED"
   if average < 40:
      passed_or_failed = 'FAILED'
   results.append(passed_or_failed)

   print ("\n")
   print ("%s has: %s" % (Student, passed_or_failed))
   print ("\n")

The main issues I am having in my code are shown below:

   cont = input('Do you want to keep entering students? Y/N: ')
if cont=='N' or 'n':
   print ("\n")
   print ("-----------------------------------------------------------------------------")
   print ("\n")
   print ("Thank you for using FinalGrade. Goodbye.")

Is there any solution to this problem? Thank you.

Oscar
  • 97
  • 4
  • 14
  • Try `if cont in ['N', 'n']:` Also duplicate question. – tobias_k Jan 10 '14 at 13:01
  • @tobias_k doesnt fix the issue :( Im not sure if Im doing something wrong or not but I replaced my "if cont=='N' or 'n':" with what you suggested and it didnt work – Oscar Jan 10 '14 at 13:05
  • Note that you have the same problem with the condition of the `while` loop! – tobias_k Jan 10 '14 at 13:15

2 Answers2

3

if cont=='N' or 'n':

You need to do either:

if cont in "nN":

or:

if cont in ["n", "N"]:

or even:

if cont.lower() == "n":

Writing what you had if cont=='N' or 'n': would not evaluate correctly as you expect. This is essentially saying:

  • if cont is ("N" or "n") then do something

Note: the brackets around ("N" or "n"); this will evaluate to True and then your if statement becomes: if cont == True: which always evaluates to True.

See:

>>> cont = "Y"
>>> if cont == "N" or "n":
...     print "cont is N or n"
... 
cont is N or n

Update::

You will also want to change your code structure a bit as well to something like this:

while True:
    ... most of your code ...

    cont = raw_input("Do you want to continue? (Y/N)")
    if cont.lower() == "n":
        break

Update II: From your comments Here is a complete corrected version of your program: #!/usr/bin/env python

#FinalGrade

results = []

print ("\n")
print ("-----------------------------------------------------------------------------")
print ("\n")
Institution = str(input("Please Enter the Name of Your Insitution: "))

while True:
   print ("\n")
   print ("---------------------------------NEW STUDENT---------------------------------")
   print ("\n")
   Year = str(input("Please Enter the Year of the Student (For Example, 'Year 1 / 2 / 3 / 4'): "))
   print ("\n")
   print ("-----------------------------------------------------------------------------")
   print ("\n")
   Student = str(input("Student Full Name: "))
   print ("\n")
   Grade1 = int(input("Enter Student's First Term Grade: "))
   Grade2 = int(input("Enter Student's Second Term Grade: "))
   Grade3 = int(input("Enter Student's Third Term Grade: "))
   Grade4 = int(input("Enter Student's Fourth Term Grade: "))

   average =  (Grade1+Grade2+Grade3+Grade4)/4

   print ("\n")
   print ("-----------------------------------------------------------------------------")
   print ("\n")
   print ("Total Grade Average: %G" % (average))

   passed_or_failed = "PASSED"
   if average < 40:
      passed_or_failed = 'FAILED'
   results.append(passed_or_failed)

   print ("\n")
   print ("%s has: %s" % (Student, passed_or_failed))
   print ("\n")

   cont = input('Do you want to keep entering students? Y/N: ')

   if cont.lower() == "n":
      print ("\n")
      print ("-----------------------------------------------------------------------------")
      print ("\n")
      print ("Thank you for using FinalGrade. Goodbye.")
      break

Sample run: http://codepad.org/hvoYCXWL

Note that the condition to check for entering more data is properly indented inside the while loop's block. This is important.

James Mills
  • 18,669
  • 3
  • 49
  • 62
  • Hi I tried both of your examples of solutions and the sentance still isnt being displayed, instead it just loops back to the NEW STUDENT section :( Am I doing something wrong? – Oscar Jan 10 '14 at 13:10
  • I don't think your logic ever broke out of the loop :/ – James Mills Jan 10 '14 at 13:14
  • Tried the updated answer now too and I get nothing if I put "N" and if I put "Y" I get the sentence along with the loop of new student – Oscar Jan 10 '14 at 13:22
  • Please paste your full code to somewhere like http://codepad.org -- It wasn't clear where the 2nd part of your code pasted in your question was to fit into your loop construct. In general you are misusing boolean expressions here. Python has quite clear operator precedence and evaluation order. – James Mills Jan 10 '14 at 13:25
  • I have pasted it: http://codepad.org/o5UQl0GR - Sorry for being unclear in the question. Thank you for all your help. – Oscar Jan 10 '14 at 13:27
  • Updated answer with a corrected version of your program. – James Mills Jan 10 '14 at 13:34
  • Wow! Thank you so much @James, fully appreciate your help! I can see where Ive gone wrong too. Thanks again :) – Oscar Jan 10 '14 at 13:45
0
if cont=='N' or 'n':

Should be

if cont=='N' or cont == 'n':

Or better

if cont in [ 'N', 'n' ]:
glglgl
  • 89,107
  • 13
  • 149
  • 217
Raul Andres
  • 3,766
  • 15
  • 24