Command line game
Tags: python, beginners
Published: 2021-10-02 03:40:53

The following is a project of an excellent online course I am taking at codingnomads.com. I am a civil engineer who is a beginner in programing and now I am learning Python. I am in the first course, and as part of one of my assignments I had to do a Command-Line game. The basic instructions of the game is:

•	Ask the player for their name.
•	Display a message that greets them and introduces them to the game world.
•	Present them with a choice between two doors.
•	If they choose the left door, they'll see an empty room.
•	If they choose the right door, then they encounter a dragon.
•	In both cases, they have the option to return to the previous room or interact further.
•	When in the seemingly empty room, they can choose to look around. If they do so, they will find a sword. They can choose to take it or leave it.
•	When encountering the dragon, they have the choice to fight it.
•	If they have the sword from the other room, then they will be able to defeat it and win the game.
•	If they don't have the sword, then they will be eaten by the dragon and lose the game.
So first I propose this code, which was functional but susceptible to errors:
name=input("Hello! thanks for playing, what is your name? ")
sword=False
print(f"Hello {name}! Welcome to the game!")

Win_Game=False

while Win_Game==False:
    choice=input("Please choose between this 2 doors, Left or Right? ")

    if choice=="Left":
        print(f"Al right {name}! you are in an empty room now! ")
        choice_left=input("You want to look around or go back? Yes(look around) or No(Go back)? ")
        if choice_left=="Yes":
            choice_sword=input("Hey, there is a sword there! Do you want to take it before you go back?? Yes or No? ")
            if choice_sword=="Yes":
                sword=True 
                print("Alright, nothing can stop you now, you have a good sword")
            elif choice_sword=="No":
                print("Alright then! Walk without the sword ")
    
    if choice=="Right":
        print(f"My God {name}, there is a dragon here! ")
        choice_right=input("You want to kill the dragon or go back? Yes or No? ")
        if choice_right=="Yes":
            if sword:
                print(f"Hell Yeah {name}! You kill the dragon! ")
                Win_Game=True
                break
            else:
                print("Oh No! You are eaten by the dragon, you lost the game :/ ")
            break
    
if Win_Game:
    print("Hey you won the game!")

quit()

So with the advice of my tutor and friend, he pointed out 3 points I can strength:
1.	In the section in which I make a user-input of “Yes” or “No”, like when I asked if they want to take the sword, if the user don’t put specifically “Yes” or “No”, but writes something else, like “maybe” or anything else, there is no procedure to proceed, which brings a new concept for me of Sanitation of the input. So far I understand, this mean, that you make sure your program get what is supposed to get in terms of format of the input that user will provide. That’s why he suggest an else clause. 
2.	There is not a real need of the break in the while Win_Game==False, since after completing some condition I also included the Win_Game=True, this will break itself the loop, and there is not a real need for the break in the code. This definitely increase my understanding of loops and help me to make simpler code (that eventually leads to easier and maybe better programing).
3.	At the end, I am checking, with an IF to display a “You win the game” message, but as the while loop will only be exited once you win the game, there is not a real need for this checking, 

{
name=input("Hello! thanks for playing, what is your name? ")
sword=False
print(f"Hello {name}! Welcome to the game!")

Win_Game=False

while Win_Game==False:
    choice=str(input("Please choose between this 2 doors, left or right? ")).lower()
    
    if choice=="left":
        print(f"Al right {name}! you are in an empty room now! ")
        choice_left=input("You want to look around or go back? Yes(look around) or No(Go back)? ").lower()
        if choice_left=="yes":
            choice_sword=input("Hey, there is a sword there! Do you want to take it before you go back?? Yes or No? ").lower()
            if choice_sword=="yes":
                sword=True 
                print("Alright, nothing can stop you now, you have a good sword")
            elif choice_sword=="no":
                print("Alright then! Walk without the sword ")
            else:
                print("Well it seems that you are not sure of what to do, you better go back to the 2 doors")
                continue
        elif choice_left=="no":
            continue
        else:
            print("Well it seems that you are not sure of what to do, you better go back to the 2 doors")
    
    if choice=="right":
        print(f"My God {name}, there is a dragon here! ")
        choice_right=input("You want to kill the dragon or go back? Yes or No? ").lower()
        if choice_right=="yes":
            if sword:
                print(f"Hell Yeah {name}! You kill the dragon! ")
                Win_Game=True
                #break
            else:
                print("Oh No! You are eaten by the dragon, you lost the game :/ ")
                quit() #new position to exit the loop and the game
            #break
        elif choice_right=="no":
            print("Alright run back then!")
            continue
        else:
            print("Well it seems that you are not sure of what to do, you better go back to the 2 doors")
print("Hey you won the game!")

}



Making these changes, now the game still works, but can receive answers different than yes or no, and it will not collapse. Thanks to the people that helped me with this small projects of my course, and I am looking forward to keep learning.