2
\$\begingroup\$

I'm 13 years old and turning 14 years soon! I Coded this simple text game in around 30 minutes, can someone rate it? and give some feedback

import random
import time
current_coin = 500
def play():
    computer2 = random.choice(['r','p','s'])
    computer1 = random.choice(['r','p','s'])
    if computer2 == computer1:
        return 'tie'
    if is_win(computer2, computer1):
        return 'computer2 wins!!'
    if is_lose(computer2, computer1):
        return 'computer1 wins!!'
    else:
        return 'it looks like you made a typo! try again'

def is_win(computer2, computer1):
    if (computer2 == "r" and computer1 == "s") or (computer2 == "s" and computer1 == "p") or (computer2 == "p" and computer1 == 'r'):
        return True

def is_lose(computer2, computer1):
    if (computer2 == "s" and computer1 == "r") or (computer2 == "p" and computer1 == "s") or (computer2 == "r" and computer1 == 'p'):
        return True

while True:
    print('welcome to rock paper scissors betting simulator!\n')
    time.sleep(1)
    print('1 = computer1\n')
    time.sleep(1)
    print('2 = computer2\n')
    time.sleep(1)
    beton = str(input('please type 1 or 2 to bet!    \n'))
    coinbet = str(input('you currently have ' + str(int(current_coin)) + ' coins! how much would you like to bet!!\n'))
    if int(str(coinbet)) > int(current_coin):
        print('you dont have that much?\n')
        coinbet = str(input('you currently have ' + str(int(current_coin)) + ' coins! how much would you like to bet!!\n'))
    else:
        print('you betted ' + str(int(coinbet)) + ' !')
    result = play()
    time.sleep(0.1)
    if result == 'tie':
        print('welp! they tied! you have ' + str(int(current_coin)) + ' left!!\n')
    if result == 'computer2 wins!!':
        print('welp, since computer2 won. . .\n')
        if beton == '1':
            print('you lost! you now have ' + str(int(current_coin) - int(coinbet)) + '!!')
            current_coin = current_coin - int(coinbet)
        if beton == '2':
            print('you won! you now have ' + str(int(current_coin) + int(coinbet)) + '!!')
            current_coin = current_coin + int(coinbet)
    if result == 'computer1 wins!!':
        print('welp, since computer1 won. . .\n')
        if beton == '2':
            print('you lost! you now have ' + str(int(current_coin) - int(coinbet)) + '!!')
            current_coin = current_coin - int(coinbet)
        if beton == '1':
            print('you won! you now have ' + str(int(current_coin) + int(coinbet)) + '!!')
            current_coin = current_coin + int(coinbet)      
    if current_coin <= 0:
        print('you have no coins left! what a loser!')
        break
\$\endgroup\$

2 Answers 2

3
\$\begingroup\$

Good start! I like that is_win() and is_lose() are separate functions. They could be improved by returning the boolean directly. In general, if you have code that looks like if condition: return True else: Return False, it can be written as return condition directly.

Also, is_lose() can be written as is_win() with the parameters swapped

def is_win(computer2, computer1):
    return (
        (computer2 == "r" and computer1 == "s") or 
        (computer2 == "s" and computer1 == "p") or 
        (computer2 == "p" and computer1 == 'r')
    )

def is_lose(computer2, computer1):
    return is_win(computer1,computer2)

This breaks if the user enters an invalid amount the second time.

beton = str(input('please type 1 or 2 to bet!    \n'))
coinbet = str(input('you currently have ' + str(int(current_coin)) + ' coins! how much would you like to bet!!\n'))
if int(str(coinbet)) > int(current_coin):
    print('you dont have that much?\n')
    coinbet = str(input('you currently have ' + str(int(current_coin)) + ' coins! how much would you like to bet!!\n'))
else:
    print('you betted ' + str(int(coinbet)) + ' !')

This would not be an issue if it was written like so:

coin_bet = None
while coin_bet == None:
    coin_bet_attempt = int(input('you currently have ' + str(int(current_coin)) + ' coins! how much would you like to bet!!\n'))
    if coin_bet_attempt > current_coin:
        print("You don't have that much?")
    else:
        coin_bet = coin_bet_attempt
print('you betted ' + str(coin_bet) + ' !')

Better yet, one could write a function that ONLY exits when a valid input occurs.

def get_coin_bet_from_user(current_coin):
    while True:
        bet = int(input('you currently have ' + str(int(current_coin)) + ' coins! how much would you like to bet!!\n'))
        if bet > current_coin:
            print("You don't have that much?")
        else:
            return bet

# called like so
coinbet = get_coin_bet_from_user(current_coin)
\$\endgroup\$
2
\$\begingroup\$

Your

def is_win(computer2, computer1):
    if (computer2 == "r" and computer1 == "s") or (computer2 == "s" and computer1 == "p") or (computer2 == "p" and computer1 == 'r'):
        return True

could be shorter (and always return a bool, instead of True or None):

def is_win(computer2, computer1):
    return (computer2, computer1) in [('r', 's'), ('s', 'p'), ('p', 'r')]

or

def is_win(computer2, computer1):
    return computer2 + computer1 in ['rs', 'sp', 'pr']

or

def is_win(computer2, computer1):
    return computer2 + computer1 in 'rspr'

Not sure why you have this:

    else:
        return 'it looks like you made a typo! try again'

How is that ever supposed to be reached? Both computer2 and computer1 are random.choice(['r','p','s']), which doesn't make typos.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.