Re: [Tutor] Trouble creating a pygame.font.SysFont, was Re: (no subject)
It seems I forgot to write pygame.init() Thank you guys for your time it is much appreciated. On Saturday, 29 November 2014, 15:31, Peter Otten <__pete...@web.de> wrote: William wrote: Hello William; please provide a meaningful subject line so that your readers get a hint whether your question is in their area of expertise. > Hello there I'm using Python 3.4 running on Ubuntu 14.04LTS > I'm new to programming and also new to linux > I'm trying to learn the pygame library > I was reading : > > http://programarcadegames.com/index.php?chapter=introduction_to_graphics&lang=en#section_5 > > On the section on how to draw text the writer has the following code > | > font ||=||pygame.font.SysFont(||'Calibri'||, ||25||, ||True||, ||False||)| > |text ||=||font.render(||"My text"||,||True||,BLACK) > ||screen.blit(text, [||250||, ||250||]) That is very hard to read. If you have to post Python code please do it in plain text without those funny "|". Thank you. > When I run the code using the IDLE the Python shell gives me the > following error > > Traceback (most recent call last): > File "/home/william/Desktop/Pygame/Python 3X/Drawing_Shapes.py", line > 48, in > font = pygame.font.SysFont('Calibri', 25, True, False) > File "/usr/local/lib/python3.4/dist-packages/pygame/sysfont.py", line > 614, in SysFont > return constructor(fontname, size, set_bold, set_italic) > File "/usr/local/lib/python3.4/dist-packages/pygame/sysfont.py", line > 537, in font_constructor > font = pygame.font.Font(fontpath, size) > pygame.error: font not initialized > > > I think the problem is that the fonts in Ubuntu are probably stored in a > different > location than in Windows OS.And therefore python can not find it using > the SysFont > method > Can anyone give me any suggestion on how to edit the code so that I can > write text > on my graphics? Quoting the page you link to: """ The first code a Pygame program needs to do is load and initialize the Pygame library. Every program that uses Pygame should start with these lines: Importing and initializing Pygame # Import a library of functions called 'pygame' import pygame # Initialize the game engine pygame.init() """ Did you follow that advice? If you did and your script is reasonably short please post it here so that we can have a look or try to run it ourselves. ___ 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] Sets Python
I am not understanding how I am supposed to do the highlighted things on the problem statement. If someone could please direct me on the steps to go about doing what is highlighted. Have do I read each command, by making the program read each line?? How do I add the invalid commands into a set? When printing the command is invalid, do I simply do as I did with the valid commands print? Problem Statement: We are going to write a program that reads an input file of commands (Commands.txt) to Arnold the Robot. The four valid command are: Up Down Left Right After you read each command, make sure it is valid (included in the set of valid commands). If the command is valid, print “Valid command entered, the command was:” and the command. If the command is invalid, print “Invalid command entered, the command was:” and the command. If the command is invalid, add it to a set of invalid commands. Keep a count of: The total number of commands read The total number of valid commands read The total number of invalid commands read After all of the commands have been read: Print the total number of commands read Print the total number of valid commands read Print the total number of invalid commands read Print the number of unique invalid commands read Print the set of invalid commands. The format of the input files is: Each line in the file contains a command, not all command are valid My Code So Far: def main() : # Define main variables leftCount = 0 rightCount = 0 upCount = 0 downCount = 0 commandCount = 0 invalidCommands = 0 command = ("left", "right", "up", "down") numUnique = 0 numInvalid = 0 invalidCommands = set() filename = input("Enter filename (default: Commands.txt): ") if len(filename) == 0 : filename = "Commands.txt" inputFile = open(filename, "r") command = input("Please enter a command for Arnold: ") if command in command : commandCount = commandCount + 1 print("The total amount of commands: ", commandCount) if command == "up" : upCount = upCount + 1 print("Valid command entered, the command was: up") elif command == "down" : downCount = downCount + 1 print("Valid command entered, the command was: down") elif command == "left" : leftCount = leftCount + 1 print("Valid command entered, the command was: left") elif command == "right" : rightCount = rightCount + 1 print("Valid command entered, the command was: right") else : print("Invalid command entered, the command entered was: ", command) numInvalid = numInvalid + 1 if not command in invalidCommands : numUnique = numUnique + 1 invalidCommands.add(command) for line in inputFile : theWords = line.split() for word in theWords : cleaned = clean(word) if cleaned != "" : numUnique.append(cleaned) print("The file contains", len(numUnique), "unique words.") # Cleans a string by making letters lowercase and removing characters that are not letters # @param string the string to be cleaned # @return the cleaned string def clean(string) : result = "" for char in string : if char.isalpha() : result = result + char return result.lower() # Start the program. main() What Happens When Ran: 3.4.1 (v3.4.1:c0e311e010fc, May 18 2014, 10:38:22) [MSC v.1600 32 bit (Intel)] Python Type "help", "copyright", "credits" or "license" for more information. [evaluate CommandsDraft.py] Enter filename (default: Commands.txt): Commands.txt Please enter a command for Arnold: dawn The total amount of commands: 1 Invalid Traceback (most recent call last): File "C:\Program Files (x86)\Wing IDE 101 5.0\src\debug\tserver\_sandbox.py", line 70, in File "C:\Program Files (x86)\Wing IDE 101 5.0\src\debug\tserver\_sandbox.py", line 52, in main builtins.AttributeError: 'int' object has no attribute 'append'___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Sets Python
On 01/12/14 01:01, niyanax...@gmail.com wrote: Have do I read each command, by making the program read each line?? Yes, each line of the file contains one command. read the file line by line. Do you know how to do that? How do I add the invalid commands into a set? Do you know how to create and use sets in Python? This exercise seems to be based on using sets so I would expect your teacher/tutorial to have explained sets before setting the problem. When printing the command is invalid, do I simply do as I did with the valid commands print? You just use the print function as usual. Problem Statement: We are going to write a program that reads an input file of commands (Commands.txt) to Arnold the Robot. The four valid command are: Up Down Left Right You need to store these four strings in a set. After you read each command, make sure it is valid (included in the */set/*of valid commands). Then check the input from the file against the contents of your set. If the command is valid, print “Valid command entered, the command was:” and the command. If the command is invalid, print “Invalid command entered, the command was:” and the command. Use an if/else statement to print the valid or invalid result. If the command is invalid, add it to a */set/*of invalid commands. And if its invalid add the new command to a new set of invalid commands, which starts out empty. Keep a count of: The total number of commands read The total number of valid commands read The total number of invalid commands read You will need counters for these and increment them after each command. After all of the commands have been read: Print the total number of commands read Print the total number of valid commands read Print the total number of invalid commands read These are just the counters above Print the number of unique invalid commands read Print the set of invalid commands. This is just the size of the invalid set and the members of that set. def main() : # Define main variables leftCount = 0 rightCount = 0 upCount = 0 downCount = 0 You aren't asked to count each individual command just the total of valid commands so you could simplify this by having one variable instead of 4. commandCount = 0 invalidCommands = 0 command = ("left", "right", "up", "down") This is not a set. You were asked to use a set. numUnique = 0 numInvalid = 0 You already have invalidCommands what is the purpose of this new variable? invalidCommands = set() Is it because this wipes out the original variable? filename = input("Enter filename (default: Commands.txt): ") if len(filename) == 0 : filename = "Commands.txt" inputFile = open(filename, "r") So far so good. command = input("Please enter a command for Arnold: ") But this is wrong. The commands are in the file so you need to loop over the file reading and processing each line. if command in command : Here you are trying to use two variables with the same name - that won;t work. You need to rename your set of valid commands - to validCommands maybe? commandCount = commandCount + 1 print("The total amount of commands: ", commandCount) You only need to print the totals at the end. But you are supposed to print a message: > If the command is valid, > print “Valid command entered, the command was:” > and the command. In programming it is important to stick to the specification you have been given and not try to interpret the problem in your own way. if command == "up" : upCount = upCount + 1 print("Valid command entered, the command was: up") elif command == "down" : downCount = downCount + 1 print("Valid command entered, the command was: down") elif command == "left" : leftCount = leftCount + 1 print("Valid command entered, the command was: left") elif command == "right" : rightCount = rightCount + 1 print("Valid command entered, the command was: right") This is OK but its more complicated than it needs to be to meet the specification. else : print("Invalid command entered, the command entered was: ", command) numInvalid = numInvalid + 1 if not command in invalidCommands : numUnique = numUnique + 1 invalidCommands.add(command) Again this looks ok. for line in inputFile : theWords = line.split() for word in theWords : cleaned = clean(word) if cleaned != "" : numUnique.append(cleaned) print("The file contains", len(
Re: [Tutor] Sets PythonTutor Digest, Vol 130, Issue 1
by making letters lowercase and removing characters that are not letters > # @param string the string to be cleaned > # @return the cleaned string > def clean(string) : >result = "" >for char in string : > if char.isalpha() : > result = result + char > return result.lower() > > > > > > # Start the program. > main() > > > > What Happens When Ran: > > 3.4.1 (v3.4.1:c0e311e010fc, May 18 2014, 10:38:22) [MSC v.1600 32 bit (Intel)] > Python Type "help", "copyright", "credits" or "license" for more information. > [evaluate CommandsDraft.py] > Enter filename (default: Commands.txt): Commands.txt > Please enter a command for Arnold: dawn > The total amount of commands: 1 > Invalid > Traceback (most recent call last): > File "C:\Program Files (x86)\Wing IDE 101 5.0\src\debug\tserver\_sandbox.py", line 70, in > File "C:\Program Files (x86)\Wing IDE 101 5.0\src\debug\tserver\_sandbox.py", line 52, in main > builtins.AttributeError: 'int' object has no attribute 'append' > -- next part -- > An HTML attachment was scrubbed... > URL: < http://mail.python.org/pipermail/tutor/attachments/20141201/fecd2537/attachment.html > > > -- > > Subject: Digest Footer > > ___ > Tutor maillist - Tutor@python.org > https://mail.python.org/mailman/listinfo/tutor > > > -- > > End of Tutor Digest, Vol 130, Issue 1 > * ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Pygame fonts not been initialised
If you still don't have a solution I suggest using the default font for pygame freesansbold.tiff it is located in the python library files just search for it in the python-pygame directory. I think your code fails because it does not pick the font you wanted hence not initialised. Below is a sample code of a font been used from a file you can equally download fonts from google fonts fontObj = pygame.font.Font("freesansbold.ttf", 40) fontObj is our object been initialised. Hope it helps On Sun, Nov 30, 2014 at 12:00 PM, wrote: > Send Tutor mailing list submissions to > tutor@python.org > > To subscribe or unsubscribe via the World Wide Web, visit > https://mail.python.org/mailman/listinfo/tutor > or, via email, send a message with subject or body 'help' to > tutor-requ...@python.org > > You can reach the person managing the list at > tutor-ow...@python.org > > When replying, please edit your Subject line so it is more specific > than "Re: Contents of Tutor digest..." > > > Today's Topics: > >1. (no subject) (William) >2. Trouble creating a pygame.font.SysFont, was Re: (no subject) > (Peter Otten) >3. Re: (no subject) (Danny Yoo) >4. Re: (no subject) (Japhy Bartlett) > > > -- > > Message: 1 > Date: Sat, 29 Nov 2014 11:31:57 +0200 > From: William > To: tutor@python.org > Subject: [Tutor] (no subject) > Message-ID: <5479928d.9000...@yahoo.com> > Content-Type: text/plain; charset=utf-8; format=flowed > > Hello there I'm using Python 3.4 running on Ubuntu 14.04LTS > I'm new to programming and also new to linux > I'm trying to learn the pygame library > I was reading : > > > http://programarcadegames.com/index.php?chapter=introduction_to_graphics&lang=en#section_5 > > On the section on how to draw text the writer has the following code > | > font ||=||pygame.font.SysFont(||'Calibri'||, ||25||, ||True||, ||False||)| > |text ||=||font.render(||"My text"||,||True||,BLACK) > ||screen.blit(text, [||250||, ||250||]) > > When I run the code using the IDLE the Python shell gives me the > following error > > Traceback (most recent call last): > File "/home/william/Desktop/Pygame/Python 3X/Drawing_Shapes.py", line > 48, in > font = pygame.font.SysFont('Calibri', 25, True, False) > File "/usr/local/lib/python3.4/dist-packages/pygame/sysfont.py", line > 614, in SysFont > return constructor(fontname, size, set_bold, set_italic) > File "/usr/local/lib/python3.4/dist-packages/pygame/sysfont.py", line > 537, in font_constructor > font = pygame.font.Font(fontpath, size) > pygame.error: font not initialized > > > I think the problem is that the fonts in Ubuntu are probably stored in a > different > location than in Windows OS.And therefore python can not find it using > the SysFont > method > Can anyone give me any suggestion on how to edit the code so that I can > write text > on my graphics? > Thank you. > | > > > -- > > Message: 2 > Date: Sat, 29 Nov 2014 14:31:04 +0100 > From: Peter Otten <__pete...@web.de> > To: tutor@python.org > Subject: [Tutor] Trouble creating a pygame.font.SysFont, was Re: (no > subject) > Message-ID: > Content-Type: text/plain; charset="ISO-8859-1" > > William wrote: > > Hello William; please provide a meaningful subject line so that your > readers > get a hint whether your question is in their area of expertise. > > > > Hello there I'm using Python 3.4 running on Ubuntu 14.04LTS > > I'm new to programming and also new to linux > > I'm trying to learn the pygame library > > I was reading : > > > > > > http://programarcadegames.com/index.php?chapter=introduction_to_graphics&lang=en#section_5 > > > > On the section on how to draw text the writer has the following code > > | > > font ||=||pygame.font.SysFont(||'Calibri'||, ||25||, ||True||, > ||False||)| > > |text ||=||font.render(||"My text"||,||True||,BLACK) > > ||screen.blit(text, [||250||, ||250||]) > > That is very hard to read. If you have to post Python code please do it in > plain text without those funny "|". Thank you. > > > When I run the code using the IDLE the Python shell gives me the > > following error > > > > Traceback (most recent call last): > > File "/home/william/Desktop/Pygame/Python 3X/Drawing_Shapes.py", line > > 48, in > > font = pygame.font.SysFont('Calibri', 25, True, False) > > File "/usr/local/lib/python3.4/dist-packages/pygame/sysfont.py", line > > 614, in SysFont > > return constructor(fontname, size, set_bold, set_italic) > > File "/usr/local/lib/python3.4/dist-packages/pygame/sysfont.py", line > > 537, in font_constructor > > font = pygame.font.Font(fontpath, size) > > pygame.error: font not initialized > > > > > > I think the problem is that the fonts in Ubuntu are probably stored in a > > different > > location than in Windows OS.And therefore python can not find it using > > the SysFont > > method > > Can anyone give me any suggestion on how to edit the co