Death Note in Python - Loop Sentinel of Death

Asked

Viewed 213 times

4

I have a question in my code of Death Note where I can’t create a loop to ask if the notebook user wants to kill someone else and the code should be closed when the user no longer wants to kill someone. Someone there gives me a force I swear not to write the name on Death Note.

Observing: In the anime Light Yagami when it does not define the death of the person that person dies automatically from cardiac arrest and in the if morte == ' ' does not enter this condition, what would be the problem?

usuario = str (input("Digite o nome do usuário do caderno: "))
print ("Bem vindo ao Death Note {}".format(usuario))
print ("Deseja matar alguém? ")
resposta = str (input("S - Sim | N - Não: "))
if resposta == 'S' or resposta == 's':
    nome = str (input("Digite o nome da vítima: "))
    morte = str (input("Digite a causa da morte: "))
    if morte == ' ': # Não funciona essa condição
        print ("O nome da vítima é {}".format(nome))
        print ("E morreu de uma parada cardíaca!")
    else:
        print ("O nome da vítima é {}".format(nome))
        print ("Causa da morte: {}".format(morte))

print ("Deseja matar mais alguém {}?".format(usuario))    '''
outramorte = str (input("S - Sim | N - Não: "))      Loop sentinela
                                                          '''
else:
    print ("{} volte quando quiser para limpar o mundo!!!".format(usuario))
  • 1

    First I think the suicide check is nome == usuario, Knowing this you can solve the Obs 2 putting a if before the print checking if the nome!=usuario so he just gives print if the last name is different, another solution is you create another variable to save the type of the last death, another thing: when it is suicide you may also have to skip the resposta = (input... and put as N =)

  • 1

    @Icaromartins I got a little lost

  • 1

    Hello @alex, I believe original problem (Death Note in Python - Loop Sentinel of Death) was solved by one of the answers below. Note that the last editions made the answers lose context, the ideal now would be you revert to (s) edition(s) (accept the answer that helped you in the original problem) and create a new question and if you need to add context to the new question add a link to this. = D

  • 2

    I still don’t understand the solution even with the answers below, some of them really helped me in some details of my code but the focus of the problems I edited still not because suddenly I wanted to implement the code thinking of some that made sense similar to Death Note in the anime.

  • 1

    In other words, I thought about some improvements in the code and I didn’t understand how you make these improvements in coding.

  • I reversed your post because it invalidates the answers given. Please read [tour] and [help] to better understand the website model.

Show 1 more comment

2 answers

10


Your code does not have a loop, just a check with if. In order for it to return to itself, we must apply a loop. More precisely a while loop. The loop while is a type of loop that repeats until the given condition is false.

But before we apply, we must pay attention to a detail of your question:

Note: In the anime Light Yagami when it does not define the death of the person that person dies automatically from cardiac arrest and in the if morte == ' ' does not enter this condition, what would be the problem?

That one if morte == ' ' does not check if the string is empty, but if the person entered a space, to fix this just type if morte == '' in place.

Also, if we are dealing here with Python 3.x (which is highly recommended to use) it is not necessary to add these str() since the return of Python 3.x input is already string type. In addition to making the code more readable, it decreases the use of processing. And if it’s Python 2, we have the function raw_input() as the correct one in this case.

How can I now insert the while loop?

To implement the while loop, simply rename if for while once we want the code to return again to that starting point. Once done, we rename the variable otherness for reply, so when to return for the while check (resposta == 'S' or resposta == 's') the value of reply will have changed by now.

That is, if the answer is different from "S" or "s", it will simply stop the loop and run the next line, which is the bye-bye to the guys.

This would be the final code:

usuario = input("Digite o nome do usuário do caderno: ")
print ("Bem vindo ao Death Note {}".format(usuario))
print ("Deseja matar alguém? ")
resposta = input("S - Sim: N - Não: ")
while resposta == 'S' or resposta == 's':
    nome = input("Digite o nome da vítima: ")
    morte = input("Digite a causa da morte: ")
    if morte == '':
        print ("O nome da vítima é {}".format(nome))
        print ("E morreu de uma parada cardíaca!")
    else:
        print ("O nome da vítima é {}".format(nome))
        print ("Causa da morte: {}".format(morte))

    print ("Deseja matar mais alguém {}?".format(usuario))
    resposta = input("S - Sim: N - Não: ")

print ("{} volte quando quiser para limpar o mundo!!!".format(usuario))

By the way, this anime is excellent.

  • 1

    I will implement the code by inserting a condition that if the user enters his own name he commits suicide and the code ends, I hope it works.

  • 1

    My implementation worked, only when it falls into two conditions of suicide (when he puts his own name and cause of death and when he puts his own name and does not justify the cause of death) also appears the print ("{} volte quando quiser para limpar o mundo!!!".format(usuario)). How’s the guy gonna clean up the world again if he killed himself.

  • 2

    Already fixed? Basically create a variable suicidio worthwhile False , when it is suicide, just before the break change this value to True. At the end you add if suicidio: [...] else: [...]

  • 2

    But first read @Icaromartins' comment. The first observation is interesting.

3

The code was almost right.

The condition if morte == '': didn’t work because it was double-spaced.

I removed the conversion of input because the return is already a string.

I added the while to loop whenever user type s indifferent if it is in high box because the method lower will convert to low cash.

The method title uppercase the first character .

resposta = 's'

while resposta.lower() == 's':

    usuario = input("\nDigite o nome do usuário do caderno: ")

    print("Bem vindo ao Death Note {}".format(usuario.title()))
    print("\nDeseja matar alguém? ")

    resposta = input("S - Sim | N - Não: ")

    if resposta.lower() == 's':

        nome = input("\nDigite o nome da vítima: ")
        morte = input("Digite a causa da morte: ")

        if morte == '':
            print ("\nO nome da vítima é {}".format(nome.title()))
            print ("E morreu de uma parada cardíaca!")
        else:
            print ("\nO nome da vítima é {}".format(nome.title()))
            print ("Causa da morte: {}".format(morte))

print ("\n{} volte quando quiser para limpar o mundo!!!".format(usuario.title())

I kept the first code so you can compare the changes I implemented with your "note 2 specification".

Follows the new version:

resposta = 's'

while resposta.lower() == 's':

    usuario = input("\nDigite o nome do usuário do caderno: ")

    print("Bem vindo ao Death Note {}".format(usuario.title()))
    print("\nDeseja matar alguém? ")

    resposta = input("S - Sim | N - Não: ")

    if resposta.lower() == 's':

        nome = input("\nDigite o nome da vítima: ")
        morte = input("Digite a causa da morte: ")

        if morte == '' and nome != usuario:
            print ("\nO nome da vítima é {}".format(nome.title()))
            print ("E morreu de uma parada cardíaca!")

        elif morte != '' and nome != usuario:
            print ("\nO nome da vítima é {}".format(nome.title()))
            print ("Causa da morte: {}".format(morte))

        elif nome == usuario:
            print ("\n{} você digitou seu próprio nome no Death Note!".format(nome.title()))
            break

        else:
            print ("\n{} volte quando quiser para limpar o mundo!!!".format(usuario.title())) 
  • 1

    Hey man I edited the question that arose an implementation of mine that arose an unforeseen there

  • 1

    Alex, compare the two codes in my reply. The change is in the conditions of the "if" commands and the addition of the "break" in the suicide.

Browser other questions tagged

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