[Tutor] CSV file Reading in python
I have a CSV which I want to be able to read in Python. I don't know the exact syntax and I get error messages. Where can I find the syntax to do this. I juiste starter learning Python. Edzard. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] tiny, little issue with list
Hi Rafael, LogActivities = [] prompt = ("What have you done today? Enter 'quit' to exit. ") while True: activity = input(prompt) # Do this here so 'quit' is not appended to the list if activity == 'quit': # To get out of the loop break # If still in the loop, append this activity to the list # Note, no else statement here LogActivities.append(activity) # Do this outside the loop if LogActivities: # See if the list is not empty print("Let me recap. This is what you've done today: %s." % ", " .join(LogActivities)) Sri On Sun, Mar 19, 2017 at 5:47 PM, Rafael Knuth wrote: > LogActivities = [] > prompt = ("What have you done today? ") > prompt += ("Enter 'quit' to exit. ") > > while True: > activity = input(prompt) > LogActivities.append(activity) > > if activity == "quit": > print("Let me recap. This is what you've done today: %s." % ", > " .join(LogActivities)) > > This program is supposed to take user input and to log his activities > into a list. > All works well, only when the user quits, the program adds 'quit' to > LogActivities. > How do I prevent my program from doing this? > > Python 3.6.0 (v3.6.0:41df79263a11, Dec 23 2016, 07:18:10) [MSC v.1900 > 32 bit (Intel)] on win32 > Type "copyright", "credits" or "license()" for more information. > >>> > = RESTART: C:/Users/Rafael/Documents/01 - BIZ/PYTHON/Python > Code/PPC_159.py = > What have you done today? Enter 'quit' to exit. shower > What have you done today? Enter 'quit' to exit. walk the dog > What have you done today? Enter 'quit' to exit. drink coffee > What have you done today? Enter 'quit' to exit. prepare lunch > What have you done today? Enter 'quit' to exit. take coding lesson > What have you done today? Enter 'quit' to exit. quit > Let me recap. This is what you've done today: shower, walk the dog, > drink coffee, prepare lunch, take coding lesson, quit. > What have you done today? Enter 'quit' to exit. > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] HTML module for Python
Hi Looking for recommendations on Python module to use to generate HTML pages/tables, other HTML content. Kindly help. Regards Ni ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Using Class Properly - early beginner question
I am trying to write a food shopping list. The user should be able to add items to that shopping list, and later on decide what should happen to those purchased foods: instantly consumed or stored. My initial idea was to create a parent class to populate the shopping list and a child class to manage the purchased items as described above. While writing the parent class, I ran into the following issue: How do I properly declare a variable that takes user input? Do I write methods in the same fashion like in a regular function? And how do I call that class properly? This is what I came up with: class BuyFoods(object): def __init__(self, outlet): self.outlet = outlet def CreateShoppingList(self, shopping_list, prompt, food): self.shopping_list = shopping_list self.prompt = prompt self.food = food shopping_list = [] prompt = ("Which foods would you like to purchase?\nEnter 'quit' to exit. ") food = input(prompt) while food != "quit": shopping_list.append(food) food = input(prompt) print("You just purchased these foods: %s." % ", ".join(shopping_list)) Tesco = BuyFoods("Tesco") Tesco.CreateShoppingList() That's the error message I get: Python 3.6.0 (v3.6.0:41df79263a11, Dec 23 2016, 07:18:10) [MSC v.1900 32 bit (Intel)] on win32 Type "copyright", "credits" or "license()" for more information. >>> == RESTART: C:\Users\Rafael\Documents\01 - BIZ\PYTHON\Python Code\PPC_28.py == Traceback (most recent call last): File "C:\Users\Rafael\Documents\01 - BIZ\PYTHON\Python Code\PPC_28.py", line 136, in Tesco.CreateShoppingList() TypeError: CreateShoppingList() missing 3 required positional arguments: 'shopping_list', 'prompt', and 'food' >>> ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] CSV file Reading in python
> On Mar 20, 2017, at 14:37, Edzard de Vries wrote: > > I have a CSV which I want to be able to read in Python. I don't know the > exact syntax and I get error messages. Where can I find the syntax to do this. > I juiste starter learning Python. Edzard, Please post your code and the errors you are getting so we can see what issue you are having. — David Rock da...@graniteweb.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Using Class Properly - early beginner question
On Tue, Mar 21, 2017 at 12:20 PM, Rafael Knuth wrote: > > While writing the parent class, I ran into the following issue: > How do I properly declare a variable that takes user input? > Do I write methods in the same fashion like in a regular function? > And how do I call that class properly? When you "call the class" as you put it, you are asking the class to create a specific, new instance of that class, a new object. You may or may not have to pass one or more arguments into the __init__ method of that class. How do you know? What parameters (other than "self") does the __init__ method have? It needs those to create an object instance. What is required depends on how you set up your class' __init__ method. Once you have created an object from a class, then you may access attributes or methods defined for that object using "dot" notation. Methods are very much like functions with the exception that they are tied to an object, thus the "self" parameter which refers to that object instance. > This is what I came up with: > > class BuyFoods(object): > def __init__(self, outlet): > self.outlet = outlet > def CreateShoppingList(self, shopping_list, prompt, food): > self.shopping_list = shopping_list > self.prompt = prompt > self.food = food > shopping_list = [] > prompt = ("Which foods would you like to purchase?\nEnter > 'quit' to exit. ") > food = input(prompt) > > while food != "quit": > shopping_list.append(food) > food = input(prompt) > > print("You just purchased these foods: %s." % ", > ".join(shopping_list)) > > Tesco = BuyFoods("Tesco") Here is where you created your new object, "Tesco". Now you can access *this* object's methods and attributes with the dot notation. > Tesco.CreateShoppingList() And here you try to access the "CreateShoppingList" method of the object "Tesco". [Note: It is customary to use the same naming conventions for methods that you use for functions. So instead of using camel case for the method's name, it would be more customary to use a name format, "create_shopping_list".] But notice in your class definition that this method requires the following arguments to be passed into it: self, shopping_list, prompt, and food. You provided "self" automatically by creating the object, "Tesco", and accessing its "CreateShoppingList()" method, but you did not provide the other arguments. Thus the error message you get below. You would need to call it something like: Tesco.CreateShoppingList(['milk', 'bread', 'oranges'], 'Which foods would you like to purchase?\nEnter "quit" to exit.', 'spam') This would clear the immediate errors, but the logic of your method is not designed to make use of these arguments being passed in. Instead, you do nothing with them and basically ask for all of this information again. Does this make sense to you? > That's the error message I get: > > Python 3.6.0 (v3.6.0:41df79263a11, Dec 23 2016, 07:18:10) [MSC v.1900 > 32 bit (Intel)] on win32 > Type "copyright", "credits" or "license()" for more information. > == RESTART: C:\Users\Rafael\Documents\01 - BIZ\PYTHON\Python Code\PPC_28.py == > Traceback (most recent call last): > File "C:\Users\Rafael\Documents\01 - BIZ\PYTHON\Python > Code\PPC_28.py", line 136, in > Tesco.CreateShoppingList() > TypeError: CreateShoppingList() missing 3 required positional > arguments: 'shopping_list', 'prompt', and 'food' So in your method, to do what it looks like you are trying to do *with arguments*, you should be doing things like: food = input(self.prompt) self.shopping_list.append(self.food) etc. But if you truly want the user to be inputting this information, then you don't want your method to have these arguments! So you have to choose which approach you want to do. HTH! -- boB ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor