[Tutor] Basics
Hi, I'm relatively new to this mailing list (and python!) and I would greatly appreciate some exercises to try or something to get me familiar with the system. Thanks, Cameron Macleod ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Infinite Loop
Hi, I've been trying to code a simple guess my number game as a starter programming project but I've generated an infinite loop accidentally. Since I'm new to programming in general, I can't really figure out what's causing it. Thanks === import random print("\tWelcome to 'Guess my Number'!") print("\nI'm thinking of a number between 1 and 100.") print("Try to guess it in as few attempts as possible.\n") #set the initial values the_number = random.randint(1, 100) guess = int(input("Take a guess: ")) tries = 1 # guessing loop while guess != the_number: if guess > the_number: print("Lower...") else: print("Higher...") guess = int(input("Take a guess: ")) tries += 1 print("You guessed it! The number was", the_number) print("And it only took you", tries, "tries!\n") if tries <= 5: print("I didn't know roadrunners could type!") elif tries >= 99: print("P'raps 'Only' wasn't the right word...") elif tries == 100: print("0_0 You are the unluckiest person in the world. Or the stupidest...") input("\n\nPress the enter key to exit.") ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Input
Hi, When you type Input("\n\nPress The Enter Key To Exit") it forces you to press the enter key to close the program. Why is it the enter key instead of e.g. the 'esc' key? ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Time subtractrion
Hi, I've been trying to code a timer that tells you how long you've been on the net and I can't figure out how to produce a figure in hours, minutes and seconds that is constantly being updated. If anyone could point out a module with functions like this or built in functions, I'd be very grateful. Cameron ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] __init__ doesn't seem to be running
Hello everyone, I'm using Python 3.3 and am trying to write a simple to-do list program. I have a class which runs pretty much everything called todo and the __init__ method doesn't seem to be running. class todo(): def __init__(self): tasks = [] def writeTask(todoList): name = input("Please enter the task > ") desc = input("Please enter a short description (optional) > ") while True: try: imp = int(input("How important is this task? 1-100 > ")) break except TypeError: imp = int(input("How important is this task? 1-100 > ")) if imp > 100: imp = 100 elif imp < 1: imp = 1 todoList.write("\"" + name + "\",\"" + desc + "\",\"" + str(imp) + "\"") print("Task written!") try: todoList = open('todo.txt', 'r+') except IOError: todoList = open('todo.txt', 'w') for line in todoList: tasks.append(line.strip()) writeTask(todoList) todoList.close() main = todo() Whenever I run this, I get the error: Traceback (most recent call last): File "C:\Python33\todo.py", line 8, in class todo(): File "C:\Python33\todo.py", line 34, in todo tasks.append(line.strip()) NameError: name 'tasks' is not defined Indicating that __init__ hasn't run since that is the initialization for tasks. I had always understood that __init__ was the equivalent of a constructor, and should be run at the instantiation of any class. Any help would be much appreciated, Cameron ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] __init__ doesn't seem to be running
I added the "self." to both the references to tasks and then I added the print statement to the __init__ function and I got the dreaded NameError of death: Traceback (most recent call last): File "C:\Python33\todo.py", line 6, in class todo: File "C:\Python33\todo.py", line 31, in todo self.tasks.append(line.strip()) NameError: name 'self' is not defined I then moved the entirety of the code that appeared outside of the functions but within the class into the writeTask function, this produced the longed after print statement and no errors, but was inappropriate for the task in hand. So I re-jiggled most of it into the __init__ method and left the call to writeTask outside of the class, passing in the argument main.todoList . This gave me the print statement (the init is running now!) but also this error: Traceback (most recent call last): File "C:\Python33\todo.py", line 34, in main.writeTask(main.todoList) AttributeError: 'todo' object has no attribute 'todoList' I fixed this with an attachment to self on the todoList object, but then got this error instead. Traceback (most recent call last): File "C:\Python33\todo.py", line 34, in main = todo() File "C:\Python33\todo.py", line 14, in __init__ for line in todoList: NameError: global name 'todoList' is not defined My code now looks something like this: class todo(): def __init__(self): self.tasks = [] print("Hello") try: self.todoList = open('todo.txt', 'r+') except IOError: self.todoList = open('todo.txt', 'w') for line in todoList: self.tasks.append(line.strip()) def writeTask(todoList): name = input("Please enter the task > ") desc = input("Please enter a short description (optional) > ") while True: try: imp = int(input("How important is this task? 1-100 > ")) break except TypeError: imp = int(input("How important is this task? 1-100 > ")) if imp > 100: imp = 100 elif imp < 1: imp = 1 todoList.write("\"" + name + "\",\"" + desc + "\",\"" + str(imp) + "\"") print("Task written!") main = todo() main.writeTask(main.todoList) It's recognising todoList as global now which is better than not being acknowledged at all, but why would it not be defined? (Sorry if this came up twice, wasn't sure whether or not I'd replied to all) On Fri, Mar 15, 2013 at 9:42 PM, Hugo Arts wrote: > On Fri, Mar 15, 2013 at 9:21 PM, Cameron Macleod < > cmacleod...@googlemail.com> wrote: > >> Hello everyone, I'm using Python 3.3 and am trying to write a simple >> to-do list program. I have a class which runs pretty much everything called >> todo and the __init__ method doesn't seem to be running. >> >> class todo(): >> def __init__(self): >> tasks = [] >> >> def writeTask(todoList): >> name = input("Please enter the task > ") >> desc = input("Please enter a short description (optional) > ") >> while True: >> try: >> imp = int(input("How important is this task? 1-100 > ")) >> break >> except TypeError: >> imp = int(input("How important is this task? 1-100 > ")) >> if imp > 100: >> imp = 100 >> elif imp < 1: >> imp = 1 >> todoList.write("\"" + name + "\",\"" + desc + "\",\"" + str(imp) >> + "\"") >> print("Task written!") >> try: >> todoList = open('todo.txt', 'r+') >> except IOError: >> todoList = open('todo.txt', 'w') >> for line in todoList: >> tasks.append(line.strip()) >> writeTask(todoList) >> todoList.close() >> >> main = todo() >> >> Whenever I run this, I get the error: >> >> Traceback (most recent call last): >> File "C:\Python33\todo.py", line 8, in >> class todo(): >> File "C:\Python33\todo.py", line 34, in todo >> tasks.append(line.strip()) >> NameError: name 'tasks' is not defined >> >> Indicating that __init__ hasn't run since that is the initialization for >> ta