1. Hello!

    First of all, welcome to MapleLegends! You are currently viewing the forums as a guest, so you can only view the first post of every topic. We highly recommend registering so you can be part of our community.

    By registering to our forums you can introduce yourself and make your first friends, talk in the shoutbox, contribute, and much more!

    This process only takes a few minutes and you can always decide to lurk even after!

    - MapleLegends Administration-
  2. Experiencing disconnecting after inserting your login info? Make sure you are on the latest MapleLegends version. The current latest version is found by clicking here.
    Dismiss Notice

Need help with Python program

Discussion in 'Off-Topic' started by Csochapili, Oct 15, 2015.

  1. Csochapili
    Offline

    Csochapili Slime

    24
    22
    1
    Sep 19, 2015
    Female
    9:43 PM
    Csochapili
    Crossbowman
    I started learning Python 3 days ago. I created this rock, paper, scissors program and cannot find anything wrong with it. Everything works apart from when the user inputs how many points they would like to play to, the game doesnt finish when that amount of points is reached by either the user or the computer. Any help? :

    playerscore = 0
    computerscore = 0


    points = input(str("How many points would you like to play to?"))
    print("You: ", playerscore," - ",computerscore," :pC")

    game_finished = False

    while True:

    game_finished = False
    x = input(str("Rock, Paper or Scissors?"))
    x = x.lower()

    if(x != "rock" and x != "paper" and x != "scissors"):
    print("Invalid choice, try again.")
    continue

    else:
    import random
    y = random.randint(0,2)
    if y == 0:
    z = "rock"
    elif y == 1:
    z = "paper"
    elif y == 2:
    z = "scissors"
    print(z)

    if (x == z):
    print("Opponent picked",x,",","It's a tie!")

    elif (x == "rock"):
    if (z == "paper"):
    print("Opponent picked scissors, you win!")
    playerscore = playerscore + 1
    else:
    print("Opponent picked paper, you lose!")
    computerscore = computerscore + 1

    elif (x == "paper"):
    if (z == "rock"):
    print("Opponent picked rock, you win!")
    playerscore = playerscore + 1
    else:
    print("Opponent picked scissors, you lose!")
    computerscore = computerscore + 1

    elif (x == "scissors"):
    if (z == "rock"):
    print("Opponent picked rock, you lose!")
    computerscore = computerscore + 1
    else:
    print("Opponent picked paper, you win!")
    playerscore = playerscore + 1
    print("You: ", playerscore," - ",computerscore," :pC")
    print(playerscore, computerscore, points)


    if (playerscore == points):
    print("Congratulations, you beat the computer!")
    game_finished == True
    break

    elif (computerscore == points):
    print("Unfortunately the computer has beaten you, try again!")
    game_finished == True
    break

    else:
    continue
     
  2. Bulba
    Offline

    Bulba Skelegon Retired Staff

    1,059
    1,035
    433
    Dec 11, 2014
    Male
    Singapore
    4:43 AM
    I don't know Python so I may be wrong, but going by your earlier syntax (and also common usage across languages), the bolded statements looks like it compares the value of game_finished rather than assign a "True" value to it, so game_finished is always "False" and your program never leaves the loop.

    Try replacing with: game_finished = True
     
  3. Poofcakes
    Offline

    Poofcakes Pizzatarian Retired Staff

    1,194
    2,267
    492
    May 8, 2015
    Female
    The Netherlands
    10:43 PM
    You're looping on 'While True', which will always be True, so the loop will never end. You gotta change that condition to make it False when the player wins or loses, and then the loop will end.
     
    • Like Like x 1
  4. Kiran
    Offline

    Kiran Slime

    21
    26
    11
    Sep 6, 2015
    4:43 AM
    Where is the part??
    while not True:
    .....
    ....
    .....
    ...
    End

    I remember when I doing snake game(python 2.7).the structure should included

    While true (when event is still moving)
    While not true (happen in event detected,close)
     
  5. Bulba
    Offline

    Bulba Skelegon Retired Staff

    1,059
    1,035
    433
    Dec 11, 2014
    Male
    Singapore
    4:43 AM
    Also, according to this page and what poof said, you might need to edit your while loop condition to (game_finished == False) first, and then properly assign game_finished to True when the game is over.
     
  6. Redwine
    Offline

    Redwine Capt. Latanica

    301
    356
    278
    Apr 20, 2015
    USA
    4:43 PM
    Redwine
    157
    CIRCUS
    I believe the logic flow is working correctly because CsochapiliCsochapili breaks out correctly when the conditions are met.

    you just need to parse points to type int

    that is,

    points = int(raw_input(str("How many points would you like to play to?")))

    if you notice, when you play the game, the output looks like this:

    Rock, Paper or Scissors?rock
    scissors
    Opponent picked paper, you lose!
    ('You: ', 1, ' - ', 1, ' :pC')
    (1, 1, '3')

    notice the '3' is in quotes, which indicates it is a str type and is not being treated as a number, therefore your (playerscore == points) and (computerscore == points) will never trigger
     
  7. Kiran
    Offline

    Kiran Slime

    21
    26
    11
    Sep 6, 2015
    4:43 AM
    And I found out one more things
    You forget to stated

    point=0 (in the beginning,before raw input???)

    the point will be having a counter
    point=point-1(when user play the game continues)

    If point>=0 : *this need to stated when event exit,else it will repeat
    print (who win)
    exit()

    I assume point as counter

    Just asking...did you arrange your code in the orders of spacing correctly.Cause python are quite messy(I spend one month to fix my buggy code for this term assignment....)
     
    Last edited: Oct 15, 2015
  8. Bulba
    Offline

    Bulba Skelegon Retired Staff

    1,059
    1,035
    433
    Dec 11, 2014
    Male
    Singapore
    4:43 AM
    Correct me if I'm wrong, but points = 0 is not needed since a user-input value is assigned to the variable at the start.

    ... unless Python has some sort of compile/runtime error for uninitialised variables, but since the program can run that shouldn't be a problem.
     
    • Like Like x 1
  9. Kiran
    Offline

    Kiran Slime

    21
    26
    11
    Sep 6, 2015
    4:43 AM
    Well ,it's not really compulsory.Let me check awhile....I might wrong about this.

    Edit: Bulba is correct
     
    Last edited: Oct 15, 2015
  10. Ghost.
    Offline

    Ghost. Nightshadow

    676
    842
    355
    Sep 12, 2015
    Male
    USA
    4:43 PM
    Ghost/Reclaimer
    Night Lord
    200
    Project
    Should be

    game_finished == true

    while(game_finished) <== evaluates to true

    //stuff

    and then game_finished == false inside loop to end the game

    It seems weird to say game_finished == true at the beginning so maybe rename your variable to something like still_playing or something like that
     
    • Like Like x 2
  11. Redwine
    Offline

    Redwine Capt. Latanica

    301
    356
    278
    Apr 20, 2015
    USA
    4:43 PM
    Redwine
    157
    CIRCUS
    I ran the code with my fix and it worked
     
    • Like Like x 1
  12. Ghost.
    Offline

    Ghost. Nightshadow

    676
    842
    355
    Sep 12, 2015
    Male
    USA
    4:43 PM
    Ghost/Reclaimer
    Night Lord
    200
    Project
    Didn't realize you have break statements in your loop until now, disregard my comment lol
     
  13. Poofcakes
    Offline

    Poofcakes Pizzatarian Retired Staff

    1,194
    2,267
    492
    May 8, 2015
    Female
    The Netherlands
    10:43 PM
    You don't have to do it that way, but I guess it's a way to do it. I never learned to do it like that either. :p
    And can confirm that RedwineRedwine's fix works~
     
    • Like Like x 1
  14. Bulba
    Offline

    Bulba Skelegon Retired Staff

    1,059
    1,035
    433
    Dec 11, 2014
    Male
    Singapore
    4:43 AM
    I also prefer using actual conditions for while loops, so it's easier for you (and anyone else who uses your code) to see what your while loop is running for, rather than having to search through your code for the break statements.
     
    • Like Like x 3
  15. OP
    OP
    Csochapili
    Offline

    Csochapili Slime

    24
    22
    1
    Sep 19, 2015
    Female
    9:43 PM
    Csochapili
    Crossbowman
    Mine says name 'raw_int' is not defined
     
  16. Redwine
    Offline

    Redwine Capt. Latanica

    301
    356
    278
    Apr 20, 2015
    USA
    4:43 PM
    Redwine
    157
    CIRCUS
    my apologies, I forgot that I changed "input" to "raw_input"

    because of my python version, I had to use raw_input. change it back to "input"
     
  17. OP
    OP
    Csochapili
    Offline

    Csochapili Slime

    24
    22
    1
    Sep 19, 2015
    Female
    9:43 PM
    Csochapili
    Crossbowman
    Okay thanks a lot! Although I dont know why:

    points = int(input(str("How many points would you like to play to?")))

    works but:

    points = input(str("How many points would you like to play to?"))

    doesnt :S
     
  18. OP
    OP
    Csochapili
    Offline

    Csochapili Slime

    24
    22
    1
    Sep 19, 2015
    Female
    9:43 PM
    Csochapili
    Crossbowman
    Also, how would I write "If points doesnt equal a number print invalid answer, try again"
     
  19. Redwine
    Offline

    Redwine Capt. Latanica

    301
    356
    278
    Apr 20, 2015
    USA
    4:43 PM
    Redwine
    157
    CIRCUS
    I haven't used python since college, but if I had to guess what's probably happening is that the input command converts the user input to a character string. When you program, a 3 is different from a '3'. The first is a number and can be compared with ==, >=, <= with other numerics in the way you expect it to. The second is a character string and cannot be compared (well, it can, but it will compare as a string).

    So when your program stored the user input, because it was stored as a character, all future comparisons with it failed. By making it an int, you allowed it to be compared to other numbers.
     
  20. OP
    OP
    Csochapili
    Offline

    Csochapili Slime

    24
    22
    1
    Sep 19, 2015
    Female
    9:43 PM
    Csochapili
    Crossbowman
    But why is the str still in there?
     
    • Like Like x 1

Share This Page