Problem with numbers

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Aug 2005
Posts: 138
Reputation: a1eio is an unknown quantity at this point 
Solved Threads: 21
a1eio's Avatar
a1eio a1eio is offline Offline
Junior Poster

Re: Problem with numbers

 
0
  #11
Aug 4th, 2006
Sweet man!, thats wicked. Nevermind the 'shortcuts' it works like a beaut'.
One thing though, when you enter large armies (eg: 10 v 10) it keeps running the simulation until the end, where it then shows you the final outcome, you could (if you wanted) put in a simple line:
  1. raw_input("Hit Enter To Continue")
this would act as a 'pause' (very much like the one you have at the VERY end of your program) allowing the reader to read that particular battle then move on to the next when ready. All you would have to do is put it at the begining (or end) of the main 'while True' loop.

Great job though
Last edited by a1eio; Aug 4th, 2006 at 1:48 am.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 187
Reputation: Matt Tacular is an unknown quantity at this point 
Solved Threads: 7
Matt Tacular's Avatar
Matt Tacular Matt Tacular is offline Offline
Unverified User

Re: Problem with numbers

 
0
  #12
Aug 4th, 2006
it still errors if nothing is entered at all, how do i make it refuse to accept nothing for an answer.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 138
Reputation: a1eio is an unknown quantity at this point 
Solved Threads: 21
a1eio's Avatar
a1eio a1eio is offline Offline
Junior Poster

Re: Problem with numbers

 
0
  #13
Aug 4th, 2006
where you have the 'try' and 'except' section of your code, your checking to see if there is a NameError, that means, if there is a NameError, it will catch it, however, it will not catch any others. If you don't specify a particular error to catch, then it will check for all errors.

in simple terms, just take NameError out of the except part, so your left with simply:
  1. except:
  2. ...

however, if you want to return specific responses acording to specific errors you could have something along the lines of:
  1. try:
  2. ...
  3. except NameError:
  4. print "Please enter a number."
  5. except SyntaxError:
  6. print "You must enter something"
  7. except:
  8. print "Stop screwing about and just enter number!"
but that above isn't probably needed, as the user is only entering a number. so on except: should do it all.
Last edited by a1eio; Aug 4th, 2006 at 7:59 pm.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 187
Reputation: Matt Tacular is an unknown quantity at this point 
Solved Threads: 7
Matt Tacular's Avatar
Matt Tacular Matt Tacular is offline Offline
Unverified User

Re: Problem with numbers

 
0
  #14
Aug 13th, 2006
Ok, I have a new problem now, i was planing on using the program i made using your guys' help here to make a graphical risk game, and i figured i would start simple with just a block of four countries.

[php]from Tkinter import *

def addMen():
addMenC = input("Where do you wish to add men? (country 1,2,3 or 4):")
addMenLoop = True
c1a = 0
c2a = 0
c3a = 0
c4a = 0
while addMenLoop:
if addMenC == 1:
c1a += 3
country1Label.configure(text = str(c1a))
break
elif addMenC == 2:
c2a += 3
country2Label.configure(text = str(c2a))
break
elif addMenC == 3:
c3a += 3
country3Label.configure(text = str(c3a))
break
elif addMenC == 4:
c4a += 3
country1Label.configure(text = str(c4a))
break



riskWindow = Tk()
riskWindow.title("Risk")

country1TitleLabel = Label(riskWindow, text="Country 1", width=30)
country1TitleLabel.grid(row=0, column=0)

country2TitleLabel = Label(riskWindow, text="Country 2", width=30)
country2TitleLabel.grid(row=0, column=1)

country3TitleLabel = Label(riskWindow, text="Country 3", width=30)
country3TitleLabel.grid(row=2, column=0)

country4TitleLabel = Label(riskWindow, text="Country 4", width=30)
country4TitleLabel.grid(row=2, column=1)

country1Label = Label(riskWindow, text=str(c1a), width=30)
country1Label.grid(row=1, column=0)

country2Label = Label(riskWindow, text=str(c2a), width=30)
country2Label.grid(row=1, column=1)

country3Label = Label(riskWindow, text=str(c3a), width=30)
country3Label.grid(row=3, column=0)

country4Label = Label(riskWindow, text=str(c4a), width=30)
country4Label.grid(row=3, column=1)

armyButton = Button(riskWindow, text="Add men to a country", command=addMen)
armyButton.grid(row=4, column=0)

riskWindow.mainloop()
[/php]

the problem i would like solved is that when you want to add men, it will make it add the 3 guys to the country, but if you want to add 3 more it does nothing. i need it to add the 3 guys, not just put 3 guys total on the country.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 1,546
Reputation: Ene Uran has a spectacular aura about Ene Uran has a spectacular aura about 
Solved Threads: 174
Ene Uran's Avatar
Ene Uran Ene Uran is offline Offline
Posting Virtuoso

Re: Problem with numbers

 
0
  #15
Aug 14th, 2006
Once you go to GUI programming, your approach has to change past just making labels. User actions are initiated by clicking on buttons, data is entered in an entry widget, results and messages can be displayed in labels.

Get used to callback functions. Also, you might as well start a class Risk, so variables are transferred properly via the self. prefix. Just a little extra learning curve here! GUI programming is not too hard, just a mildly different way of thinking
Last edited by Ene Uran; Aug 14th, 2006 at 3:42 pm.
drink her pretty
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 187
Reputation: Matt Tacular is an unknown quantity at this point 
Solved Threads: 7
Matt Tacular's Avatar
Matt Tacular Matt Tacular is offline Offline
Unverified User

