I don’t understand why my program isn’t working

Asked

Viewed 104 times

0

I was creating a program that defined which was the age of 4 people if she was male, until then I was able to make the codes that define who is older but when I try to define that the person has to be of sex "M" The male program simply gives me 0 as a response, this only occurs when I add the following commands
(and a2 == "M":, and B2 == "M":, and C2 == "M", and D2 == "M":). I’m learning to code in python

    a = str(input("Insira seu nome: "))
    a1 = int(input("Insira a sua idade: "))
    a2 = str(input("Informe seu sexo [M/F]: ")).upper()
    print(" ")
    b = str(input("Insira seu nome: "))
    b1 = int(input("Insira a sua idade: "))
    b2 = str(input("Informe seu sexo [M/F]: ")).upper()
    print(" ")
    c = str(input("Insira seu nome: "))
    c1 = int(input("Insira a sua idade: "))
    c2 = str(input("Informe seu sexo [M/F]: ")).upper()
    print(" ")
    d = str(input("Insira seu nome: "))
    d1 = int(input("Insira a sua idade: "))
    d2 = str(input("Informe seu sexo [M/F]: ")).upper()
    
    maior_idade_homem = 0
    if a1 > b1 and a1 > c1 and a1 > d1 and a2 == "M":
        maior_idade_homem = a
    elif b1 > a1 and b1 > c1 and b1 > d1 and b2 == "M":
        maior_idade_homem = b
    elif c1 > a1 and c1 > b1 and c1 > d1 and c2 == "M":
        maior_idade_homem = c
    elif d1 > a1 and d1 > c1 and b1 > c1 and d2 == "M":
        maior_idade_homem = d
    print("O homem da maior idade é {}".format(maior_idade_homem))
  • 2

    Why the first time you requested sex you used upper() and the other times did just upper, without the parentheses?

  • 1

    It is worth remembering that input returns a string, then do str(input(...)) is unnecessary, just use the input(...) and there, you’ll have the string.

  • As previously mentioned: place upper(). Another thing, your last if that is like elif d1 > a1 and d1 > c1 and d1 > c1 and d2 == "M":... I think it should be elif d1 > a1 and d1 > b1 and d1 > c1 and d2 == "M":. Notice that you used d1 > c1 twice.

  • Those parodies are wrong even Woss, vlw by warning

  • okay hkotsubo, vlw by reminder

  • I checked here Paulo Marques and really was wrong, already corrected but the code continues to 0 as a result

Show 1 more comment

2 answers

0

Hello Gustavo you made a mistake in this line :

  d2 = str(input("Informe seu sexo [M/F]: ")).upper

The right thing would be:

  d2 = str(input("Informe seu sexo [M/F]: ")).upper()

In addition I recommend changing > for >= because in situations where the older age repeats the program will return 0

  • I fixed it but the command keeps giving 0, I don’t know why

  • The older age was repeated ?

  • Nothing is repeated only appears "the man of age is 0" being that it was to appear the name of the man who possesses the greatest age

0


Follows the code "functional":

a = input("Insira seu nome: ")
a1 = int(input("Insira a sua idade: "))
a2 = input("Informe seu sexo [M/F]: ").upper()
print(" ")
b = input("Insira seu nome: ")
b1 = int(input("Insira a sua idade: "))
b2 = input("Informe seu sexo [M/F]: ").upper()
print(" ")
c = input("Insira seu nome: ")
c1 = int(input("Insira a sua idade: "))
c2 = input("Informe seu sexo [M/F]: ").upper()
print(" ")
d = input("Insira seu nome: ")
d1 = int(input("Insira a sua idade: "))
d2 = input("Informe seu sexo [M/F]: ").upper()

maior_idade_homem = 0
if a1 >= b1 and a1 >= c1 and a1 >= d1 and a2 == "M":
    maior_idade_homem = a
elif b1 >= a1 and b1 >= c1 and b1 >= d1 and b2 == "M":
    maior_idade_homem = b
elif c1 >= a1 and c1 >= b1 and c1 >= d1 and c2 == "M":
    maior_idade_homem = c
elif d1 >= a1 and d1 >= b1 and d1 >= c1 and d2 == "M":
    maior_idade_homem = d
print("O homem da maior idade é {}".format(maior_idade_homem))

The problem with your original code was your last elif:

elif d1 > a1 and d1 > c1 and b1 > c1 and d2 == "M":

Note that in the third condition of this elif, you are checking whether b1 > c1, when in fact you should check whether d1 > c1 (most likely happened due to lack of attention/typo).

Note that as suggested by Willer Quintanilha, utilize >= because if two or more people were of equal age, their code would return 0, as it would not enter any if.

However, your code there is still a bug, this logical in contrast to the typing bug you had.

Your program will fail if a sex person F have the greatest age of all people. This bug I will leave to you to resolve, since it is a great logic exercise for you to practice!

  • Okay thanks so much for the help Willer Quintanilha, I will see if I can solve this problem when the "F" has an age. Thanks

  • I’m not him, but I’m glad he helped you. If the answer has corresponded to what you expected, you can accept this answer by clicking on the tick on the side.

  • Okay thanks for warning ihavenoidea

Browser other questions tagged

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