0

i have began python this year and am creating a simple Rock Paper Scissors game with integrated score keeper. The program is causing an undesirable loop (repeating the main menu portion.) i am 98% sure this is caused by an improper indentation somewhere in the loop but i am just stumped. run the code and see if you can find my mistake. thank you!!!

    import random as rand

computer_score = 0
user_score = 0
print("""
==========================
Rock, Paper, or Scissors?
==========================
""")

# while loop to perform finite loop as long as specific perameters are met
while True:
    option = ["Rock", "Paper", "Scissors"]
    a = rand.choice(option)
    user_input = input("""Choose one:
    1) Rock
    2) Paper
    3) Scissors

    press 'Q' to quit game

    Enter your option: """)

# line 28,29) to make sure user_input string is a digit and to convert string to an integer
# line 30) since indexing always starts at 0, if user inputs "rock (1)"...
# [user_input - 1] will make sure "Rock" is selected in list(option[0])
# line 30,31) if user_input is the same as random choice (a): user/computer_score will not change
    if user_input.isdigit == True:
        user_input = int(user_input)
        if option[user_input - 1] == a:
            computer_score, user_score = computer_score, user_score
            print("its a tie!")
            print("computer: ", a, "user: ", user_input)
            print("computer score: ", computer_score,
                  "     user score: ", user_score)
            print("\n")

         # line 39,40,41)if user_input is rock and random choice (a) is paper: computer_score will add 1
        elif option[user_input - 1] == "Rock":
            if a == "Paper":
                computer_score = computer_score + 1
                print("computer wins!")
                print("computer: ", a, "user: ", user_input)
                print("computer score: ", computer_score,
                      "     user score: ", user_score)
                print("\n")

            elif a == "Scissors":
                user_score = user_score + 1
                print("you win!")
                print("computer: ", a, "user: ", user_input)
                print("computer score: ", computer_score,
                      "     user score: ", user_score)
                print("\n")

        elif option[user_input - 1] == "Paper":
            if a == "Rock":
                user_score = user_score + 1
                print("you win!")
                print("computer: ", a, "user: ", user_input)
                print("computer scoore: ", computer_score,
                      "     user score: ", user_score)
                print("\n")
            elif a == "Scissors":
                computer_score = computer_score + 1
                print("computer wins")
                print("computer: ", a, "user: ", user_input)
                print("computer_score: ", computer_score,
                      "     user score: ", user_score)
                print("\n")

        elif option[user_input - 1] == "Scissors":
            if a == "Rock":
                computer_score = computer_score + 1
                print("computer wins")
                print("computer: ", a, "user: ", user_input)
                print("computer score: ", computer_score,
                      "     user score: ", user_score)
                print("\n")
            elif a == "Paper":
                user_score = user_score + 1
                print("You win!")
                print("computer: ", a, "user: ", user_input)
                print("computer score: ", computer_score,
                      "     user score: ", user_score)
                print("\n")


        elif user_input.isdigit() == False:
            if str(user_input) == "q" or "Q":
                print("final score: \n")
                print("computer score: ", computer_score,
                      "     user score: ", user_score)
            else:
                print("invalid input")
  • Does this answer your question? [How can I read inputs as numbers?](https://stackoverflow.com/questions/20449427/how-can-i-read-inputs-as-numbers) – G. Anderson Feb 06 '20 at 20:46
  • I think you are missing a statement to exit the loop – Edeki Okoh Feb 06 '20 at 20:46
  • Your first `if` is never true, and you don't have a case to handle that situation – G. Anderson Feb 06 '20 at 20:47
  • @G.Anderson ah you are right! I switched '''if user_input.isdigit == True:``` with ``` if True:``` and the program continued to work normally. I am just a little confused because to my (wrong) understanding, when i called the .isdigit method, I thought that since i am inputting a digit, and converting the string to an integer in the line below that, it would be true. But now i am thinking if my 1st user_input is causing something strange since i have not converted that input to integer? I will keep playing with this thank you for the input – LuxuryCondo Feb 06 '20 at 21:38
  • You're taking the user input _above_ that line first and not casting it to int. The initial value is set before the condition is checked for the first time. – G. Anderson Feb 06 '20 at 21:39
  • yes! I got it thank you – LuxuryCondo Feb 06 '20 at 21:55

0 Answers0