0

New to python and coded a simple rock, paper scissors game below. Why does the scores show after entering q while running it in visual studio code but this doesn't happen after entering q while opening the file.

import random 

user_wins = 0
computer_wins = 0 

options = ["rock", "paper", "scissors"]

while True: 
    user_input = input("Type Rock/Paper/Scissors or Q to quit: ").lower()
    if user_input == "q":
        break

    if user_input not in options:
        continue

    random_number = random.randint(0, 2)
    # Rock: 0 Paper: 1 Scissors: 2
    computer_pick = options[random_number]
    print("Computer picked", computer_pick + ".")

    if user_input == "rock" and computer_pick == "scissors":
        print("You Won!")
        user_wins += 1 
        
    elif user_input == "paper" and computer_pick == "rock":
        print("You Won!")
        user_wins += 1 
    
    elif user_input == "scissors" and computer_pick == "paper":
        print("You Won!")
        user_wins += 1 

    elif user_input == computer_pick:
        print ("It's a Draw")  

    else:
        print("You lose")
        computer_wins += 1

print("You won", user_wins, "rounds!")
print("The Computer won", computer_wins,"rounds!")
print("Thank you for playing")
Koedlt
  • 4,286
  • 8
  • 15
  • 33
Matthew
  • 3
  • 2
  • What do you mean by "entering q after opening the file"? You launch it via command-line in any case, right? – P.Jo Feb 03 '23 at 13:38
  • more likely it is being run in a terminal (cmd) window on Windows, and it closes imediattely on program end. Just add this line at the end of your program: `input("\nPress to close program")` – jsbueno Feb 03 '23 at 13:40
  • Does this answer your question? [How to keep a Python script output window open?](https://stackoverflow.com/questions/1000900/how-to-keep-a-python-script-output-window-open) – Abdul Aziz Barkat Feb 03 '23 at 13:42

1 Answers1

1

When opening a program as a file, the window will immediately close when the code ends - this means that as soon as q is entered, the program exits the while loop, shows the scores and closes instantly after, leaving no time for you to read it. Visual studio code doesn't have a window to close, leaving the text able to be read.

To fix this, you can add input() at the end of the code, meaning that the user would have to press enter to end the program.

meable
  • 118
  • 8