Infinite Looping Issue - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Infinite Looping Issue (/thread-27640.html) |
Infinite Looping Issue - nsadams87xx - Jun-14-2020 Hey everyone, I'm trying to write the darts game "Cricket". If anyone is not familiar with the game it goes like this: A player needs to hit 15 through 20 and the bullseye 3 times total. Each turn you get 3 darts. If you don't hit 15, 16, 17, 18, 19, 20, or a BE, the dart counts for nothing. If you have already hit one of these numbers 3 times and you hit it again, your player score increases by that number. (Ex: 20 has been hit 5 times, so the 20 block is "closed" out and the player would have 40 points so far). I'm starting off easy and just using 20 and bullseye for now. The trouble I'm having is that when I run my code I get stuck in an infinite loop even after "closing out" 20 and BE. Any suggestions? twenty = 0 bullseye = 0 player_score = 0 twenty_close = False bullseye_close = False while twenty_close is False and bullseye_close is False: dart1 = input("Enter dart 1 score: ") if dart1 == 20: twenty += 1 if twenty == 3: twenty_close is True if twenty_close is True and bullseye_full is True: break elif twenty > 3: player_score = player_score + 20 elif dart1 == "BE": bullseye += 1 if bullseye == 3: bullseye_full is True if twenty_close is True and bullseye_close is True: break elif bullseye > 3: player_score = player_score + 50 dart2 = input("Enter dart 2 score: ") if dart2 == 20: twenty += 1 if twenty == 3: twenty_close is True if twenty_close is True and bullseye_close is True: break elif twenty > 3: player_score = player_score + 20 elif dart2 == "BE": bullseye += 1 if bullseye == 3: bullseye_close is True if twenty_close is True and bullseye_close is True: break elif bullseye > 3: player_score = player_score + 50 dart3 = input("Enter dart 3 score: ") if dart3 == 20: twenty += 1 if twenty == 3: twenty_close is True if twenty_close is True and bullseye_full is True: break elif twenty > 3: player_score = player_score + 20 elif dart3 == "BE": bullseye += 1 if bullseye == 3: bullseye_close is True if twenty_close is True and bullseye_close is True: break elif bullseye > 3: player_score = player_score + 50 print("Winner!", player_score) RE: Infinite Looping Issue - SheeppOSU - Jun-15-2020 is cannot be used to assign value, but merely check a value. When you type bullseye is True it will return True if bullseye is true, and False if bullseye is not True. That's the logic behind an if statement. You write a statement like that and it returns true or false in which the if statement can then decide what to do from there. To assign a value you must use the "=" operator. Ex. bullseye_close = True .If I may also add, as this grows, if you continue to use this method, it will only grow more difficult to manage. One way I like to simplify if statements is through dictionaries. This is just an example in pygame, but you should be able to see the concept that I'm showing. keyToString = {pygame.K_l: "l", pygame.K_o: "o", pygame.K_e: "e", pygame.K_c: "c"} for event in pygame.event.get(): #Simply gets all the events (For example a key being pressed) if event == pygame.QUIT: #If a player exits the game sys.exit() if event in keyToString: print("The letter %s was pressed" %keyToString[event])Now imagine if I had 26 different letters and only using if statements vs this method, see how much more efficient it is? Hopefully this helps. |