| | |
Problem with numbers
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
![]() |
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:
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
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:
Python Syntax (Toggle Plain Text)
raw_input("Hit Enter To Continue")
Great job though
Last edited by a1eio; Aug 4th, 2006 at 1:48 am.
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:
however, if you want to return specific responses acording to specific errors you could have something along the lines of:
but that above isn't probably needed, as the user is only entering a number. so on except: should do it all.
in simple terms, just take NameError out of the except part, so your left with simply:
Python Syntax (Toggle Plain Text)
except: ...
however, if you want to return specific responses acording to specific errors you could have something along the lines of:
Python Syntax (Toggle Plain Text)
try: ... except NameError: print "Please enter a number." except SyntaxError: print "You must enter something" except: print "Stop screwing about and just enter number!"
Last edited by a1eio; Aug 4th, 2006 at 7:59 pm.
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.
[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.
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
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
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.
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!
[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!
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]
[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!
![]() |
Similar Threads
- Sony Vaio Fn key problem (USB Devices and other Peripherals)
- Loop counting odd and even numbers (C++)
- Sorting and Searching to get rid of Duplicate Numbers (C++)
- PS2 Keyboard problem in XP (USB Devices and other Peripherals)
- Number input problem. (C++)
- Basic File I/O problem (C++)
- Matching negative numbers (Perl)
- gcd problem (C++)
- factorizing numbers (Java)
- How many different numbers are there? (C++)
Other Threads in the Python Forum
- Previous Thread: python for web design
- Next Thread: range().remove()?
| Thread Tools | Search this Thread |
advanced aliased bash beginner bits calling casino changecolor class clear command convert corners count csv cturtle cursor def definedlines dictionary digital dynamic dynamically events examples external file float format frange function google gui hints homework i/o iframe import info input java line linux list lists loop matching mouse multiple number numbers obexftp output parsing path port prime programming projects py py2exe pygame pygtk python random rational raw_input recursion return scrolledtext signal singleton skinning stderr string strings subprocess table tails terminal text thread threading time tkinter tlapse tuple tutorial ubuntu unicode urllib urllib2 valueerror variable voip web-scrape whileloop windows word wxpython






