Home > Mobile >  How would I make it so that whenever the tie conditional is met, it loops and grabs a new entry from
How would I make it so that whenever the tie conditional is met, it loops and grabs a new entry from

Time:09-30

For my final project in unit 2 of my python class, we had to make a relatively simple rock, paper, and scissors game. I really enjoyed the class and wanted to get extra credit for it, so I made it more complicated with functions and whatnot. My issue is that whenever the condition for tying is met, what I want my code to do is loop back to where it asks you to play again, and get a new selection from random.choice([x, x, x, etc.]). How would I implement this?

import random
import time

rps = random.choice(['rock', 'paper', 'scissors', 'lizard', 'spock'])
def win_cond():
    print('i choose...')
    time.sleep(0.9)
    print(rps)
    time.sleep(1)
    print('I WIN!!!')
    time.sleep(2)
    print('nerd!')
    play()


def lose_cond():
    print('i choose....')
    time.sleep(0.9)
    print(rps)
    time.sleep(1)
    print('GAH!!! I LOSE!!!')
    play()

def play():
    play = input('you wanna play!? yes or no / y or n: ')
    if (play == 'yes' or 'y'):
        game()
    else:
        print('okay lol')


def game():
    rpsU = input('welcome to the game ong!! choose rock, paper, scissors, lizard, or spock! ').lower()
    rps = random.choice(['rock', 'paper', 'scissors', 'lizard', 'spock'])
    print(rps)
        
    if (rpsU == 'rock' and (rps == 'paper' or rps == 'spock')):
        win_cond()
    elif (rpsU == 'rock' and 'rps' == 'rock'):
        print('tie,,')
        play()
    elif (rpsU == 'rock' and (rps == 'paper' or rps == 'spock')):
        lose_cond()
    
    
    if (rpsU == 'spock' and (rps == 'lizard' or rps == 'paper')):
        win_cond()
    elif (rpsU == 'spock' and rps == 'spock'):
        print('tie,,')
        play()
    elif (rpsU == 'spock' and (rps == 'scissors' or 'rock')):
        lose_cond()
    
    if (rpsU == 'scissors' and (rps == 'spock' or 'rock')):
        win_cond()
    elif (rpsU == 'scissors' and rps == 'scissors'):
        print('tie,,')
        play()
    elif (rpsU == 'scissors' and (rps == 'paper' or 'lizard')):
        lose_cond()
    
    if (rpsU == 'paper' and (rps == 'lizard' or 'scissors')):
        win_cond()
    elif (rpsU == 'paper' and rps == 'paper'):
        print('tie,,')
        play()
    elif(rpsU == 'paper' and (rps == 'spock' or 'rock')):
        lose_cond()

    if (rpsU == 'lizard' and (rps == 'rock' or 'scissors')):
        win_cond()
    elif (rpsU == 'lizard' and rps == 'lizard'):
        print('tie,,')
        play()
    elif (rpsU == 'lizard' and (rps == 'spock' or 'paper')):
        lose_cond()

    if (rpsU != 'rock' and rpsU != 'paper' and rpsU != 'scissors' and rpsU != 'lizard' and rpsU != 'spock'):
        print('what the freakazoids!?')
        
        
play()

CodePudding user response:

Ok, if i understand correctly, you want the game to take a new random value every time, but the problem here is that you have a global variable rps that is the same as the local variable in game, you should delete the global variable, and leave only the one in game, and use it as a parameter on win_cond(rps) and lose_cond(rps). Now, note that the program is not complete, it doesnt cover all the possible combinations, for example, what happens if i choose rock and the computer chooses scissors?

Heres the code without the minor fixes:

import random
import time


def win_cond(rps):
    print('i choose...')
    time.sleep(0.9)
    print(rps)
    time.sleep(1)
    print('I WIN!!!')
    time.sleep(2)
    print('nerd!')
    play()


def lose_cond(rps):
    print('i choose....')
    time.sleep(0.9)
    print(rps)
    time.sleep(1)
    print('GAH!!! I LOSE!!!')
    play()

def play():
    play = input('you wanna play!? yes or no / y or n: ')
    if (play == 'yes' or 'y'):
        game()
    else:
        print('okay lol')


def game():
    rpsU = input('welcome to the game ong!! choose rock, paper, scissors, lizard, or spock! ').lower()
    rps = random.choice(['rock', 'paper', 'scissors', 'lizard', 'spock'])
    print(rps)
        
    if (rpsU == 'rock' and (rps == 'paper' or rps == 'spock')):
        win_cond(rps)
    elif (rpsU == 'rock' and 'rps' == 'rock'):
        print('tie,,')
        play()
    elif (rpsU == 'rock' and (rps == 'paper' or rps == 'spock')):
        lose_cond(rps)
    
    
    if (rpsU == 'spock' and (rps == 'lizard' or rps == 'paper')):
        win_cond(rps)
    elif (rpsU == 'spock' and rps == 'spock'):
        print('tie,,')
        play()
    elif (rpsU == 'spock' and (rps == 'scissors' or 'rock')):
        lose_cond(rps)
    
    if (rpsU == 'scissors' and (rps == 'spock' or 'rock')):
        win_cond(rps)
    elif (rpsU == 'scissors' and rps == 'scissors'):
        print('tie,,')
        play()
    elif (rpsU == 'scissors' and (rps == 'paper' or 'lizard')):
        lose_cond(rps)
    
    if (rpsU == 'paper' and (rps == 'lizard' or 'scissors')):
        win_cond(rps)
    elif (rpsU == 'paper' and rps == 'paper'):
        print('tie,,')
        play()
    elif(rpsU == 'paper' and (rps == 'spock' or 'rock')):
        lose_cond(rps)

    if (rpsU == 'lizard' and (rps == 'rock' or 'scissors')):
        win_cond(rps)
    elif (rpsU == 'lizard' and rps == 'lizard'):
        print('tie,,')
        play()
    elif (rpsU == 'lizard' and (rps == 'spock' or 'paper')):
        lose_cond(rps)

    if (rpsU != 'rock' and rpsU != 'paper' and rpsU != 'scissors' and rpsU != 'lizard' and rpsU != 'spock'):
        print('what the freakazoids!?')
        
        
play()
  • Related