Re: Free place to host python files?
Stuff like code.google, sf.net, are more oriented towards serious development, not just holding random apps, aren't they? Anyway, I found MediaFire, which looks like it will suffice for now. -- http://mail.python.org/mailman/listinfo/python-list
Re: Free place to host python files?
On Dec 16, 8:29 am, s...@pobox.com wrote: > feba> I'm getting started in python, and it would be helpful to have a > feba> place to put up various code snippets I've made, so I don't have > feba> to send them individually to each person I want to show it to. > feba> I'd prefer to use something that would give me a directory for my > feba> use only, instead of something where you can only upload one at a > feba> time. I'd especially like to avoid stuff that uses CAPTCHAs > feba> and/or forced waiting periods. > > http://pastebin.com/? > > -- > Skip Montanaro - s...@pobox.com -http://smontanaro.dyndns.org/ well, ignoring the fact that pastebin doesn't work for me for some reason, I'm talking about hosting it as a .py downloadable, not a hunk of text. -- http://mail.python.org/mailman/listinfo/python-list
Free place to host python files?
I'm getting started in python, and it would be helpful to have a place to put up various code snippets I've made, so I don't have to send them individually to each person I want to show it to. I'd prefer to use something that would give me a directory for my use only, instead of something where you can only upload one at a time. I'd especially like to avoid stuff that uses CAPTCHAs and/or forced waiting periods. I'd really rather not have to run a server off my own computer, if it can be avoided at all. -- http://mail.python.org/mailman/listinfo/python-list
Re: Need help improving number guessing game
>The good news is that Python functions are objects too, so you can pass >them as params to another function. duh, duh, duh, duh, duh! I knew I was missing something there. Thanks. >if not mini <= x <= maxi: also thanks for this, I forgot about that. But I have it as if not minr < guess < maxr: because it's to DISALLOW the numbers to stay the same. >That was just a suggestion, and it would have been pretty >interesting IMHO as a basis for a "from Q&D procedural scripting to OO >application programing" tutorial. Yeah, I can see that. -- http://mail.python.org/mailman/listinfo/python-list
Re: Need help improving number guessing game
> .strip() returns a copy of the string without leading and ending whitespaces (inlcuding newlines, tabs etc). Ahh. I had removed it because it didn't seem to do anything, but I've readded it. And I understand your dictionary stuff correctly now, I think, and I worked it in. Currently, I have: import random def safeint(prompt="y"): while True: x = input(prompt) try: x = int(x) except ValueError: print("BAD INPUT!") else: break return x def safestr(prompt="y"): while True: x = input(prompt) try: x = str(x) except ValueError: print("BAD INPUT!") else: break return x def customrange(game, lowunsafe=True): game['defrang'] = False #Keeps setup from changing range to defaults while lowunsafe: #makes sure that the low number is positive picklow = safeint(prompt="PLEASE PICK THE LOW NUMBER: ") if picklow < 0: print("LOW NUMBER MUST BE POSITIVE") else: lowunsafe = False pickhigh = safeint(prompt="PLEASE PICK THE HIGH NUMBER: ") if pickhigh - picklow <= 2: #see setup(). print("HIGH MUST BE AT LEAST THREE GREATER THAN LOW") else: game['minr'], game['maxr'] = picklow, pickhigh print("RANGE IS [%s-%s]!" % (game['minr'], game['maxr'])) def wantcustom(game, unsure=True): #Allows user to decide their own range for guessing. while unsure: tryrange = safestr(prompt=\ "WOULD YOU LIKE TO CREATE A CUSTOM RANGE? "\ +"Y/N: ") if tryrange.strip().lower() == "n": game['minr'], game['maxr'] = 1, 99 #Default range. see setup unsure = False elif tryrange.strip().lower() == "y": customrange(game) unsure = False else: print("INVALID INPUT") def samesettings(game, unsure=True): while unsure: keepset = safestr(prompt="USE SAME SETTINGS? Y/N: ") if keepset.strip().lower() == "y": game['minr'], game['maxr'] = 1, 99 #Default range. see setup unsure = False elif keepset.strip().lower() == "n": wantcustom(game) numplayers(game) unsure = False else: print("INVALID INPUT") def setup(game): #minr, maxr make minimum and maximum. Can be adjusted. #Make sure that maxr - minr is at least 3. #1 or less would be impossible. 2 would only have one guess for victory #The first would be unplayable, the second would play itself if game['maxr'] - game['minr'] <= 2: raise ValueError("INVALID RANGE!") #If this fails, check line 43 game['gcount'] = 0 #Reset guess count game['target'] = random.randint(game['minr'], game['maxr']) def playerswitch(game): #Player Switch #if player's a witch: burn(her) if game['player'] is game['player1']: game['player'] = game['player2'] else: game['player'] = game['player1'] def youwin(game): if game['pnum'] == 1: print("CONGRATULATIONS! IT TOOK YOU %s GUESSES" % game ['gcount']) else: game['player']['score'] += 1 end = "CONGRATULATIONS %s! SCORE -- P1:%s P2:%s" print(end % (game['player']['name'],\ game['player1']['score'], game['player2']['score'])) def playagain(game, unsure=True): while unsure: playover = safestr(prompt="PLAY AGAIN? Y/N: ") if playover.strip().lower() == "y": game['play'] = True samesettings(game) setup(game) unsure = False elif playover.strip().lower() == "n": print("GOOD BYE. PLAY AGAIN SOON!") game['play'] = False unsure = False else: print("INVALID INPUT") def autofinish(game): if game['maxr'] - game['minr'] == 2: print("...ONLY ONE OPTION LEFT!") youwin(game) playagain(game) def numplayers(game, unsafe=True): while unsafe: num = safeint(prompt="1 OR 2 PLAYERS?\n> ") if num == 1 or 2: #ONLY allow 1 or 2P. unsafe = False else: print("INVALID INPUT") game['pnum'] = num def guesses(game, unsafe=True): while unsafe: guess = safeint(prompt="[%s-%s]%s>> " % \ #Shows range (game['minr'], game['maxr'],\ #And which player's turn game['player']['name'])) if guess >= game['maxr']: print("NUMBER MUST BE IN RANGE") guesses(game) guesscheck(game) elif guess <= game['minr']: print("NUMBER MUST BE IN RANGE") guesses(game) guesscheck(game) else: unsafe = False game['guess'] = guess def guesscheck(game): if game['guess'] == game['target']: if game['pnum'
Re: Need help improving number guessing game
Spent a bit more time looking over suggestions and working out some annoyances. import random def customrange(game, lowunsafe=True): game['defrang'] = False #Keeps setup from changing range to defaults while lowunsafe: #makes sure that the low number is positive picklow = int(input("PLEASE PICK THE LOW NUMBER: ")) if picklow < 0: print("LOW NUMBER MUST BE POSTIVE") else: lowunsafe = False pickhigh = int(input("PLEASE PICK THE HIGH NUMBER: ")) if pickhigh - picklow <= 2: #see setup(). print("HIGH MUST BE AT LEAST THREE GREATER THAN LOW") else: game['minr'], game['maxr'] = picklow, pickhigh print("RANGE IS [%s-%s]!" % (game['minr'], game['maxr'])) def wantcustom(game, unsure=True): #Allows user to decide their own range for guessing. while unsure: pickrange = input("WOULD YOU LIKE TO CREATE A CUSTOM RANGE? Y/ N: ") if pickrange.lower() == "n": game['minr'], game['maxr'] = 1, 99 #Default range. see setup unsure = False elif pickrange.lower() == "y": customrange(game) unsure = False else: print("INVALID INPUT") def samesettings(game, unsure=True): while unsure: keepset = input("USE SAME SETTINGS? Y/N: ") if keepset.lower() == "y": game['minr'], game['maxr'] = 1, 99 #Default range. see setup unsure = False elif keepset.lower() == "n": wantcustom(game) numplayers(game) unsure = False else: print("INVALID INPUT") def setup(game): #minr, maxr make minimum and maximum. Can be adjusted. #Make sure that maxr - minr is at least 3. #1 or less would be impossible. 2 would only have one guess for victory #The first would be unplayable, the second would play itself if game['maxr'] - game['minr'] <= 2: raise ValueError("INVALID RANGE!") #If this fails, check line 43 game['gcount'] = 0 #Reset guess count game['target'] = random.randint(game['minr'], game['maxr']) def playerswitch(game): #Player Switch #if player's a witch: burn(her) if game['player'] == game['player1']: game['player'] = game['player2'] else: game['player'] = game['player1'] def youwin(game): if game['pnum'] == 1: print("CONGRATULATIONS! IT TOOK YOU %s GUESSES" % game ['gcount']) else: if game['player'] == game['player1']: game['p1sc'] += 1 else: game['p2sc'] += 1 end = "CONGRATULATIONS %s! SCORE -- P1:%s P2:%s" print(end % (game['player'], game['p1sc'], game['p2sc'])) def playagain(game, unsure=True): while unsure: playover = input("PLAY AGAIN? Y/N: ") if playover.lower() == "y": game['play'] = True samesettings(game) setup(game) unsure = False elif playover.lower() == "n": print("GOOD BYE. PLAY AGAIN SOON!") game['play'] = False unsure = False else: print("INVALID INPUT") def autofinish(game): if game['maxr'] - game['minr'] == 2: print("...ONLY ONE OPTION LEFT!") youwin(game) playagain(game) def numplayers(game, unsafe=True, prompt="1 OR 2 PLAYERS?\n> "): while unsafe: while True: num = input(prompt) try: #Make sure that num is valid num = int(num) except ValueError: print("BAD VALUE!") else: break if num == 1 or 2: #ONLY allow 1 or 2P. unsafe = False else: print("INVALID INPUT") game['pnum'] = num def guesses(game, unsafe=True): while unsafe: while True: try: guess = int(input("[%s-%s]%s>> " \ #keeps user aware of who's turn it is, and the range % (game['minr'], game['maxr'], game['player']))) except ValueError: print("BAD VALUE!") else: break if guess >= game['maxr']: print("NUMBER MUST BE IN RANGE") guesses(game) guesscheck(game) elif guess <= game['minr']: print("NUMBER MUST BE IN RANGE") guesses(game) guesscheck(game) else: unsafe = False game['guess'] = guess def guesscheck(game): if game['guess'] == game['target']: if game['pnum'] == 1: game['gcount'] += 1 youwin(game) playagain(game) elif game['guess'] > game['target']: print("TOO HIGH") if game['pnum'] == 1: game['gcount'] += 1 game['maxr'] = game['guess'] else: print("TOO LOW") if game['pnum'] == 1: game['gcount'] += 1 game['minr'] = game['guess'] def guessing(game): guesses(game) guessc
Re: Need help improving number guessing game
I added the ability to select your own range. It takes two new modules: def customrange(game, lowunsafe=True): game['defrang'] = False #Keeps setup from changing range to defaults while lowunsafe: #makes sure that the low number is positive picklow = int(input("PLEASE PICK THE LOW NUMBER: ")) if picklow < 0: print("LOW NUMBER MUST BE POSTIVE") else: lowunsafe = False pickhigh = int(input("PLEASE PICK THE HIGH NUMBER: ")) if pickhigh - picklow <= 2: #see setup(). print("HIGH MUST BE AT LEAST THREE GREATER THAN LOW") else: game['minr'], game['maxr'] = picklow, pickhigh print("RANGE IS [%s-%s]!" % (game['minr'], game['maxr'])) def wantcustom(game, unsure=True): #Allows user to decide their own range for guessing. while unsure: pickrange = input("WOULD YOU LIKE TO CREATE A CUSTOM RANGE? Y/ N: ") if pickrange.lower() == "n": game['minr'], game['maxr'] = 1, 99 #Default range unsure = False elif pickrange.lower() == "y": customrange(game) unsure = False else: print("INVALID INPUT") A slightly updated setup (it needed it anyway): def setup(game): #minr, maxr make minimum and maximum. Can be adjusted. #Make sure that maxr - minr is at least 3. #1 or less would be impossible. 2 would only have one guess for victory #The first would be unplayable, the second would play itself if game['maxr'] - game['minr'] <= 2: raise ValueError("INVALID RANGE!") game['gcount'] = 0 #Reset guess count game['target'] = random.randint(game['minr'], game['maxr']) and putting wantcustom(game) immediately before setup(game) in main(). -- http://mail.python.org/mailman/listinfo/python-list
Re: Need help improving number guessing game
Alright! This is feeling more like it. #!/usr/bin/python #Py3k, UTF-8 import random def setup(game, minr=1, maxr=99): #minr, maxr make minimum and maximum. Can be adjusted. game['minr'], game['maxr'] = minr, maxr game['gcount'] = 0 #Reset guess count game['target'] = random.randint(minr, maxr) def playerswitch(game): #Player Switch #if player's a witch: burn(her) if game['player'] == game['player1']: game['player'] = game['player2'] else: game['player'] = game['player1'] def youwin(game): if game['pnum'] == 1: print("CONGRATULATIONS! IT TOOK YOU %s GUESSES" % game ['gcount']) else: if game['player'] == game['player1']: game['p1sc'] += 1 else: game['p2sc'] += 1 end = "CONGRATULATIONS %s! SCORE -- P1:%s P2:%s" #Can the following line be more compact? print(end % (game['player'], game['p1sc'], game['p2sc'])) def playagain(game): playover = input("PLAY AGAIN? Y/N: ") if playover.strip().lower() == "y": game['play'] = True setup(game) else: print("GOOD BYE. PLAY AGAIN SOON!") game['play'] = False def autofinish(game): if game['maxr'] - game['minr'] == 2: print("...ONLY ONE OPTION LEFT!") youwin(game) playagain(game) def numplayers(game, prompt="1 OR 2 PLAYERS?\n> "): while True: num = input(prompt) try: num = int(num) except ValueError: print("BAD VALUE") else: break game['pnum'] = num def guesses(game): game['guess'] = int(input("[%s-%s]%s>> " \ #keeps user aware of who's turn it is, and the range % (game['minr'], game['maxr'], game['player']))) def guesscheck(game): if game['guess'] == game['target']: if game['pnum'] == 1: game['gcount'] += 1 youwin(game) playagain(game) elif game['guess'] >= game['maxr']: print("NUMBER MUST BE IN RANGE") guesses(game) guesscheck(game) elif game['guess'] <= game['minr']: print("NUMBER MUST BE IN RANGE") guesses(game) guesscheck(game) elif game['guess'] > game['target']: print("TOO HIGH") if game['pnum'] == 1: game['gcount'] += 1 game['maxr'] = game['guess'] else: print("TOO LOW") if game['pnum'] == 1: game['gcount'] += 1 game['minr'] = game['guess'] def guessing(game): guesses(game) guesscheck(game) if game['pnum'] == 2: playerswitch(game) autofinish(game) def main(game=None): if game is None: game = {} print("WELCOME TO THE SUPER NUMBER GUESSING GAME!") numplayers(game) game['play'] = True game['player1'], game['player2'] = "P1", "P2" game['player'] = game['player1'] # P1 goes first #Scores start at 0 game['p1sc'], game['p2sc'], game['gcount'] = 0, 0, 0 setup(game) while game['play'] is True: guessing(game) if __name__ == "__main__": main() first off, I want to thank all of you for your help with this. I really don't think I could've learned all of this out nearly as quickly by reading tutorials and documentation, let alone had anything near the grasp I have on it now. '''This''' is why I like learning by doing. The only things I still don't really understand are .strip ().lower(), and try/except/else, and I plan on looking them up before I do anything else. In the past few hours I've gone from not having a clue what the whole {'fred': 0, 'barney': 0} thing was about to being able to fully understand what you're talking about, and put it into practice 2; I feel like this process is going quicker and quicker every time I refine it. It also feels like it's getting easier to solve various bugs when I create them they pop up. It might be because I'm getting into it and having more fun, because the refinements are less major each time, because they're easier to pick up, or some combination of all of those. Either way, I feel very excited about it. 3; I found some very helpful gedit plugins. While I was in preferences, I noticed you can have it highlight the margin; so I set that to 75 to try to keep from going over the character limit/line recommendation. Draw Spaces, a plugin, showing spaces is also pretty helpful. I also found a python auto complete plugin which I haven't used so far, but which looks very promising, along with a terminal program (Keeps me from juggling windows, anyway) 4; I readded the counter for one player games. Out of curiosity, what do you think of: if game['pnum'] == 1: game['gcount'] += 1 ? I'm not sure whether this is good or bad. On the one hand, it keeps it from adding to gcount without needing to, on the other hand it seems like it might be more wasteful to check pnum than to just add to gcount. It also makes it harder to adjust it to show gcount in two player mode, if you want to do that
Re: Need help improving number guessing game
>Seems ok. You may want to use arguments with default values for a and b >(and possibly to use more meaningfull names): I changed it to minr and maxr. Mini is fine, but I can't name a variable maxi unless I have a good feminine hygiene joke to use with it. I don't see the aim of your changes to setup(). I can kinda understand checking to make sure that you didn't make the minimum higher than the maximum, but I think where you put minr/maxr would make it use the same minr/maxr as the end of the previous game, wouldn't it? >Minor point for a short program, but still good practice : use >constants. IE : I had done this initially, but it seemed wasteful and needlessly confusing in this situation. >I assume 'p1sc' means "player_1_score" ? >If so, you may want to use a dict for scores: I don't really understand dicts yet; actually, the tutorial I'm following (http://www.briggs.net.nz/log/writing/snake-wrangling-for- kids/ , designed for tweens, but other than the pointless anecdote and joke here and there, I've found it a very good guide) doesn't even seem to mention them, from a search of the pdf. Actually, apparently I stopped and started working on this just before the chapter on functions and modules. I'll look into that later on, but for now I'm pretty happy with how it works. >Is it really necessary to WRITE THIS IN ALL UPPERS ?-) If you didn't notice, in the original it was "CONGLATURATIONS". I could also make a "VIDEO GAME BAND. Heavy Metal's Not Dead." joke here, but I'm afraid a disappointingly small amount of people will get it. >Python has proper booleans too Yeah, I had those initially too, but "play = True" was causing trouble for some reason when I copy/pasted it into IDLE to try to troubleshoot, so I changed it to 1. I'll probably change it back later. >You should either put this in it's own function (could be named 'main'), >or at least "protect" it with an "if __name__ == '__main__':" test. Could you go into a bit more detail on this? I don't understand what should be its own function, nor do I understand what that line would do or how to use it. James, could you work that into a section of what I have to make it a bit easier to understand? -- http://mail.python.org/mailman/listinfo/python-list
Re: Need help improving number guessing game
#!/usr/bin/python #Py3k, UTF-8 import random def setup(): #global target, guess, a, b #a, b make minimum, maximum. Can be adjusted. a, b = 1, 99 target = random.randint(a, b) return a, b, target def playerswitch(player): #Player Switch #if player's a witch, burn her! if player == "P1": player = "P2" else: player = "P1" return player def youwin(pnum, player, p1sc, p2sc): if pnum == 1: print("CONGRATULATIONS!") else: if player == "P1": p1sc += 1 else: p2sc += 1 end = "CONGRATULATIONS %s! SCORE -- P1:%s P2:%s" print(end %(player, p1sc, p2sc)) return p1sc, p2sc def playagain(play): playover = input("PLAY AGAIN? Y/N: ") if playover.strip().lower() == "y": play = 1 a, b, target = setup() else: print("GOOD BYE. PLAY AGAIN SOON!") quit() return play, a, b, target def guesscheck(guess, target, play, a, b, p1sc, p2sc): if guess == target: p1sc, p2sc = youwin(pnum, player, p1sc, p2sc) play, a, b, target = playagain(play) elif guess >= b: print("NUMBER MUST BE IN RANGE") guess = int(input("[%s-%s]%s>> " % (a, b, player))) play, a, b, target, p1sc, p2sc = guesscheck(guess, target, play, a, b, p1sc, p2sc) elif guess <= a: print("NUMBER MUST BE IN RANGE") guess = int(input("[%s-%s]%s>> " % (a, b, player))) play, a, b, target, p1sc, p2sc = guesscheck(guess, target, play, a, b, p1sc, p2sc) elif guess > target: print("TOO HIGH") b = guess else: print("TOO LOW") a = guess return play, a, b, target, p1sc, p2sc def guessing(a, b, player, pnum, target, p1sc, p2sc, play): #a and b are to keep the user aware of min/max guess = int(input("[%s-%s]%s>> " % (a, b, player))) play, a, b, target, p1sc, p2sc = guesscheck(guess, target, play, a, b, p1sc, p2sc) if pnum == 2: player = playerswitch(player) return play, a, b, player, target, p1sc, p2sc #Let the show begin! print("WELCOME TO THE SUPER NUMBER GUESSING GAME!") pnum = int(input("1 OR 2 PLAYERS?\n> ")) play = 1 player = "P1" # P1 goes first #Scores, keep track of times player guessed first. p1sc, p2sc = 0, 0 #Grabs minimum, maximum, and target numbers a, b, target = setup() while play == 1: play, a, b, player, target, p1sc, p2sc \ = guessing(a, b, player, pnum, target, p1sc, p2sc, play) This is what I have now. It seems to work perfectly, and I tried to work in your suggestions. Thanks, by the way, for recommending the style guide. I've bookmarked it, and I'll try to remember to consult it in the future. One (extremely minor) annoyance is that if you have only one possible guess left you still have to type it in manually. As an example, [30-32]P#>> can only possibly be 31 (and I've changed it so that any number >= 32 or <= 30 won't be accepted, so it's all the user can input), which means whatever player is in control has a %100 chance of winning. I know that "if b - a == 2" would be a simple enough test for this, but I'm not quite sure when to put it, or what to do with it. And yes, I'm aware that guess = int(input("[%s-%s]%s>> " % (a, b, player))) play, a, b, target, p1sc, p2sc = guesscheck(guess, target, play, a, b, p1sc, p2sc) is repeated three times. I will probably try to change this into a function later on; right now, I just spent a few hours trying to get this to work and making sure that it does (by playing it), so I'm going to take a break. -- http://mail.python.org/mailman/listinfo/python-list
Re: Need help improving number guessing game
#!/usr/bin/python #Py3k, UTF-8 import random def startup(): print("WELCOME TO THE SUPER NUMBER GUESSING GAME!") global pnum, play, player, p1sc, p2sc pnum = int(input("1 OR 2 PLAYERS?\n> ")) play = True player = "P1" #P1 goes first p1sc = 0 #Number of times... p2sc = 0 #player guessed before opponent def setup(): global target, guess, a, b a = 1 b = 99 target = random.randint(a, b) guess = 0 #Won't fall between 1 and 99 def playerswap(): global player if player == "P1": player = "P2" else: player = "P1" def guessing(): global guess, player, target, play, pnum, p1sc, p2sc, a, b guess = int(input("[%s-%s]%s>> " % (a, b, player))) #keeps the user aware of min/max if guess == target: if pnum == 1: print("CONGRATULATIONS!" ) else: if player == "P1": p1sc += 1 else: p2sc += 1 print("CONGRATULATIONS %s! SCORE -- P1:%s P2:%s" %(player, p1sc, p2sc)) playover = input("PLAY AGAIN? Y/N: ") if playover.strip().lower() == "y": play = True setup() else: play = False elif guess > b: print("NUMBER MUST BE IN RANGE") elif guess <= a: print("NUMBER MUST BE IN RANGE") elif guess > target: print("TOO HIGH") b = guess else: print("TOO LOW") a = guess if pnum ==2: playerswap() startup() setup() while play is True: guessing() This is what I have so far. better? worse? I'm guessing a mix of the two. It took me a lot longer to get working, but I think it works better. I also added a bit that tells you if you go higher or lower than an already specified too high/low markers; although it doesn't make you repeat that turn. I'm not sure if all those 'globals' are bad, but they don't seem like they're something that should be good. Functionally, it seems to work just fine. -- http://mail.python.org/mailman/listinfo/python-list
Need help improving number guessing game
#!/usr/bin/python/ #Py3k, UTF-8 import random print(" --- WELCOME TO THE SUPER NUMBER GUESSING GAME --- " + ("\n" * 5)) pnum = int(input("1 OR 2 PLAYER?\nP#: ")) target = random.randint(1, 99) #Pick a random number under two digits guess1 = 0 #Zero will never be picked as target... guess2 = 0 #so it makes a good default value p1score = 0 #For two player mode... p2score = 0 #let's keep score! print("LET'S START THE GAME. \nGUESS ANY WHOLE NUMBER FROM 1 TO 99.") while True: if pnum == 1: #1p mode while True: guess1 = int(input("\n>> ")) if guess1 > 100: print("ONLY NUMBERS FROM 1 TO 99") elif guess1 > target: print("TOO HIGH") elif guess1 == target: print("CONGLATGURATIONS! PLAY AGAIN?") target = random.randint(1, 99) #Set up the game again play = int(input("0 TO END: ")) if play == 0: print("GOOD BYE. PLAY AGAIN SOON!") quit() else: print("TOO LOW") if pnum == 2: #2p mode while True: guess1 = int(input("\nP1> ")) #Player 1's turn if guess1 > 100: print("ONLY NUMBERS FROM 1 to 99") elif guess1 > target: print("TOO HIGH") elif guess1 == target: p1score += 1 print("GOOD JOB, PLAYER 1! THE SCORE IS:\nP1: %s --- P2: %s\nPLAY AGAIN?" % (p1score, p2score)) target = random.randint(1, 99) #Set up game again play = int(input("0 TO END: ")) if play == 0: print("GOOD BYE. PLAY AGAIN SOON!") else: print("TOO LOW") guess2 = int(input("\nP2> ")) #Player 2's turn if guess2 > 100: print("ONLY NUMBERS FROM 1 to 99") elif guess2 > target: print("TOO HIGH") elif guess2 == target: p2score += 1 print("GOOD JOB, PLAYER 2! THE SCORE IS:\nP1: %s --- P2: %s\nPLAY AGAIN?" % (p1score, p2score)) target = random.randint(1, 99) #Set up game again play = int(input("0 TO END: ")) if play == 0: print("GOOD BYE. PLAY AGAIN SOON!") else: print("TOO LOW") else: print("INVALID PLAYER SELECTION") pnum = int(input("1 OR 2 PLAYER?\nPN#: ")) I have one major problem with this; the 'replay' selection. It quits if you put in 0, as it should, and continues if you put in any other number. However, if you just press enter, it exits with an error. it also looks really ugly, and I'm sure there has to be plenty of better ways to do it. I'd also appreciate tips on how it could be better in general. I should think that P1 and P2's turns shouldn't have to be completely repeated; but I'm not quite sure how to def something like that. -- http://mail.python.org/mailman/listinfo/python-list
Re: (Very Newbie) Problems defining a variable
Actually, I have gedit set to four spaces per tab. I have no reason why it's showing up that large on copy/paste, but the file itself is fine. Thanks for the advice Chris, Stephen, I can definitely see how those are both far better ways of doing it. I have it as: #!/usr/bin/python #Py3k, UTF-8 bank = int(input("How much money is in your account?\n>>")) if bank <=0: print("You need a postive amount to gain interest.") quit() target = int(input("How much money would you like to earn each year? \n>>")) interest = 0 i = 0 while interest < target: #determine the interest rate to use if bank >= 10: rate = 0.0173 elif bank >= 5: rate = 0.0149 elif bank >= 25000: rate = 0.0124 elif bank >= 1: rate = 0.0085 else: rate = 0.0060 #Now that we know what interest rate to use, apply it... lastbank = bank#To calculate interest... bank += (bank * rate) #Update earnings... interest = bank - lastbank #And figure out how much interest is made! i += 1 #So we can see which year a calculation represents print("Year %s, at %s rate: %s paid, %s in bank." % (i, rate, interest, bank)) now it checks to make sure the account is positive before moving on, in addition to using your recommendations on readability and efficiency in getting the rate -- http://mail.python.org/mailman/listinfo/python-list
Re: (Very Newbie) Problems defining a variable
On Dec 12, 5:56 am, Bruno Desthuilliers wrote: > feb...@gmail.com a écrit : > > > > > #!/usr/bin/python > > #Py3k, UTF-8 > > > bank = int(input("How much money is in your account?\n>>")) > > target = int(input("How much money would you like to earn each year? > > \n>>")) > > > interest = 0 > > i = 0 > > > while interest < target: > > #determine the interest rate to use > > if bank >= : > > rate = 0.006 > > elif bank >= 1 and bank <= 24999: > > rate = 0.0085 > > elif bank >= 25000 and bank <= 4: > > rate = 0.0124 > > elif bank >= 5 and bank <= 9: > > rate = 0.0149 > > elif bank >= 10: > > rate = 0.0173 > > (snip) > > > I'm pretty certain that that is also the problem in the code. I'm > > pretty sure it's a problem with the 'if' statements', and it looks > > like it's one of those mistakes that's so simple you look back on it > > and laugh at yourself. If you put in a bank number <= , it fails, > > saying "NameError: name 'rate' is not defined". If you put in one > > higher, it runs correctly, but thinks that the rate is 0.006 > > Indeed. That's what you asked for. If bank is >= , then rate will be > set to 0.006, and the following tests will be skipped. Else - since you > just don't handle the case -, rate is not defined at all. > > I guess you wanted your first test to be: > > if bank <= : > ... > > FWIW, when using if/elif that way, make sure you always end with a > "default" else clause (even if just to signal you didn't expect to be > there...) > > HTH that's it, thanks! was confused with it being basically in a column of all >= *. I replaced it with if bank <= 0: print("You're in the red!") quit() elif bank >= 1 and bank <= : rate = 0.0060 elif bank >= 1 and bank <= 24999: rate = 0.0085 elif bank >= 25000 and bank <= 4: rate = 0.0124 elif bank >= 5 and bank <= 9: rate = 0.0149 elif bank >= 10: rate = 0.0173 else: print("What's this doing here?") which also changes it to keep it from going on forever if you put in a negative amount. Out of curiosity, would you still recommend applying an 'else' clause in this case? I don't see how it could ever be triggered, even if there's an error of some kind -- http://mail.python.org/mailman/listinfo/python-list