Home > Net >  Why is pause() being printed after the rock paper scissors game in this code?
Why is pause() being printed after the rock paper scissors game in this code?

Time:12-28

I want pause() to print three times before the rest of the rock, paper, scissors game, y'know, for suspense. Does anyone know of a solution to this?

Many thanks in advance :)

import random
import time

choices = ['rock', 'paper', 'scissors']
result1 = random.choice(choices)
result2 = random.choice(choices)

print('player one has chosen '   result1)
print('player two has chosen '   result2)

def pause(): #To make it super suspenseful.
    time.sleep(1)
    print('.')

for _ in range(3):
    pause()
    
if result1 == result2:
    print('draw!')
    if result1 == choices[0]: #Rock
        if result2 == choices[1]: #Paper
            print('player two wins!')
        elif result2 == choices[2]: #Scissors
            print('player one wins!') 
    elif result1 == choices[1]: #Paper
        if result2 == choices[0]: #Rock
            print('player one wins!')
        elif result2 == choices[2]: #Scissors
            print('player two wins!')
    elif result1 == choices[2]: #Scissors
        if result2 == choices[0]: #Rock
            print('player two wins!')
        elif result2 == choices[1]: #Paper
            print('player one wins')

CodePudding user response:

Your code for the pause works as intended, you have unneeded indentions in your elifs (this was causing your game to not output anything after the suspenseful 3 dots if the game was not a draw):

if result1 == result2:
    print('draw!')

# removed unneeded indentions from lines after here:
if result1 == choices[0]: #Rock
    if result2 == choices[1]: #Paper
        print('player two wins!')
    elif result2 == choices[2]: #Scissors
        print('player one wins!') 
elif result1 == choices[1]: #Paper
    if result2 == choices[0]: #Rock
        print('player one wins!')
    elif result2 == choices[2]: #Scissors
        print('player two wins!')
elif result1 == choices[2]: #Scissors
    if result2 == choices[0]: #Rock
        print('player two wins!')
    elif result2 == choices[1]: #Paper
        print('player one wins')

I removed one layer of indents from your code after your print('draw') so that it runs in the case of a draw not occurring. Making these changes should allow your game to work in all scenarios

CodePudding user response:

I have just realised that the dots were actually in the right place. Sorry and thank you for the answer.

  • Related