Python Forum
What should i do, for this code to work -> description
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
What should i do, for this code to work -> description
#1
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
Reply
#2
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
Reply
#3
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Get file description Raysz 2 521 Nov-25-2023, 03:46 PM
Last Post: Raysz
  hi need help to make this code work correctly atulkul1985 5 799 Nov-20-2023, 04:38 PM
Last Post: deanhystad
  newbie question - can't make code work tronic72 2 694 Oct-22-2023, 09:08 PM
Last Post: tronic72
  Beginner: Code not work when longer list raiviscoding 2 826 May-19-2023, 11:19 AM
Last Post: deanhystad
  Why doesn't this code work? What is wrong with path? Melcu54 7 1,804 Jan-29-2023, 06:24 PM
Last Post: Melcu54
  Code used to work 100%, now sometimes works! muzicman0 5 1,447 Jan-13-2023, 05:09 PM
Last Post: muzicman0
  color code doesn't work harryvl 1 896 Dec-29-2022, 08:59 PM
Last Post: deanhystad
  Something the code dont work AlexPython 13 2,265 Oct-17-2022, 08:34 PM
Last Post: AlexPython
  Why is the 'meta description' html tag not translated? Melcu54 2 983 Oct-15-2022, 10:55 PM
Last Post: Larz60+
  cannot get code to work Led_Zeppelin 10 2,464 Jun-30-2022, 06:28 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020