how can I make a simple python Russian roulette

Asked

Viewed 487 times

0

I wanted a simple scrip where it counts up to 6 using while or another repeating method. And there’s going to be a list with 5 failed and 1 died and he’s counting to 6 drawing this list and when he talks died wanted it to look like. but in my script it continues talking until completing 6 loops

    def russa(self, message, name_sender, to=''):
     list_morte = ['Morreu','falho','falho','falho','falho','falho',]
     self.post(message='Vamos nós matar hehehe') 
     self.post(message='Go.!') 
     contador =0
     while (contador<6):
       time.sleep(3) #delay de 5 segundos
       self.post(message='/roll')
       list_morte = random.choice(list_morte)
       self.post('/me %s' % list_morte)
     contador =contador+1 
     if list_morte == 'Morreu':
        self.post(message='Morreu')
  • 1

    Could [Edit] the question and add your code?

  • like I said above I don’t know how to make him stop when he talks died and something very simple but useful for a beginner

2 answers

6


If you want the repetition to stop when the "died" value is drawn, just make a conditional structure:

while contador < 6:
    print('atirando...')
    acao = random.choice(list_morte)
    print(acao)
    if acao == 'Morreu':
        break  # Aqui para o laço
    contador += 1

If the number of attempts has been undefined, provided it stops when the action "died", the best would be to put in a loop infinite, using:

while True:
    acao = random.choice(list_morte)
    print(acao)
    if acao == 'Morreu':
        break

Thus, the program will be running until the action "died".

Note: you were overwriting the value of list_morte in making list_morte = random.choice(list_morte), which generated a very different output than expected, so I called acao the return of choice.

  • I just edit the code and test it as you suggest but when I run it loops and returns a a a . and if I don’t put list_death = Random.Choice(list_death) it returns the whole list and not a single item. erro loop

  • @Londarks I need you to fix the indentation of your code in the question to make sure what you did.

  • the code is right I used what’s up there please check again

  • @Londarks if so, your code makes less sense than the initial one, so I recommend you draft a table test of your code and understand what’s wrong.

0

Look, just below, my version of Roleta Russa, using the loop of repetition for.

# Jogo roleta russa

from time import sleep
from random import choice

print('=' * 30)
print(f'\033[34m{f"Roleta Russa":^30}\033[m')
print('=' * 30)

valores = ['Falhou', 'Falhou', 'Falhou', 'Falhou', 'Falhou', 'Morreu']

cont = 0
for c in range(1, 7):
    print('Girando!')
    for i in range(30):
        sleep(0.1)
        print(f'{chr(46)}', end='')
    print()
    x = choice(valores)
    cont += 1
    if x != valores[-1]:
        print(f'\033[32m{cont}º disparo {x}!\033[m')
        if cont == 6:
            print(f'\033[34mVocê é muito Sortudo! Continua Vivo!\033[m')
    else:
        print(f'\033[31m{x}! No {cont}º disparo!\033[m')
        break

Browser other questions tagged

You are not signed in. Login or sign up in order to post.