Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with Class
#1
Hi...just looking for assistance with this simple class with functions:

The class is located in a separate file "survey.py"

The program that uses the class is stored as file "languages_survey.py

class AnonymousSurvey():
	"""Collect anonymous answers to a survey question."""
	
	def __init__(self, question):
		"""Store a question, and prepare to store responses."""
		self.question = question
		self.responses = []
		
	def show_question(self):
		"""Show the survey question."""
		print(self.question)
		
	def store_response(self, new_response):
		"""Store a single response to the survey."""
		self.responses.append(new_response)
		
	def show_results(self):
		"""Show all the responses that have been given."""
		print("Survey results:")
		for response in self.responses:
			print('- ' + response)
from survey import AnonymousSurvey

# Define a question, and make a survey.
question = "What Language did you first learn to speak?"
my_survey = AnonymousSurvey(question)

# Show the question, and store responses to the question.
my_survey.show_question()
print("Enter 'q' at any time to quit.\n")
while True:
	response = input("Language: ")
	if response == 'q':
		break
	my_survey.store_response(response)
	
# Sho the survey results.
print("\nThank you to everyone who participated in the survey!")
my_survey.show_results()
Error:
What Language did you first learn to speak? Enter 'q' at any time to quit. Language: English Traceback (most recent call last): File "language_survey.py", line 11, in <module> response = input("Language: ") File "<string>", line 1, in <module> NameError: name 'English' is not defined ------------------ (program exited with code: 1) Press return to continue
At the languages prompt, I'm entering English and get the error. I know it's a simple program, but I've gone over it and can't seem to find the error that's referenced.
Reply
#2
I created two files with your code above and run your code without any errors.
I donĀ“t know what you do to get that error message.
Reply
#3
This is python3 code but I guess somehow you are running it with python2. Is that the case?

https://python-forum.io/Thread-Python3-2...-raw-input
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
That was it! Grrrrrrr.....

Set Geany to run python3 and BOOM.

Thanks for the help.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020