Python Forum

Full Version: What should i do, for this code to work -> description
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
What needs to be fixed? so that the part after else works the same way as the part before it, i.e. a question is asked and an answer is given.
The second part of my question is how to loop the code after that so that when you answer "Nein" the program will restart, and when you answer "Ja" the message_J will appear. Thank you!

This is my code:
question = input('Wie heißt du?')
answer = input('Bist du Frau oder Mann?')

message_N = "GO AGAIN BOYS!!!"
message_E = "Nein"
message_J = "Too bad, lmao"
message_f = f"Sehr geehrte Frau {question},\nIch grüße dich!"
message_m = f"Sehr geehrter Herr {question}, \nIch grüße dich!"


if question == str():
    print(answer)
if answer == "Mann":
    print(message_m)
elif answer == "Frau":
    print(message_f)
else:
    answer == input('Bist du jemanden anderer?')
    print(answer)
if answer == "Ja":
    print(message_J)
elif answer == "Nein":
    print(message_N)**


This is my output:
Output:
C:\Users\zahar\PycharmProjects\Shrek\venv\Scripts\python.exe C:/Users/zahar/PycharmProjects/Shrek/name.py Wie heißt du?asd Bist du Frau oder Mann?asd Bist du jemanden anderer?Ja asd Process finished with exit code 0
I am not sure if I understand what you want. But perhaps this helps.
message_N = "GO AGAIN BOYS!!!"
message_E = "Nein"
message_J = "Too bad, lmao"
repeat = True

while repeat:
    repeat = False
    question = input('Wie heißt du?')
    answer = input('Bist du Frau oder Mann?')
    message_f = f"Sehr geehrte Frau {question},\nIch grüße dich!"
    message_m = f"Sehr geehrter Herr {question}, \nIch grüße dich!"
    if question == str():
        print(answer)
    if answer == "Mann":
        print(message_m)
    elif answer == "Frau":
        print(message_f)
    else:
        answer = input('Bist du jemanden anderer?')
        print(answer)
        if answer == "Ja":
            print(message_J)
        elif answer == "Nein":
            print(message_N)
            repeat = True
It is not exactly clear what you are trying to do from your code and your explanation does not help much either but:

This does not look right

You can loop using a while loop:
if question == str():
    print(answer)
If you want to check that the answer contains some content, you can do:

if question is not None:
Your variables are not named correctly which confuses people while looking at your code. It is obviously the below are ridiculously named.

question = input('Wie heißt du?')
answer = input('Bist du Frau oder Mann?')
which should probably be:

name = input('Wie heißt du?')
gender = input('Bist du Frau oder Mann?')
Lastly, you can loop with a while loop but fixing the above would probably be a good place to start.

while answer != message_J