4,305 Posted Topics
Re: You want to keep your card symbols simple and unique to be able to evaluate the hand ... [code]suit_str = "CDHS" rank_str = "23456789TJQKA" deck = [rank+suit for suit in suit_str for rank in rank_str] print(deck) """my output (made pretty) --> [ '2C', '3C', '4C', '5C', '6C', '7C', '8C', '9C', … | |
Re: A time based random number generator will only work if called once, or uses the random time between a user input event. The function time.time() only updates 18.2 times per second, too slow if you request several random numbers quickly. | |
Re: Generally module md5 is used to encrypt strings/texts like passwords by making a unique check sum of the string. Once encrypted you cannot decrypt the resulting check sum back to the original string. The only way you can find out its contents is to encrypt another string and compare the … | |
Re: We wouldn't have this problem if Windows machines would ship with Python installed. The real problem is that MS wants to make money with their old VB interpreter. | |
Re: Similar to Gribouillis' code, but uses lambda as a helper function ... [code=python]# sort a more complex combination of lists/tuples: mylist = [ (1, ['a', '3.1', 'ad']), (2, ['b', '4.0', 'bd']), (3, ['c', '2.5', 'cd']), ] # sort by item at index [1][1] of each tuple # using a helper … | |
Re: This might be a little easier to understand for a beginner (thanks to pythopian) ... [code]# parse text data into a dictionary using # split at newline and split at space def parse2dict(text): data_dict = {} for line in text.split('\n'): if line: key, val = line.split() data_dict.setdefault(key, []).append(val) return data_dict … | |
Re: This might be easier to understand ... [code]if player[0] not in 'rps': print "incorrect choice entered" [/code] | |
Re: As you most likely found out, there are a number of ways to do this. You picked a good one. There was also a snippet at: [url]http://www.daniweb.com/code/snippet237089.html[/url] | |
A function that takes a string and returns the number of words in the string. An example of using pointers in Pascal. | |
Re: [QUOTE=CurtisEClark;1042345][CODE]import random import pygame w = 640 h = 480 n = random.randrange(6) x = open("shapes.txt") z = x.readlines()[n] screen = pygame.display.set_mode((w, h)) if z == "circle": pygame.draw.circle(screen, (random.randrange(255), random.randrange(255), random.randrange(255)), (random.randrange(640), random.randrange(480)), 100 ) elif z == "line": pygame.draw.line(screen, (random.randrange(255), random.randrange(255), random.randrange(255)), (random.randrange(640), random.randrange(480)), 100 ) elif z == … | |
Re: You can simply use this little test ... [code]int_rounds = 3 win = 0 loss = 0 print(int_rounds > max(win, loss)) # True win = 0 loss = 0 print(int_rounds > win or int_rounds > loss) # True win = 0 loss = 0 print(int_rounds > win and int_rounds > … | |
Re: A Python dictionary is an efficient way to store data and has very fast lookup speeds ... [code]# in Python you could use a dictionary key:val pair # where the key is the integer index and val is the float # if you just want to store values at an … | |
Re: I usually whip up a little test program. Here you use the length of your columns list to avoid problems ... [code]# possible test data data = """\ a,z,1 b,y c,x,1 d,w,1 e,v f,u""" fname = "something.txt" # write test data file ... fout = open(fname, "w") fout.write(data) fout.close() def … | |
Re: Actually, if you temporarily flip to [B]Reply[/B] with quote in the OP post you find out the request was for: [code] H e l l o W o r l d [/code] That is 2 spaces between 'o' and 'W', DaniWeb swallowed up the extra space since it was not … | |
Re: How would you define AI? Some folks say that ELIZA is an example of AI: [url]http://www.manifestation.com/neurotoys/eliza.php3[/url] | |
Re: Use numpy shape ... [code]import numpy as np # create a 3 by 5 array of integer zeroes z = np.zeros([3, 5], int) print( z ) """ [[0 0 0 0 0] [0 0 0 0 0] [0 0 0 0 0]] """ # check the shape print( "shape --> … | |
Re: You can pull a few fixed things out of the drawEye() function and use parameters, but radius makes no sense since it changes within the function ... [code]from graphics import * def drawCircle(win, centre, radius, colour): circle = Circle(centre, radius) circle.setFill(colour) circle.setWidth(2) circle.draw(win) def drawEye(win, centre): drawCircle(win, centre, 40, "white") … | |
Re: Why don't you simply separate your class Stack out and send it through some simple tests to figure out the errors? Looks like pop() should return something. | |
Re: This all has been solved in thread: [url]http://www.daniweb.com/forums/thread237012.html[/url] | |
Python can use its COM support and the OCX of the WindowsMediaPlayer to play mp3 music files. Make sure that your Python installation has the win32com folder. Check [url]http://starship.python.net/crew/mhammond/win32/Downloads.html[/url] for the latest win32com support. [COLOR="Red"]This code relies on file WMPlayer.OCX which MS has removed on newer versions of Windows. See … | |
Re: Or you can do it this way ... [code=python]# these are the accepted characters in the input string accepted = '0123456789dr' while True: mystr = raw_input("Enter data (allowed are integers and 'dr'): ") if all(x in accepted for x in mystr): break print("unaccepted characters in input, try again") print(mystr) # … | |
Re: [QUOTE=pyprog;1040831]Assume I have a file of the following format: a,1 b,2 c,3 d,4 Here is my code: [CODE] def junk(f): d1 = {} d2 = {} for line in f: columns = line.split(":") letters = columns[1] numbers = columns[2] d1[letters] = numbers d2[numbers] = letters return (d1, d2) def something(): … | |
Re: When you post a question like that, give as much pertinent information as you can. You needed to tell us right from the start that you are using the Zelle module graphics. Also, read the code that folks respond with and check it against the errors in your code. I … | |
Re: Compared to all the unused brain space, this is nothing! | |
Re: For a wx.TextCtrl() instance I simply use the WriteText() method. | |
Re: [QUOTE=nevets04;1038393]Solved: [code] List = [] for x in xrange(5): List.append(raw_input("")) [/code][/QUOTE] Wow, I would have never guessed that from your original question. Hint, variable names starting with a capital letter are by convention used for class names. | |
Re: [QUOTE=nevets04;1039439]I know Remembering secure passwords is difficult. So I made this. [code] import random, os a= ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','1','2','3','4','5','6','7','8','9','{',':','>','?','}','|','!','@','#','$','%','^','&','*','(',')','-','+'] z = int(raw_input("How many words in your password?: ")) os.system("clear") List = [] for x in xrange(z): List.append(random.choice(a)) print "".join(List) print "1) Yes" print "2) No" b = int(raw_input("Would you like to save … | |
Re: You should really start a new thread of your own! The string function find() can find a subtext within a larger text, as will re.findall(). The advantage of find() is that it will give you the location in the form of an index. | |
Re: I hope you have luck finding someone who actually uses pygtk. I run on a Windows platform and pygtk is a bear to install. | |
Re: [QUOTE=Mathhax0r;1039559]The first two only require one liners of logic, so here: [CODE]n = int(raw_input("How many squares? ")) squares = [x*x for x in range(1,n+1)] print square[/CODE] [CODE]def even_only(argList): return filter(lambda m: m%2==0, argList)[/CODE] As for the pig latin, you'll need to break the sentence down into words with split, do … | |
Re: For programs like this, recursion is not a very good idea. Calling a function, even itself, is one of the slower things in programming because of the stack control. You will find that in the faster prime algorithms repeated function calls are absent. Here is an example of one of … | |
Re: Take a looky at something like this ... [code]data = """\ ------------------------------------ ----------- --------- ------------------------------------ ----------------------------- ------- PRODUCT KIT TYPE STATE MAINTENANCE REFERENCED BY ------------------------------------ ----------- --------- ------------------------------------ ----------------------------- ------- HP I64VMS ACUXE V6.40-11P09A Full LP Installed HP I64VMS AVAIL_MAN_BASE V8.3-1H1 Full LP Installed HP I64VMS OPENVMS V8.3-1H1 HP I64VMS … | |
Re: If you read a binary as a text file (mode='r'), you have to be aware that the binary data can inadvertently contain an EOF (End Of File) marker early on, at which point the read would stop. Using a binary read (mode='rb') will prevent this. Here is a simple way … | |
Re: I think you should learn Lisp to get familiar with parenthesis. Yes, you are using too many of those in your Python code. You can study up in the precedence of mathematical operations to remove a fair number of them. | |
Re: You can use one of the built-in string functions ... [code]n = 2 digits = 3 ns = str(n).rjust(digits, '0') print(ns) # 002 [/code] | |
Re: One eye has to be positioned center_left, the other center_right ... [code]from graphics import * import math def drawCircle(win, centre, radius, colour): circle = Circle(centre, radius) circle.setFill(colour) circle.setWidth(2) circle.draw(win) def drawTarget(): win = GraphWin() centre_left = Point(50,100) centre_right = Point(150,100) drawCircle(win, centre_left, 40, "white") drawCircle(win, centre_left, 20, "blue") drawCircle(win, centre_left, … | |
Re: This problem really asks for the use of a dictionary object ... [code=python]# typical test data data = """\ 1 20 2 56 3 42 4 53 5 90 6 33 """ fname = 'mytest123.txt' # write test data to a file fout = open(fname, "w") fout.write(data) fout.close() # read … | |
Re: What Gribouillis means is run this code and post the output here on DaniWeb ... [code=python]# show the system path for Python ( PYTHONPATH ) import sys print( sys.path ) [/code]You need to run this with the version of Python you are interested in. Also you cannot permanently set PYTHONPATH … | |
Re: jaison2, [B]range(drawBlock)[/B] in your code does not make any sense, since range() expects an integer value. In your loop you want something like this ... [code]for row in range(height): # multiplier will give width number of '#' characters print '#' * width [/code] | |
Re: I want to encourage posters to give us the version of Python they are using. Otherwise the rest of us have to assume and ask. Python25 for instance allows you to use the print statement or the print() function. So using the fact that print() was used is not necessarily … | |
Re: Remove all the silly [B]os.system("clear")[/B], they are not needed and work on one specific OS only, then it will work ... [code]#Credits: #Nevets04 #Fallen #Uber1337 #Farout #import os List = [] z = int(raw_input("How many words in your list?: ")) #os.system("clear") for x in xrange(z): List.append(raw_input("Enter word: ")) List.sort()#,os.system("clear") print … | |
Re: Take a look at the final lines of this code snippet: [url]http://www.daniweb.com/code/snippet216747.html[/url] | |
Re: So the next question is: Is it you against the computer, or you against another human? | |
| |
Re: Let's hope this is not homework and the OP had a chance to learn something. In a true palindrome test you also need to remove everything but the case adjusted letters. "Madam in Eden I'm Adam" is a palindrome. | |
Re: You can easily explore it yourself ... [code]# exploring Python's file random access text = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' fname = 'test103.txt' # create a test file fout = open(fname, "w") fout.write(text) fout.close() # open the test file for reading fin = open(fname, "r") # file positions are zero based # tell the … | |
Re: In your second problem in function even_list the for loop should iterate through the list you are giving the function. If you find an item in that list that is even, then you have to append it to the list you call lista. If you are trouble shooting, it always … | |
| |
Re: The more common approach would be to use append() ... [code=python]for line in f: columns = line.split(",") letters = columns[ 0 ] list1.append(letters) [/code]I think it is less mistake prone. I also want to point out, that should you want to read the second column (the end of each line), … |
The End.