Re: Problem with numbers

 
0
  #16
Aug 17th, 2006
can someone show me what ene uran is talking about, i dont quite get it. I understand doing everything through labels instead of the other window, but the way i program when im learning a language, is to do it as simple as possible from my point of view, even if its harder to use the program, then i slowly change the reast of the code to make it easier to use the program, i will do the same here.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,043
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 934
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Problem with numbers

 
0
  #17
Aug 17th, 2006
Matt, as I look at your initial GUI code I can see two problems not related to Tkinter. I marked them with # !!!!!!!!!!!!!!!!, those will be errors!
[php]from Tkinter import *

def addMen():
addMenC = input("Where do you wish to add men? (country 1,2,3 or 4):")
addMenLoop = True
c1a = 0
c2a = 0
c3a = 0
c4a = 0
while addMenLoop:
if addMenC == 1:
c1a += 3
# !!!!!!!!!!!!!!!!!
# country1Label was defined outside the function
# and is not known inside this function
# dito for the three other labels
country1Label.configure(text = str(c1a))
break
elif addMenC == 2:
c2a += 3
country2Label.configure(text = str(c2a))
break
elif addMenC == 3:
c3a += 3
country3Label.configure(text = str(c3a))
break
elif addMenC == 4:
c4a += 3
country1Label.configure(text = str(c4a))
break



riskWindow = Tk()
riskWindow.title("Risk")

country1TitleLabel = Label(riskWindow, text="Country 1", width=30)
country1TitleLabel.grid(row=0, column=0)

country2TitleLabel = Label(riskWindow, text="Country 2", width=30)
country2TitleLabel.grid(row=0, column=1)

country3TitleLabel = Label(riskWindow, text="Country 3", width=30)
country3TitleLabel.grid(row=2, column=0)

country4TitleLabel = Label(riskWindow, text="Country 4", width=30)
country4TitleLabel.grid(row=2, column=1)

# !!!!!!!!!!!!!!!!!!!!!!!!!
# c1a was defined in the function, but is not known outside the function
# dito for c2a, c3a, c4a in the lines that follow
country1Label = Label(riskWindow, text=str(c1a), width=30)
country1Label.grid(row=1, column=0)

country2Label = Label(riskWindow, text=str(c2a), width=30)
country2Label.grid(row=1, column=1)

country3Label = Label(riskWindow, text=str(c3a), width=30)
country3Label.grid(row=3, column=0)

country4Label = Label(riskWindow, text=str(c4a), width=30)
country4Label.grid(row=3, column=1)

armyButton = Button(riskWindow, text="Add men to a country", command=addMen)
armyButton.grid(row=4, column=0)

riskWindow.mainloop()
[/php]
Now, you could make all those variables global for the function.
[php]from Tkinter import *

def addMen():
# make these labels global so they can be used in the function
global country1Label
global country2Label
global country3Label
global country4Label
# make these variables global, so changes transfer out
global c1a, c2a, c3a, c4a
addMenC = input("Where do you wish to add men? (country 1,2,3 or 4):")
addMenLoop = True
while addMenLoop:
if addMenC == 1:
c1a += 3
country1Label.configure(text = str(c1a))
break
elif addMenC == 2:
c2a += 3
country2Label.configure(text = str(c2a))
break
elif addMenC == 3:
c3a += 3
country3Label.configure(text = str(c3a))
break
elif addMenC == 4:
c4a += 3
country1Label.configure(text = str(c4a))
break


riskWindow = Tk()
riskWindow.title("Risk")

c1a = 0
c2a = 0
c3a = 0
c4a = 0

country1TitleLabel = Label(riskWindow, text="Country 1", width=30)
country1TitleLabel.grid(row=0, column=0)

country2TitleLabel = Label(riskWindow, text="Country 2", width=30)
country2TitleLabel.grid(row=0, column=1)

country3TitleLabel = Label(riskWindow, text="Country 3", width=30)
country3TitleLabel.grid(row=2, column=0)

country4TitleLabel = Label(riskWindow, text="Country 4", width=30)
country4TitleLabel.grid(row=2, column=1)

country1Label = Label(riskWindow, text=str(c1a), width=30)
country1Label.grid(row=1, column=0)

country2Label = Label(riskWindow, text=str(c2a), width=30)
country2Label.grid(row=1, column=1)

country3Label = Label(riskWindow, text=str(c3a), width=30)
country3Label.grid(row=3, column=0)

country4Label = Label(riskWindow, text=str(c4a), width=30)
country4Label.grid(row=3, column=1)

armyButton = Button(riskWindow, text="Add men to a country", command=addMen)
armyButton.grid(row=4, column=0)

riskWindow.mainloop()
[/php]
The next issue will be that input() happens in the command window, and you have to switch back and forth between the tk window and the command winow. You will get tired of that real quick, that's where the entry widget comes to the rescue. Now your input happens in the tk window. By the way, Tkinter also has a spinbox widget and you can make the value jump by three on every click of the up arrow.

Anyway, happy explorations! Best way to learn!
Last edited by vegaseat; Aug 17th, 2006 at 5:11 pm.
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,043
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 934
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Problem with numbers

 
0
  #18
Aug 17th, 2006
My bad! Actually, as I was playing around with the code, I discovered that the following global defines were not needed:
[php] # make these labels global so they can be used in the function
global country1Label
global country2Label
global country3Label
global country4Label
[/php]
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC