Skip to content

Instantly share code, notes, and snippets.

@nichotined
Created September 10, 2019 05:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nichotined/ec6865e86f9c12e9caeeddae9aec49f0 to your computer and use it in GitHub Desktop.
Save nichotined/ec6865e86f9c12e9caeeddae9aec49f0 to your computer and use it in GitHub Desktop.

Programming Class

[[TOC]]

Week 3

Conditional If, Elif, Else

There may be times when you want to have a result

Looping

Iterator

Iterable

For

A for loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a set, or a string).

While

fruits = ["banana", "apple", "orange", "kiwi"]
while(len(fruits) > 1):
    fruits.pop()
print(fruits)

Week 3 Assignment

  1. Say you have a list of numbers. Many of them are duplicate, create a script to eliminate the duplication. Your program behavior should exactly the same as below:

    $ python3 duplicate.py
    Give me a list of numbers: [1,2,3,2,3,4,5,1]
    Okay!!
    Here you are: [1,2,3,4,5]
    
  2. Generate a random number between 1 to 9. Ask the user to guess the number then answer whether it's too high, too low or the number is right.

  3. Say there is a robot in a point of location (0,0). The robot can move toward UP, DOWN, LEFT and RIGHT with given steps. The trace of robot movement is shown as the following:

    UP 5
    DOWN 3
    LEFT 3
    RIGHT 2
    

    The numbers after the direction are steps. Please write a program to compute the distance from the position after the movement and the original point. If the distance is a float, then just print the nearest integer. The output of your program should be:

    Robot location is 2, 1
    Distance is 2
    

    Hint:
    Distance is round of square of y power of 2 plus x power of 2

  4. Say you are playing a game of rock-paper-scissors with the computer.
    Remember rock beats scissors, paper beats rock, scissors beats paper and if it's the same then it's a draw.
    If you quit the game, there will be a score printed between you versus the computer.
    Please write a program for this game.

  5. Say you are going to write a code for a game to guess a word. The features are explained below:

    1. When the game start, your program will ask for a character
    2. User can guess for many times but only 5 times given for the wrong character guessed. More than 5, they are losing. 
    3. If they are giving a right character, let them know they are right.
    4. If the user already guesses the character, don't penalize them, ask them to guess again.
    5. If the user wins or loses, let them start a new game or quit.
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment