Do you think we just do your homework for you? Do what you can and show us some effort and ask specific questions. Effort in, effort out.
Do you think we just do your homework for you? Do what you can and show us some effort and ask specific questions. Effort in, effort out.
Well to be honest the easiest way for me is to use a class, I love them:
import random
class Game:
def __init__(self,name):
self.name=name
print ("Hello,",self.name,"welcome to Blackjack")
print("Your final total must be between 18 and 21 to win")
self.score=0
self.draw()
def draw(self):
self.card=random.randint(1,10)
print('Drew a', self.card)
self.score+=self.card
if self.score<18:
self.choose()
elif self.score < 21:
endwin(self.score)
else:
bust()
def choose(self):
print('Your current score is', self.score)
self.choice=raw_input('Select H to hit or Q to quit: ')
self.choice=self.choice.capitalize()
if 'H' in self.choice:
self.draw()
else:
quitlose()
def endwin(score):
print('Score:',score,' you win!')
def bust():
print('You busted, you lose')
def quitlose():
print('You quit before reaching 18, you lose!')
name=raw_input('Enter your name: ')
hand=Game(name)
it's a bit ugly and rudimentry, but you can begin to see what you want from it.
You are not appending the list anywhere.
Computer see's list as nothing
See's someone needs to input something
See's something being printed
See's that for every interation it will print a list of nothing
And then see's more user input
and likewise:
>>>r=[]
>>> r.extend((input('Three Things: ').split()))
Three Things: my uncle bob
>>> r
['my', 'uncle', 'bob']
>>>
which could be done without the initial empty list.
pardon me woooee I was referring to a Frame, a toplevel window, I focus_set() for clarity I'll show you a bit of an example, which works on Windows, "I'll give takefocus a shot tomorrow, too tired right now
self.upanim=Button(self, text='Upload an image',command=self.con_image)
self.upanim.grid(row=1,column=4)
self.submit=Button(self,text='Add',command=self.addme)
self.submit.bind('<Return>',self.addmejunction)
self.submit.grid(row=12,column=2,columnspan=3)
def con_image(self):
self.file1=askopenfilename(filetypes=[('GIF FILES','*.gif'),('JPEG FILES','*.jpg')])
try:
self.new_image=ImageTk.PhotoImage(file=self.file1)
self.picture['image']=self.new_image
self.picture.grid(row=1,column=2)
self.submit.focus_set()
Please not that that is not ALL of con_image nor is it all of the Frame, just wanted to show relevant parts.
okay, now a couple more issue have been realized, does focus_set() not work on ubuntu because the window doesn't go to the front nor does the focus get set on what I assign it to, also, SMTP doesn't seem to work nor does webbrowser, this is all pretty odd to me...
Of course, this ends after the first two draws. The total is a randint (1,10). I guess I don't actually understand what str(sum(total)) does.
Alright, you're putting in some effort so I'll help you step-by-step as long as you continue the effort.
First I'll start with that last part str(sum(total))
hopefully you know str=string
sum is just that, the sum of what is in the following parenthesis "in this case it's a list with numbers which you have labeled total"
now let's brainstorm what you could do to make it go until the user wins or busts... in other words we want the program to run While the player's score is less than 18 and if the score is > 18 then we need to check if the score is over 21 in which case the player would lose, else the player wins.
Show us the code you have so far, you need to show effort around here.
Matzek, so you know people are much less likely to respond to an old thread, it's advised to start your own.
well I'm not so sure that regex would be necessary for this if it's just a couple words.
def find_these_words(text):
if 'I' in text:
print('found I')
if 'love' in text:
print('found love')
>>> t=input('Text>>> ')
Text>>> Ilovepython
>>> find_these_words(t)
found I
found love
>>>t=input('Text>>> ')
Text>>>I love daniweb
>>>find_these_words(t)
found I
found love
>>>
I'm having an issue getting python 3.2 to recognize files in ubuntu, it's the first time I've tried to code while running it. A specific example of the issue is when setting an .ico to be a bitmap logo for a Frame in tkinter it's claiming that there it's not defined...
Traceback (most recent call last):
File "/home/joshua/Python/Python-3.2.2/gpytact17.py", line 1617, in <module>
[B] start.iconbitmap('pycontlog.ico')[/B]
File "/usr/lib/python3.1/tkinter/__init__.py", line 1514, in wm_iconbitmap
return self.tk.call('wm', 'iconbitmap', self._w, bitmap)
_tkinter.TclError: bitmap "pycontlog.ico" not defined
it's very odd to me, it doesn't actually seem like the error is just a matter of an issue finding the file "which is in the same folder by the way", but rather an issue of it claiming that a STRING is not defined. This is not an issue on Windows or Mac, any thoughts?
Just in case there's any confusion yes I bolded the part of the Error that showed my code, it's scary that I have to say that, but I've seen some awful questions around the net about stuff like that lately... Don't worry my Daniweb family, I haven't lost hope in you yet ;)
For all searching for this I found a GREAT source for Python 3 PIL with easy installation and confirmed operational http://www.lfd.uci.edu/~gohlke/pythonlibs/#pil
Test notes:
running on windows vista
running in tkinter on python 3.2.2
jpeg support confirmed using ImageTk.PhotoImage
I don't know about receiving, but I expect the service could perform that function as well.
Using SMTP you could have use the same email address and receive email in a program, you could do something like checking the sender and only showing or notifying you if the sender == the recipient of your sent message.
this is what i am trying to do
1)create a java program for a computer that can send and receive message (Instant messaging)2)create a java program for a mobile phone (Instant messaging)
3)connect this two application together so both of can receive message from one another..
that's the basic idea.... connection should be TCP/IP and a server would handle the sending of messages to and from one another.....
is it possible to achieve the above? any suggestions and guidance?
Well as my name implies I'm not a java guy BUT just to throw an idea out there for you, I'll show you how I send SMS messages from one of my programs in python simply using the SMTP module and Gmail
def send(body):
two=self.recipient.get()
frum=self.username.get()
pazz=self.pword.get()
def sendit(body, to = two, username = frum, password = pazz):
print('Verifying Login')
mail_server = smtplib.SMTP("smtp.gmail.com", 587)
mail_server.ehlo()
mail_server.starttls()
mail_server.ehlo()
mail_server.login(username, password)
mail_server.sendmail(username, to, body)
mail_server.close()
try:
sendit(body)
logend()
self.mail()
print('Login Successful')
except:
self.error_login()
def sender():
MSG='''Message From User Here'''
send(MSG)
which was originally based on a code TechB posted as a response in http://www.daniweb.com/software-development/python/code/380881
Early this morning, and I haven't a clue how this slipped by me, I learned about the Android SDK and Python for Android, I did a little research, it's very intriguing. Has anybody worked with this at all? Do most built in libraries and modules work for it? How do GUI's work with it "I don't suppose tkinter is somehow working on there..." I apologize for my barage but I feel...giddy about the idea of this :)
Oh I hadn't used the partial tool yet, so it presets the arg without immediately running it?
your command for that button is going to cause issues, I've noticed whenever you have command=method_or_function(parameter) it immediately attempts to run that command, I've side stepped this by creating methods that know to grab that parameter.I'm not completely sure about the rest of the issue, I'd need to see more of the code. Here's an example of what I was talking about though:
self.searchlbl=Label(self,text='Search:')
self.searchlbl.grid(row=2,column=0)
self.search=ttk.Combobox(self)
self.search['values']=contact_list
self.search.bind('<Return>',self.eventsearch_for)
self.search.focus_set()
self.submit=ttk.Button(self,text='Search',command=self.search_for)
self.submit.bind('<Return>',self.eventsearch_for)
self.submit.grid(row=2,column=5)
def search_for(self):
self.search1=self.search.get()
self.search.delete(0,END)
query_key=self.search1.title()
if query_key in ab.keys():
pop_up(query_key)
else:
fail('dict',query_key)
here I'll give you an example of how you should show us your pseudo code, let's say I'm trying to count the number of words in a list that have the letter b in them:
I need a parameter or input
I need a count
I need the count to go up by one every time a word has a b in it
I need to print the final total of the count
with that I came up with:
>>> blist=['bob','the', 'builder']
>>>
>>> def b_count(alist):
count=0
for word in alist:
if 'b' in word:
count+=1
print(count)
>>> b_count(blist)
2
please note that it is better practice to use return than print
A question on the topic of background colors, is there a way to make an entire Frame's background white, including unassigned space? And, without getting those nasty grey areas around labels and windows?
DON'T HIJACK OLD THREADS, Make your own! and 1 is a key.
what's your pseudocode algorithm? I just want to better understand EXACTLY what you're trying to do.
here's an interesting article on the subject "I didn't even know about CubicWeb before it" http://www.itworld.com/software/192113/pillars-python-six-python-web-frameworks-compared
Great, glad to help. Upvote as you see fit and mark the thread solved.
I believe the google standard is four spaces, I use one tab. It's VERY important, whitespace realllly matters in python, I suggest the one tab method, it's quicker and cleaner in my opinion.
Here was my output:
>>>
1
2
3
4
5
6
7
8
9
10
>>>
Is your indentation correct?
a=0
while a<10:
a+=1
print(a)
Here look at this and look at the code, in fact read what the code is doing step-by-step and look at the output to see if that is what it's doing. Then you'll find what exactly your problem is and we can help you better, I changed the list to make it easier on you.
from string import *
d = dict()
l = []
for i in range(ord('a'),ord('z')+1):
d[i] = chr(i)
def rotate_word(word,n):
global l
for i in range(0,len(word)):
k = ord(word[i]) + n
if k > ord('z'):
k %= ord('z')
elif k < ord('a'):
k = k % ord('a') + ord('z') - ord('a') + 1
l.append(chr(k))
return "".join(l)
wordlist = []
c_wordlist = []
r_wordlist = []
fin = ['my','friend']
for word in fin:
word = word.strip()
wordlist.append(word)
c_wordlist.append(word)
wordlist.sort()
c_wordlist.sort()
for i in wordlist:
print(i)
r_wordlist.append(rotate_word(i,7))
print(r_wordlist[1])
for word in wordlist:
for i in r_wordlist:
print(rotate_word(i,7))
if i == rotate_word(word,7):
print( i," ",word,'\n')
output:
friend
my
myplukt
myplukttwsr
myplukttwsrmypluktwsr'
myplukttwsrmypluktwsr'mypluktwsr
myplukttwsrmypluktwsr'mypluktwsrttwsr'
show us the error
No problem. Please mark this thread as solved and upvote posts if and as you see fit.
Yes, through practice, make yourself a challenging project.
show us what you have so far.
come on now, obviously you have to assign the variable to a value...
>>> s=10
>>>
>>> int(s)
10
>>> print(s)
10
>>> s+3
13
but that only answers part of it, first you have to assign s to a string value, HINT: this is done by surrounding the value with '' or "". note that above s was associated with an integer value to begin with, not a string value. Also, you should post what you come up with as a courtesy to show that you put in effort, don't forget to mark threads solved either.
Something like that, just work on perfecting what you know now.
First,learn how to implement the basics you're learning into something useful, while you are doing that you may find that you want to add more features, those features may require you to learn more advanced things. For example, recently I learned how to utilize SMTP to send E-mails from a program. As you do this you will learn more advanced skills and gain knowledge of third-party modules as well as built-in modules you haven't yet had to use.
While I don't quite understand the language in that program, I do understand what your program is doing. So it looks like you've just started using classes. How about creating a program utilizing a GUI, I suggest tkinter for its simplicity.
Post a program you've done so we can gauge your skill level.
Well, with split, just as the name implies you could split the string,into a list, in this case at the semicolon
>>>a='some;data'
>>>a
'some;data'
>>>a=a.split(';')
>>>a
['some','data']
HINT: % is the shows the remainder so:
>>>2%2
0
>>>9%2
1
so if a number IN THE RANGE OF 1 AND THE GIVEN NUMBER!!!! had a remainder of 1, would it be a divisor? How about if it had a remainder of 0? Now if we wanted to just find out the amount of divisors it had how would we do that? HINT#2: This is likely to be the LENgth of something.
I feel like it's more a matter of it being explicit copying than it being implicit pointers, review the zen of python. Don't forget to mark the thread solved etc. Happy we could help.
here's a hint:
users={'op':'doesntusegoogle',
'daniweb':'toohelpful'}
username=raw_input('Username: ')
while username not in users.keys():
print('\nInvalid username. Please try again\n')
username=raw_input('Username: ')
password=raw_input('Password: ')
while password!=users[username]:
print('\nInvalid password. Please try again\n')
password=raw_input('Password: ')
print('Welcome,'username)
Not really reverse - lowest to highest makes most sense,
1,2,3,4,5
instead of
5,4,3,2,1
is the way most people count ;)However, if you do want it highest from lowest, then simply past a keyword argument "reverse=True", which will work for both sort and sorted.
lol I knew how to count and about the reverse option ;) I was thinking of it more as prioritizing, but the count makes sense. Thanks E.
Because you've appended a pointer to that list, so when b changes the pointer sees the change. However if you make a COPY it has its own list. Consider the following interactive experiment:
>>> f=['bob','jack']
>>> d=['bill']
>>> d.append(f)
>>> d
['bill', ['bob', 'jack']]
>>> f.pop()
'jack'
>>> f
['bob']
>>> d
['bill', ['bob']]
>>> f.append('jack')
>>> f
['bob', 'jack']
>>> g=f[:]
>>> g
['bob', 'jack']
>>> f.pop()
'jack'
>>> f
['bob']
>>> g
['bob', 'jack']
and of course:
>>> d.append(g)
>>> d
['bill', ['bob', 'jack']]
>>> f
['bob']
>>> f.append('cow')
>>> f
['bob', 'cow']
>>> g
['bob', 'jack']
>>> d
['bill', ['bob', 'jack']]
Well if you're sure that you're going to be working with only integers then you could convert the elements in the set to integers 'because they are strings when inputed by the user thus the ' '. I did something like:
def print_set(some_set):
fixed_set=set()
for element in some_set:
try:
element=int(element)
fixed_set.add(element)
except ValueError:
print('\n',element,'was not an integer and will not be includeded in the final set\n')
some_set=fixed_set
return(some_set)
Nope, looks to me like it's just straight up printing the set as-is
Well
>>> stored_set= input("Enter the elements of the set(separated with spaces): ").split()
Enter the elements of the set(separated with spaces): my name is joshua
>>> stored_set=set(stored_set)
>>> stored_set
{'joshua', 'is', 'my', 'name'}
>>> len(stored_set)
4
>>>
>>> stored_set= input("Enter the elements of the set(separated with spaces): ").split()
Enter the elements of the set(separated with spaces): my name is joshua joshua
>>> stored_set=set(stored_set)
>>> stored_set
{'joshua', 'is', 'my', 'name'}
>>> len(stored_set)
4
is one option, but the EVEN MORE intuitive way is:
>>> stored_set= set(input("Enter the elements of the set(separated with spaces): ").split())
Enter the elements of the set(separated with spaces): joshua joshua bob
>>> stored_set
{'joshua', 'bob'}
just remember, Python is VERY much like English, say what you're trying to do out loud and there's a good chance you're very close like in this case "I need the input to be in a set" and essentially it came out to set(input()) which looks alot to me like input, in a set lol ;) You're in good hands here at Daniweb buddy, just keep puttin' in effort.
the set will automatically remove items as they go in if they are pre-existing. Look closely at my last post.
and to remind you again that you don't need to stick the set in a list I bring you another presentation by the pyguy62 IDLE theater company:
>>> this_set
{'john', 'silver', 'long'}
>>>
>>>
>>> a
['long', 'john', 'silver']
>>>
>>>
>>> a.append('bob')
>>>
>>> for word in a:
this_set.add(word)
>>> this_set
{'bob', 'john', 'silver', 'long'}
I grant you a pass my fellow noob, here ya go, seeing is believing:
>>> this_set={'long','john','silver'}
>>>
>>> this_set
{'john', 'silver', 'long'}
>>>
>>>len(this_set)
3
>>>
>>> type(this_set)
<class 'set'>
well, from what I'm seeing it looks like it'll come up surrounded by brackets then curly braces.
[{'my','set'}]
because you made the set a list of the set, and I'm asking myself why you found that necessary... also, work on your names i is not a good name, I looked at that and was like; oh, are we playing with integers today? And then was terribly disappointed to find out otherwise :'(, lesson of the day, don't make me sad, and practice good practices.
pardon me, I mis-phrased the question. I meant, why is it sorted in reverse.
The method sort() sorts the list in place and returns None
>>> list1 = ['Herring','used','to','be','abundant','in','the','AtlAntic','Ocean','then','herring','got','overfished'] >>> list1.sort(key=lambda s : s.count('a')) >>> print list1 ['Herring', 'used', 'to', 'be', 'in', 'the', 'AtlAntic', 'then', 'herring', 'got', 'overfished', 'Ocean', 'abundant']
The function sorted() creates a new sorted list and does not modify the initial list
>>> list1 = ['Herring','used','to','be','abundant','in','the','AtlAntic','Ocean','then','herring','got','overfished'] >>> print sorted(list1, key=lambda s : s.count('a')) ['Herring', 'used', 'to', 'be', 'in', 'the', 'AtlAntic', 'then', 'herring', 'got', 'overfished', 'Ocean', 'abundant'] >>> print list1 ['Herring', 'used', 'to', 'be', 'abundant', 'in', 'the', 'AtlAntic', 'Ocean', 'then', 'herring', 'got', 'overfished']
is sorted(list1, key=lambda s : s.count('a')) sorting the list by the amount of lower case a's in each string?
I'm not sure but I'm guessing that something like that would differ for each device, and of course there's the possibility that it may have a domain-specific like language connected to it. I'm intrigued by the idea though. Not to digress but on a semi-related topic I recently came across this http://pyrorobotics.org/