Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Infinite Looping Issue
#2
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.
Reply


Messages In This Thread
Infinite Looping Issue - by nsadams87xx - Jun-14-2020, 11:58 PM
RE: Infinite Looping Issue - by SheeppOSU - Jun-15-2020, 12:13 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  looping and indentation issue ameydiwanji 3 2,461 Jul-01-2019, 10:53 AM
Last Post: perfringo
  Looping issue, stops working JohnOsis 7 4,464 Apr-05-2018, 09:20 PM
Last Post: nilamo
  Looping issue JohnOsis 2 2,941 Apr-01-2018, 08:56 AM
Last Post: JohnOsis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020