#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    6
    Rep Power
    0

    Complete beginner - trying to understand my mistake. 3.3.2.


    Code:
    # This program is my first Python program.
    
    print('Hello mysterious life form!')
    
    print('What is your gender? Male, female or other?')
    
    playerGenderInput = input()
    
    if playerGenderInput == 'male' or 'Male.':
        playerGender == 'male'
    elif playerGenderInput == 'female' or 'Female.':
        playerGender == 'female'
    else:
        playerGender == 'unknown' + print ('I did not quite get that. Not to worry though.')
            
    if playerGender == 'male':
        print('It is good to meet you sir... oh, I did not quite catch your name. It is..?')
    elif playerGender == 'female':
        print('It is good to meet you madam... oh, I did not quite catch your name. It is..?')
    else playerGender == 'unknown':
        print('It is good to meet you, whatever you are... oh, I did not quite catch your name. It is..?')
    
    playerName = input()
    
    print('Ahh, ' + myName)
    
    print('Have a nice day!')
    Running it results in:

    File "(the file's location)", line 20
    else playerGender == 'unknown':
    (a '^' below the 'r' in 'Gender')

    SyntaxError: invalid syntax
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    114
    Rep Power
    3
    That else should be an elif.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    194
    Rep Power
    2
    You seem to be confusing "==" with "=". Use "==" when testing equality, and use "=" when assigning a value.
    python Code:
    # This program is my first Python program.
    print('Hello mysterious life form!')
    print('What is your gender? Male, female or other?')
     
    player_gender = input('What is your gender? Male, female or other?').lower()
     
    if player_gender not in ("male","female"):
        print('I did not quite get that. Not to worry though.')
        player_gender = 'unknown'
     
    if player_gender == 'male':
        print('It is good to meet you sir... oh, I did not quite catch your name. It is..?')
    elif player_gender == 'female':
        print('It is good to meet you madam... oh, I did not quite catch your name. It is..?')
    else: 
        print('It is good to meet you, whatever you are... oh, I did not quite catch your name. It is..?')
     
    my_name = input()
     
    print('Ahh, {}'.format(my_name))
    print('Have a nice day!')

    -Mek
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    6
    Rep Power
    0
    Thanks

    Got it working:

    Code:
    # This program is my first Python program.
    
    print('Hello mysterious life form!')
    
    playerGender = input('What is your gender? Male, female or other? ').lower()
    
    if playerGender not in ("male","female"):
        print('I did not quite get that. Not to worry though.')
        playerGender = 'unknown'
    
    if playerGender == 'male':
        print('It is good to meet you sir... oh, I did not catch your name. It is..?')
    elif playerGender == 'female':
        print('It is good to meet you madam... oh, I did not quite catch your name. It is..?')
    else:
        print('It is good to meet you, whatever you are... oh, I did not quite catch your name. It is..?')
    
    playerName = input()
    
    print('Ahh, {}'.format(playerName))
    print('Have a nice day!')
    One more question for now: How does one keep it from closing after you enter your name?
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    100
    Rep Power
    2
    Originally Posted by ClutchHunter
    One more question for now: How does one keep it from closing after you enter your name?
    Can you be more specific, please? Closing what?
    After your code asks for someone's name, and when the user presses enter, the program doesn't end.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    194
    Rep Power
    2
    Originally Posted by ClutchHunter
    One more question for now: How does one keep it from closing after you enter your name?
    Just throw another input statement at the end.
    Code:
    input("\nPlease press enter to close.")
    -Mek
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    6
    Rep Power
    0
    So I'm trying to make my own little program. Much testing has led me to the conclusion that the bit which is supposed to give the compass letter a random number between one and four doesn't work. How would one make it work?

    Code:
    import random
    import time
    
    def displayIntro():
        print()
        print()
        print('---------------------------------')
        print()
        print()
        print('Hello there!')
        print()
        print('This is the first program I have made in Python away from any tutorial.')
        print()
    
    def chooseDirection():
        direction = ''
        while direction != 'n' and direction != 's' and direction != 'e' and direction != 'w':
            print('Which direction on the compass do you wish to go? There is a 25% chance of treasure and a 75% chance of death... (n, s, e, w)')
            direction = input()
    
        return direction
    
    def checkDirection(chosenDirection):
    
        n = random.randint(1, 4)
        s = random.randint(1, 4)
        e = random.randint(1, 4)
        w = random.randint(1, 4)
    
        treasureDirection = random.randint(1, 4)
    
        if chosenDirection == str(treasureDirection):
            print('Hurray! You found some treasure.')
        else:
            print('You were met by a group of bandits and murdered for what little you had. Better luck next time!')
    
    playAgain = 'yes'
    while playAgain == 'yes' or playAgain == 'y':
    
        displayIntro()
    
        directionCompass = chooseDirection()
    
        checkDirection(directionCompass)
    
        print('Do you want to play again? (yes or no)')
        playAgain = input()
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    194
    Rep Power
    2
    Might get you on the right track (code is python 3):
    python Code:
    import random
    import time
     
    OPTIONS = ('n','s','e','w')
     
    def display_intro():
        print("\n")
        print("---------------------------------")
        print("\n")
        print("Hello there!\n")
        print("This is the first program I have made in Python away from any tutorial.\n")
     
    def choose_direction():
        direction = ""
        while direction not in OPTIONS:
            print("Which direction on the compass do you wish to go?\n"
                  "There is a 25% chance of treasure and a 75% chance of death...")
            direction = input("(N, S, E, W): ").lower()
        return direction
     
    def check_direction(chosen_direction):
        treasure_direction = random.choice(OPTIONS)
        if chosen_direction == treasure_direction:
            print('Hurray! You found some treasure.')
        else:
            print(chosen_direction,treasure_direction)
            print("You were met by a group of bandits and murdered for what little"
                  "you had.\nBetter luck next time!")
     
    def main():
        play_again = 'yes'
        while play_again in ('yes','y'):
            display_intro()
            direction_compass = choose_direction()
            check_direction(direction_compass)
            play_again = input("Do you want to play again? (yes or no): ")
     
    if __name__ == "__main__":
        main()

    -Mek

IMN logo majestic logo threadwatch logo seochat tools logo