Paper-scissors-stick-stone game in Python I would like to condense the code under def game(). I was told through a response to my question on Stack Overflow that I could use dictionary which goes something like this:

mychoice = {1:"paper", 2:"scissors", 3:"stick", r: "stone"}




import random
import time
from getpass import getuser


def my_input():

    #validinput = False
    #while not validinput:
    while True:
        try:
            mychoice = int(input("Your choice? (1. paper, 2. scissor, 3. stick or 4. stone) "))

        except ValueError:
            print('You did not enter a valid input. Enter again.')
            continue

        else:
            if mychoice == 1:
                return 'paper'
            elif mychoice == 2:
                return 'scissor'
            elif mychoice == 3:
                return 'stick'
            elif mychoice == 4:
                return 'stone'
            else:
                print('Invalid choice. Enter again.')


def randomized():
    choice = ['paper', 'scissor', 'stick', 'stone']
    return random.choice(choice)


def game():
    user_continue = True
    while user_continue:
        myinput = my_input()
        print('YOU: ' + str(myinput))
        randomval = randomized()
        time.sleep(1)
        print('COMPUTER: ' + randomval)
        won = 'Congratulations ' + getuser() + '. You won!'
        lost = 'Sorry ' + getuser() + '. You lost.'
        draw = "It's a draw."
        if myinput == 'paper' and randomval == 'scissor':
            time.sleep(1)
            print(lost)
        elif myinput == 'scissor' and randomval == 'paper':
            time.sleep(1)
            print(won)
        elif myinput == 'paper' and randomval == 'stick':
            time.sleep(1)
            print(lost)
        elif myinput == 'stick' and randomval == 'paper':
            time.sleep(1)
            print(won)
        elif myinput == 'paper' and randomval == 'stone':
            time.sleep(1)
            print(won)
        elif myinput == 'stone' and randomval == 'paper':
            time.sleep(1)
            print(lost)
        elif myinput == 'scissor' and randomval == 'stick':
            time.sleep(1)
            print(lost)
        elif myinput == 'stick' and randomval == 'scissor':
            time.sleep(1)
            print(won)
        elif myinput == 'scissor' and randomval == 'stone':
            time.sleep(1)
            print(lost)
        elif myinput == 'stick' and randomval == 'stone':
            time.sleep(1)
            print(lost)
        elif myinput == 'stone' and randomval == 'stick':
            time.sleep(1)
            print(won)
        else:
            print(draw)

        #continue looping until user says no
        user_yn = str.upper(input('Do you want to continue? Press Enter or type any value other than N '))
        if user_yn != 'N':
            print('continuing...')
            continue
        else:
            print('ending...')
            break
game()
So instead of this part of your code

    else:
        if mychoice == 1:
            return 'paper'
        elif mychoice == 2:
            return 'scissor'
        elif mychoice == 3:
            return 'stick'
        elif mychoice == 4:
            return 'stone'
        else:
            print('Invalid choice. Enter again.')


you may first create that recommended dictionary

allowed_choices = {1: "paper", 2: "scissors", 3: "stick", r: "stone"}


and then replace you code with

    else:
        if mychoice in allowed_choices:
            return allowed_choices[mychoice]
        else:
            print('Invalid choice. Enter again.')


The part mychoice in allowed_choices checks only in keys of the allowed_choices dictionary (so it checks if it is 1, 2, 3, or 4).

The part return allowed_choices[mychoice] returns the value of mychoice key (e. g. "stick" for the key 3).