I feel that you may possibly be misinterpreting the assignment.
JoshuaBurleson 23 Posting Whiz
TrustyTony commented: Common.... terrible code, to be honest. -3
I feel that you may possibly be misinterpreting the assignment.
well if you're saying that you want it to do that prior to the input=='' then you could simply make two while loops changing the first to exclude the file reading, like:
while True:
inp = input('Enter text, or "Enter" to quit: ')
if inp =="":
break
else:
inp+='\n'
oinp = open('open_inputs.txt','a')
oinp.write(inp,)
oinp.close()
while True:
oinp = open('open_inputs.txt','r')
for line in oinp:
print('')#prettier
print(line)
oinp.close()
print('')#I just needed to make it prettier
inp = input('Enter text, or "Enter" to quit: ')
if inp =="":
break
else:
inp+='\n'
oinp = open('open_inputs.txt','a')
oinp.write(inp,)
oinp.close()
Although this is essentially repeating yourself, which is best to avoid.
If you could post the exact assignment that might clear things up, I'm just having 2 issues with this: 1,Posting python to a site isn't really a "beginner" project, 2, Making the output color in IDLE change isn't something that's common practice.
I don't understand why they would want you to do that...http://mail.python.org/pipermail/tutor/2005-August/040327.html
are you suppose to be using a tkinter window to display the output?
Maybe because of the print imp? I'm not sure, this worked fine for me:
while True: oinp = open('open_inputs.txt','r') for line in oinp: print(line) oinp.close() print('')#I just needed to make it prettier inp = input('Enter text, or "Enter" to quit: ') if inp == "": break else: oinp = open('open_inputs.txt','a') oinp.write(inp,) oinp.close()
ugh too late to edit. Anyways I meant to post this.
while True:
oinp = open('open_inputs.txt','r')
for line in oinp:
print('')#prettier
print(line)
oinp.close()
print('')#I just needed to make it prettier
inp = input('Enter text, or "Enter" to quit: ')
if inp =="":
break
else:
inp+='\n'
oinp = open('open_inputs.txt','a')
oinp.write(inp,)
oinp.close()
Maybe because of the print imp? I'm not sure, this worked fine for me:
while True:
oinp = open('open_inputs.txt','r')
for line in oinp:
print(line)
oinp.close()
print('')#I just needed to make it prettier
inp = input('Enter text, or "Enter" to quit: ')
if inp == "":
break
else:
oinp = open('open_inputs.txt','a')
oinp.write(inp,)
oinp.close()
I guess I'm just not quite getting how it would update each time the user enters a character into the Entry field.
oh great, I haven't even heard of the listbox until now, I'll let you know how it works out. Thanks.
it's a GUI 'tkinter' application
I'm trying to figure out how to deal with multiple matches in my address book, say the user has two people by the name of Bob; one is Bob Lastname and the other is Bob Namelast, how would you approach dealing with telling them to specify, and then allowing them to do so and get a result. Also, what if a user just has two contacts named Bob with no last name, what would you do then? I've got a temporary system in place that checks the within a given len of the input against a given len of the keys at hand, but that has several issues as I'm sure you can imagine.
Utilizing this for my pycontacts "address book" project.
I'm getting a strange TypeError when I try to place it in a function:
def send(Uto,UFrom,Usubject,Umessage):
import sys
# Import smtplib for the actual sending function
import smtplib
try:
input = raw_input
except:
pass
# Import the email modules we'll need
from email.mime.text import MIMEText
# Create a text/plain message
msg = MIMEText(Umessage)
msg['Subject'] = UFrom,':',Usubject
msg['From'], msg['To'] = "pythonthread@gmail.com", Uto
# Send the message via gmail SMTP server
s = smtplib.SMTP('smtp.gmail.com', 587)
s.ehlo()
s.starttls()
s.login(msg['From'], 'pythonthread')
try:
# Python 3.2.1
s.send_message(msg)
except AttributeError:
# Python 2.7.2
s.sendmail(msg['From'], [msg['To']], msg.as_string())
s.quit()
send('pythonthread@gmail.com','me','win','I hope')
this is returning:
Traceback (most recent call last):
File "E:\email.py", line 55, in <module>
send('pythonthread@gmail.com','me','win','I hope')
File "E:\email.py", line 47, in send
s.send_message(msg)
File "C:\Python32\lib\smtplib.py", line 803, in send_message
g.flatten(msg_copy, linesep='\r\n')
File "C:\Python32\lib\email\generator.py", line 91, in flatten
self._write(msg)
File "C:\Python32\lib\email\generator.py", line 144, in _write
self._write_headers(msg)
File "C:\Python32\lib\email\generator.py", line 364, in _write_headers
elif _has_surrogates(v):
TypeError: expected string or buffer
Choose some prep up from tasks from beginners sticky thread of Vegaseat, maybe it hurts your pride, but I warmly recommend it. Or you can do math code for projecteuler.net (I am tonyjv there, inactive for some months).
How should I invoke another classes methods without using the instance?
I'm working on those instance referring classes, I'm just not sure how to access methods otherwise, I guess my learning material is poor. Whenever I try to refer to another classes methods without the instance I get an error claiming the Class has no such method.
if you return depth from this function, although it's not a global variable, can it still be used elsewhere in the code? I've never seen the function being called to show the value from it as you did at the end.
also, ouch, my ego :-o
Nice Tony, it works great! Exactly what I needed, and furthermore, thanks for the link so I could better understand the mechanics.
Hi
I thought iI would try to teach myself how to program, so sorry if this has been asked before but I could find it.
What I am trying to do is limit an input to of an int to 50 or less with out the program throwing up an error. This is what i have so far :-#start of input loop
while True:
try:
deep = int(raw_input('Enter Depth in meters :- ',))
break
except ValueError:
print 'Enter whole meters only!(round up if needed!)'
#end of loopI want deep <= 50 and if it not to loop back to the input line.
Thanks
Ali I see you messed up with the code button, just hit it once and
'you\'ll be able to' input('your code here: ')
this will make it easier to read. Hope we were able to help you.
preferred method:
okay=False
depth=0
def dcheck():
global depth
global okay
while not okay:
try:
depth=int(input('Enter depth in range(0,50): '))
if depth in range(0,51):
print(depth)
okay=True
return okay
else:
print('Invalid Depth')
okay=False
except:
print('Invalid input')
dcheck()
okay, I understand, I've just yet to use the yield function. Is it essentially the same as return? Also, how would it be possible to be done otherwise?
@Gribouillis how does it give more than one attempt after the value error was raised with yours? I'm just curious, I've never seen it that way before.
I got it, wow, that worked well from you. Let's see if I can do that. How would I find my ISP's SMTP server info?
my code only allows the user to mess up by entering an incorrect value once, how would you make it more?
also, I would use input instead of raw_input()
I would do something like:
depth=int(input('Depth: '))
def depther():
global depth
try:
if depth <=50 and depth>0:
print(depth)
else:
while depth>50 or depth<0:
print('Incorrect range')
depth=int(input('Depth: '))
print(depth)
except:
print('Invalid input')
depth=int(input('Depth: '))
if depth <=50 and depth>0:
print(depth)
else:
while depth>50 or depth<0:
print('Incorrect range')
depth=int(input('Depth: '))
print(depth)
I defined depth test as a function so it can be reused. Also I used except to cover any error, you may not wish to do this, it's just my preference in cases like this. Now that you know this how do you suppose you would prevent the user from entering a negative depth?
I've tried that a few times and gotten socket errors for local host. Also does the From: field need to be a valid email address?
found it, I had client.title#and geometry#=() but it's just client.title()
I suppose now on to having a better way to do email, is there one?
I'm having issues with a third level Toplevel in tkinter, that is to say having a toplevel popup from another Toplevel.
self.emailbttn= Button(self, text='Email', command=self.clientchooser)
def clientchooser(self):
showing.withdraw()
client_choose()
def client_choose():
global client
client=Toplevel()
client.title=('Choose Email')
client.geometry=('800x600')
Client(client)
class Client(Frame):
'''Choose Email Client'''
def __init__(self,master):
super(Client, self).__init__(master)
self.grid()
self.create_widget()
def create_widget(self):
self.notice=Label(self, text='Choose your Email Type')
self.notice.grid(row=1,rowspan=2,column=3,columnspan=4)
self.gmail=Button(self, text='GMAIL', command=self.gopen)
self.gmail.grid(row=3,column=3,columnspan=3)
def gopen(self):
import webbrowser
webbrowser.open('https://mail.google.com/mail/u/0/?hl=en&tab=wm#compose)
And yes I'm aware that Gmail is more properly webmail as opposed to a mail client. On a related note is there a way to have the To: field automatically filled upon opening a message composition page in gmail? I'm sure there's a much better way to do email than the way I'm doing it, but I suppose the real issue at hand for me is the fact that the third level toplevel is opening WAY too small and the title doesn't show.
update: Firefox will only open if the parameters include www. or http:// otherwise it will open with IE...
Okay, now firefox is being used as the default browser for it, I don't know why there was a delay. But still nothing is working for webbrowser.get('whateverbrowser')
however trying the get() method in interactive mode also raises an error:
>>> webbrowser.get('mozilla')#same with 'firefox'
Traceback (most recent call last):
File "<pyshell#12>", line 1, in <module>
webbrowser.get('mozilla')
File "C:\Python32\lib\webbrowser.py", line 53, in get
raise Error("could not locate runnable browser")
webbrowser.Error: could not locate runnable browser
I'm pretty sure. I've also tried the path to firefox, which didn't work. This wouldn't be such an annoying issue if it didn't work in interactive mode either, but it does webbrowser.open('www.google.com') opens an instance of firefox, so why wouldn't it work in the program?
also, I've tried
webbrowser.get('firefox')
and got an error
After marking gofish up to a temporary loss I've moved back to my address book. The issue I'm currently having is that webbrowser is opening the wrong broswer. I've set my default browser to firefox (currently using windows vista), and yet the program is still opening IE, however when I tried webbrowser.open('whatever') it opened with firefox. Below is the relevant code, any ideas?
self.viewurlbttn= Button(self, text='Open URL', command=self.urlopen)
self.viewurlbttn.grid(row=2,column=1,columnspan=3)
def urlopen(self):
import webbrowser
webbrowser.open(Interface.url)
p.s. I'm using tkinter
oh, and the velocity of the shark, if you didn't want it bouncing all around the screen.
what's the specific problem with the code? Also have you tried livewires? It's a module for pygame and I prefer the simplicity. My first "game" I wrote in it is below, the only thing that would need to be modified would be collision detection on the sprites, and the event grab if you didn't want to use the mouse as I did.
from livewires import games
class Snake(games.Sprite):
def update(self):
if self.right>games.screen.width or self.left<0:
self.dx= -self.dx
if self.bottom>games.screen.height or self.top<0:
self.dy=-self.dy
class Mouse_move(games.Sprite):
def update(self):
self.x=games.mouse.x
self.y=games.mouse.y
games.init(screen_width=640, screen_height=480, fps=50)
image=games.load_image('lolpy.png', transparent=False)
games.screen.background=image
snake= games.load_image('pysnake.jpg', transparent=True)
snake= Snake(image=snake, x=320, y=240, dx=-1, dy=1)
g= games.load_image('g.jpg')
g= Mouse_move(image=g, x=games.mouse.x, y=games.mouse.y)
games.screen.add(snake)
games.screen.add(g)
games.mouse.is_visible=False
games.screen.event_grab=False
games.screen.mainloop()
:-/ I can't believe I'm still having issues like this.
do you think this could work for deal?
def deal(self, count=5):
hands= player, ai
for unit in count:
for hand in hands:
for card in self.cards:
if card not in hand:
hand.cards.append[card]
self.deck.remove(card)
self.len-=1
hand.len+=1
else:
for crd in hand.cards:
if crd==card:
hand.cards.remove(crd)
hand.score+=1
self.deck.remove(card)
why is self.deck=iter(self.deck) necessary?
a few of the print()'s are just me making sure everything is running okay. Also the hands should start out as a list of 5 cards
fixed infinite loop @ line 42. Rules are simple: ask ai for card in your hand, if they have it the cards are tossed and your score goes up by 1 if not then you draw a card, if that's the card you need the cards are tossed and score goes up by 1, then the ai does the same and it goes back and forth until player or ai is out of cards at which point the scores are tallied.
updated a bit, however there's still something wrong with ai.cards. On it's first round it claims ai.cards[0] index is out of range?
class Hand(object):
def __init__(self,cards):
self.cards=[cards]
self.score=0
self.len=len(self.cards)
def check_match(self,card0):
if card0 in self.cards:
for card in self.cards:
if card==card0:
self.cards.remove(card)
self.score+=1
else:
self.cards.append(card0)
def decide(self,other_hand):
print(self.len)
if self.len<1:
print('AI',ai.score,'Player',player.score)
else:
print('\nYour Hand:',self.cards,'\n')
choice=input('Which card would you like to ask your opponent for?: ')
if choice not in self.cards:
while choice not in self.cards:
print('\nNot a valid choice\n')
choice=input('Which card would you like to ask your opponent for?: ')
elif choice in self.cards:
other_hand.give(self, choice)
other_hand.decide(other_hand)
def give(self,other_hand,card1):
if card1 in self.cards:
for card in self.cards:
if card==card1:
other_hand.check_match(card)
#self.cards.remove(card)
else:
print('Go Fish')
other_hand.draw(self)
self.decide(ai)
def draw(self, other_hand):
deck.draw_From(self)
other_hand.decide(ai)
class AI(Hand):
def __str__(self):
print('AI HAND')
def decide(self,other_hand):
import random
random.shuffle(self.cards)
choice=self.cards[0]
other_hand.give(self,choice)
def give(self,other_hand,card1):
if card1 in self.cards:
for card in self.cards:
if card==card1:
other_hand.check_match(card)
## self.cards.remove(card)
else:
print('Go Fish')
other_hand.draw(self)
self.decide(player)
class Deck(object):
def __init__(self, cards):
self.cards=[cards]
self.len=len(self.cards)
def draw_From(self, other_hand):
draw_card=self.cards[0]
other_hand.check_match(draw_card)
self.cards.pop(0)
#main
cards=['2','2','2','2','3','3','3','3','4','4','4','4','5','5','5','5','6','6','6','6','7','7','7','7','7','8','8','8','8','9','9','9','9','J','J','J','J','Q','Q','Q','Q','K','K','K','K','A','A','A','A']
import random
random.shuffle(cards)
deck=Deck(cards[0])
cards.pop(0)
for card in cards:
deck.cards.append(card)
cards.remove(card)
print(deck.cards)
h1=deck.cards[1:7]
player=Hand(deck.cards[0])
deck.cards.pop(0)
print(player.score)
for card in h1:
deck.cards.remove(card)
print(deck.cards)
for card in h1:
player.check_match(card)
ai=AI(deck.cards[0])
deck.cards.pop(0)
h2=deck.cards[0:7]
for card in h2:
ai.check_match(card)
deck.cards.remove(card)
while len(player.cards)>0 and len(ai.cards)>0:
player.decide(ai)
It seems the error is actually in here:
class Hand(object):
def __init__(self,cards):
self.cards=[cards]
self.score=0
self.len=len(self.cards)
def check_match(self,card0):
if card0 in self.cards:
for card in self.cards:
if card==card0:
self.cards.remove(card)
self.score+=1
else:
self.cards.append(card0)
def decide(self,other_hand):
print('\nYour Hand:',self.cards,'\n')
choice=input('Which card would you like to ask your opponent for?: ')
if choice not in self.cards:
print('Oh shit')
while choice not in self.cards:
print('\nNot a valid choice\n')
choice=input('Which card would you like to ask your opponent for?: ')
elif choice in self.cards:
other_hand.give(self, choice)
def give(self,other_hand,card1):
if card1 in self.cards:
for card in self.cards:
if card==card1:
other_hand.check_match(card)
try:
self.cards.remove(card)
except:
pass
else:
print('Go Fish')
self.decide(self)
def draw(self, other_hand):
deck.draw_From(self)
other_hand.decide(self)###
class AI(Hand):
def __str__(self):
print('AI HAND')
def decide(self,other_hand):
import random
random.shuffle(self.cards)
choice=self.cards[0]
other_hand.give(self,choice)
class Deck(object):
def __init__(self, cards):
self.cards=[cards]
self.len=len(self.cards)
def draw_From(self, other_hand):
draw_card=self.cards[0]
other_hand.check_match(draw_card)
self.cards.pop(0)
#main
cards=['2','2','2','2','3','3','3','3','4','4','4','4','5','5','5','5','6','6','6','6','7','7','7','7','7','8','8','8','8','9','9','9','9','J','J','J','J','Q','Q','Q','Q','K','K','K','K','A','A','A','A']
import random
random.shuffle(cards)
deck=Deck(cards[0])
cards.pop(0)
for card in cards:
deck.cards.append(card)
cards.remove(card)
print(deck.cards)
h1=deck.cards[1:7]
player=Hand(deck.cards[0])
deck.cards.pop(0)
print(player.score)
for card in h1:
deck.cards.remove(card)
print(deck.cards)
for card in h1:
player.check_match(card)
ai=AI(deck.cards[0])
deck.cards.pop(0)
h2=deck.cards[0:7]
for card in h2:
ai.check_match(card)
deck.cards.remove(card)
player.decide(ai)
I changed my mind, I believe I may have found the real culprit
I have no idea what went wrong with my most recent code, but I suspect it has something to do with the following so I'll start there. Did I do something wrong in this?
cards=['1','1','1','1','2','2','2','2','3','3','3','3','4','4','4','4','5','5','5','5','6','6','6','6','7','7','7','7','8','8','8','8','9','9','9','9','A','A','A','A','K','K','K','K','J','J','J','J','Q','Q','Q','Q']
import random
random.shuffle(cards)
h1=[]
for card in cards[1:7]:
h1.append(card)
cards.remove(card)
searchfor is in the Interface class.
I agree with Enalicho. However I can suggest that you write out in pseudo-code what you want the program to do. Pay close attention to using the words input and while. Also, think about the number(s) that need to increase and how they may do that. Post some code and we may help more.
Is there a real name for that window? Like run-time window or something?
I've just decided to "run minimized" in windows, which only minimizes the "shell-like" window. Hopefully there's actually a code to do it though.
I'm sorry, could you explain the resolution?