Posts: 4
Threads: 1
Joined: Apr 2017
Apr-02-2017, 06:58 PM
(This post was last modified: Apr-02-2017, 08:27 PM by Ofnuts.)
Hi, I have to create a Request class with suitable attributes to hold the request Id, password, name (first and surname), programme, year of study, campus location, module name and a list of available days (Monday …Sunday) and timeslots (morning, afternoon, evening). The user must be able to enter as many unique timeslots as they wish on a single request. I also have to use the __init method to accept a request ID passed from the calling object and allow the user to input the values for the other attributes.
This is what I have so far...
class Request:
def __init__(self, request_id, password, name, programme, year_of_study, campus_location, module_name,list_of_available_days):
# This is creating methods within a class, and defining what arguments we want to accept
self.request_id = request_id # Here I am setting the variables for the instance
self.password = password
self.name = name
self.programme = programme
self.year_of_study = year_of_study
self.campus_location = campus_location
self.module_name = module_name
self.list_of_available_days = list_of_available_days The problem is I don't know how to get user inputs like for example the request_id and so on. How do I go about doing that? Any help would be very much appreciated thank you.
Posts: 4,220
Threads: 97
Joined: Sep 2016
User input is usually done with the input function. I wouldn't think the request_id would be given by the user, I would have that set by the program. But a typical example would be:
self.name = input('Please enter your name: ') The string passed as a parameter to the input function is the text prompt the user would see.
Also, please use Python tags around your code ( https://python-forum.io/misc.php?action=help&hid=25), and don't use formatting tags for code (this is typically caused by cut and paste from an application doing the formatting. Try pasting to a plain text editor before cutting and pasting to the forum).
Posts: 4
Threads: 1
Joined: Apr 2017
Apr-02-2017, 07:35 PM
(This post was last modified: Apr-02-2017, 07:35 PM by Alaweey98.)
(Apr-02-2017, 07:28 PM)ichabod801 Wrote: User input is usually done with the input function. I wouldn't think the request_id would be given by the user, I would have that set by the program. But a typical example would be:
self.name = input('Please enter your name: ') The string passed as a parameter to the input function is the text prompt the user would see.
Also, please use Python tags around your code (https://python-forum.io/misc.php?action=help&hid=25), and don't use formatting tags for code (this is typically caused by cut and paste from an application doing the formatting. Try pasting to a plain text editor before cutting and pasting to the forum).
Ok thanks for that! but the self.name=input('Please enter your name: ') does that go under all the code I have written?
Posts: 4,220
Threads: 97
Joined: Sep 2016
You could put it in the __init__ when you set the attributes, but I wouldn't recommend that. I would typically have a function that gets all the required input from the user, and then passes that to __init__ when the object is created.
Posts: 4
Threads: 1
Joined: Apr 2017
Apr-02-2017, 07:44 PM
(This post was last modified: Apr-02-2017, 11:00 PM by sparkz_alot.)
(Apr-02-2017, 07:36 PM)ichabod801 Wrote: You could put it in the __init__ when you set the attributes, but I wouldn't recommend that. I would typically have a function that gets all the required input from the user, and then passes that to __init__ when the object is created.
This is what I have so far but every time I go to run the program it doesn't run?
class Request:
def __init__(self, request_id, password, name, programme, year_of_study, campus_location, module_name,
list_of_available_days):
# This is creating methods within a class, and defining what arguments we want to accept
self.request_id = request_id # Here I am setting the variables for the instance
self.password = password
self.name = name
self.programme = programme
self.year_of_study = year_of_study
self.campus_location = campus_location
self.module_name = module_name
self.list_of_available_days = list_of_available_days
self.request_id = input('What is your ID? ')
self.password = input('What is your password? ')
self.name = input('What is your name? ')
self.programme = input("What is your programme? ")
self.year_of_study = input ('What year are you currently in?')
self.campus_location = input('What is your campus location?')
self.module_name = input ('What is your module name')
self.list_of_available_days = input('Can you list the days that you are available?') Moderator sparkz_alot:
Once again, use code tags when posting code. Refer to the Help Doc
Posts: 2,953
Threads: 48
Joined: Sep 2016
Hello! Get rid of
self.request_id = request_id # Here I am setting the variables for the instance
self.password = password
self.name = name
self.programme = programme
self.year_of_study = year_of_study
self.campus_location = campus_location
self.module_name = module_name
self.list_of_available_days = list_of_available_days I am so tired that I can't even think about something else.
Posts: 4
Threads: 1
Joined: Apr 2017
(Apr-02-2017, 07:57 PM)wavic Wrote: Hello! Get rid of
self.request_id = request_id # Here I am setting the variables for the instance
self.password = password
self.name = name
self.programme = programme
self.year_of_study = year_of_study
self.campus_location = campus_location
self.module_name = module_name
self.list_of_available_days = list_of_available_days Every time I run the program I get this error message "Process finished with exit code 0"
Posts: 2,953
Threads: 48
Joined: Sep 2016
If you on Linux or I think Mac too that means without errors.
Posts: 4,220
Threads: 97
Joined: Sep 2016
The code is only ever going to run if you make an instance:
req = Request() Note that the above won't work unless you get rid of all the parameters to __init__ as well as the code wavic recommended getting rid of.
Posts: 2,953
Threads: 48
Joined: Sep 2016
As @ ichabod801 said, your code is only a definition of a class. You have to create an instance to this class in order to use it. As he did.
|