This is my code:

print('Test Program') print("This is my proga!") name = input("Ваше имя: ") print(name, ", добро пожаловать в темный мир.") answer = 'End' while answer != 'End': answer = input("Ты бы хотел взять какое-нибудь оружие? (Y/N/End): ") if answer1 == "Y": print("1-Пулемёт") print("2-Гранатомет") print("3-Однозарядный пистолет") print("4-АА-12") answer2 = input("Ваш ответ:") if answer2 == '1' or answer2 == '2': print("Хороший выбор.") elif answer2 == '3': print("Плохой выбор, возьми другое.") else: print("Великолепный выбор.") if answer == 'N': print("Ты пацифист? Круто!") else: print("Ладно, подумай ещё.") 

but it does not work as I would like. The fact is that after entering the name, the code immediately finishes the work, as if this is all written. I want to do this: when pressing Y or N, the code will work fine, that is, press Y or N, select the answer and that's it. And when you press "End" would be the inscription "Okay, think again," and the program would start with the question "Would you like to take some kind of weapon?". So you can do?

  • And what value will have answer at the first check of a condition? - Mikhail Vaysman

2 answers 2

 print('Test Program') print("This is my proga!") name = input("Ваше имя: ") print(name, ", добро пожаловать в темный мир.") answer = '' while answer != 'End': answer = input("Ты бы хотел взять какое-нибудь оружие? (Y/N/End): ") if answer == "Y": print("1-Пулемёт") print("2-Гранатомет") print("3-Однозарядный пистолет") print("4-АА-12") answer1 = input("Ваш ответ:") if answer1 == '1' : print('Well done') if answer1 == '2': print('Not bad') else: print('You will dead') if answer =='N': print('Ты пацифист?') if answer == 'End': print('Think again') 

View the answer. Did not go to while

    Note:

     answer = 'End' while answer != 'End': 

    The condition of the cycle is not initially fulfilled (because answer == 'End' , then the condition answer != 'End' false), so the input to the cycle does not occur.

    To enter the cycle, you need to change the initial value of the answer to any other.

    • Inserted, instead of my version, your version, well, that while 'End'! = 'End' :. Nothing changed. - Mih666
    • then, exit one, tie with programming - vadim vaduxa
    • @ Mih666, I expanded the explanation a bit in response. I hope, now it became clearer. - insolor