4 Replies - 3167 Views - Last Post: 20 February 2012 - 07:15 AM Rate Topic: -----

#1 kid_19440  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 20-February 12

Hi I am getting a NameError: name 'Print ' is not defined.

Posted 20 February 2012 - 06:08 AM

# Declare Counter As Integer
# Declare Student_ID As String
# Declare Exam_score As Float
# Declare Sum_exam As Float
# Declare Avg_Exam As Float

Sum_exam = 0.0
Avg_exam = 0.0

Print(" This program will assist you in calculating the average score ")
Print(" based on input from 4 different exams. ")
Print(" Please enter student name as ID or enter Q to quit program: ")

Student_ID = input
while true: Student_ID != "Q or q"
forCounter in range (1, 5, 1)
Print(" Now enter your exam score ") + Counter
Exam_score = input
Sum_exam = Sum_exam + Exam_score

Avg_exam = Sum_exam/4

Print (" The following student: "+ Studend_ID + " has achieved.")
Print (" an average score of:   "+ Avg_exam + ".")

Print (" To continue enter another student name or enter Q to quit:")
Student_ID = input
Sum_exam = 0.0
Avg_exam = 0.0



This post has been edited by atraub: 20 February 2012 - 04:13 PM
Reason for edit:: added code tags


Is This A Good Question/Topic? 0
  • +

Replies To: Hi I am getting a NameError: name 'Print ' is not defined.

#2 Brewer  Icon User is offline

  • Awesome
  • member icon

Reputation: 179
  • View blog
  • Posts: 1,044
  • Joined: 14-June 10

Re: Hi I am getting a NameError: name 'Print ' is not defined.

Posted 20 February 2012 - 06:36 AM

Capitalization matters in Python. Try print("foo") instead of Print("foo").
Was This Post Helpful? 1
  • +
  • -

#3 kid_19440  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 20-February 12

Re: Hi I am getting a NameError: name 'Print ' is not defined.

Posted 20 February 2012 - 06:55 AM

View PostBrewer, on 20 February 2012 - 06:36 AM, said:

Capitalization matters in Python. Try print("foo") instead of Print("foo").



Thanks I realized that a few seconds after I made the post. For some reason I am getting all kinds of errors getting this to work. Do you have any suggestions? All I want is to get the average of the four test and then go to the next student but cant get passed the damm

While True:
Was This Post Helpful? 0
  • +
  • -

#4 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2113
  • View blog
  • Posts: 3,234
  • Joined: 21-June 11

Re: Hi I am getting a NameError: name 'Print ' is not defined.

Posted 20 February 2012 - 07:05 AM

The reason you can't get past it is that while True: is an infinite loop. What comes between while and the colon is the loop condition. The loop will loop as long as the condition is true. In your case the condition is True which will always be true. Thus infinite loop.

What comes after the colon is the loop body. So in your case Student_ID != "Q or q" is the loop body. This means that that line will be executed as long as True is true, i.e. forever. Since that line doesn't actually do anything, this will have no visible effect. Clearly you meant this to be the loop condition, so put it before the colon and remove the True.

Also note that Student_ID != "Q or q", will be true unless the user literally enters "Q or q" as his input.
Was This Post Helpful? 1
  • +
  • -

#5 Brewer  Icon User is offline

  • Awesome
  • member icon

Reputation: 179
  • View blog
  • Posts: 1,044
  • Joined: 14-June 10

Re: Hi I am getting a NameError: name 'Print ' is not defined.

Posted 20 February 2012 - 07:15 AM

View Postkid_19440, on 20 February 2012 - 10:25 AM, said:

View PostBrewer, on 20 February 2012 - 06:36 AM, said:

Capitalization matters in Python. Try print("foo") instead of Print("foo").



Thanks I realized that a few seconds after I made the post. For some reason I am getting all kinds of errors getting this to work. Do you have any suggestions? All I want is to get the average of the four test and then go to the next student but cant get passed the damm

While True:


What I think you're looking for is actually while Student_ID.lower() != 'q':. As sepp2k pointed out, while True: is an infinite loop and in this loop all you do is evaluate the expression Student_ID != "Q or q", which would never cause the loop to break.

My loop takes Student_ID and returns a copy with only lowercase characters. Therefore, we don't have to worry about the difference between 'Q' and 'q'. Another perfectly valid way of doing this loop would be while Student_ID != 'Q' and Student_ID != 'q':, but that is long and ugly.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1