diff --git a/1/assignment-week1.py b/1/assignment-week1.py new file mode 100644 index 0000000..555327e --- /dev/null +++ b/1/assignment-week1.py @@ -0,0 +1,83 @@ +print ("Week 1 Assignment - Math Equation Evaluator") +print ("Intern - Shu Wei") +print ("") + +eArray = list(map(str, input("Please enter your input with spaces between them: ").split())) + +#Definitions +aCount = (len(eArray)) +fCount = 3 +nCount = 4 +needle = 0 + +# Check whether the first two inputs are numbers and the last input is a sign +try: + check1 = int(eArray[0]) +except ValueError: + print("Please make sure that the input #1 is a number") + exit() +try: + check2 = int(eArray[1]) +except ValueError: + print("Please make sure that the input #2 input is a number") + exit() + +if aCount > 3: + CheckNCount = 4 + while (CheckNCount <= aCount): + try: + checkN = int(eArray[CheckNCount-1]) + except ValueError: + print ("Please make sure that the input #", CheckNCount, "is a number") + exit() + CheckNCount = CheckNCount + 2 +else: + pass + +if aCount >= 3: + CheckSCount = 3 + while (CheckSCount <= aCount): + try: + checkS = int(eArray[CheckSCount-1]) + except ValueError: + if eArray[CheckSCount-1] == "+" or eArray[CheckSCount-1] == "-" or eArray[CheckSCount-1] == "*" or eArray[CheckSCount-1] == "/": + pass + else: + print("Invalid input #", CheckSCount) + else: + print ("Please make sure that the input #", CheckSCount, "is a sign") + exit () + CheckSCount = CheckSCount + 2 +else: + pass + +#try: +# check3 = int(eArray[aCount-1]) +#except ValueError: +# if eArray[aCount-1] == "+" or eArray[aCount-1] == "-" or eArray[aCount-1] == "*" or eArray[aCount-1] == "/": +# pass +# else: +# print("Invalid last sign") +#else: +# print("Please make sure that the last input is a sign") +# exit() + +# Calculation +import operator +ops = { + "+" : operator.add, + "-" : operator.sub, + "*" : operator.mul, + "/" : operator.truediv, +} + +result1 = ops[eArray[fCount-1]](int(eArray[0]), int(eArray[1])) +if aCount > 4: + while (fCount < aCount): + fCount = fCount + 2 + result1 = ops[eArray[fCount-1]](result1, int(eArray[nCount-1])) + nCount = nCount + 2 + print (result1) +else: + print (result1) + diff --git a/1/fibonacci.py b/1/fibonacci.py new file mode 100644 index 0000000..bfd0c97 --- /dev/null +++ b/1/fibonacci.py @@ -0,0 +1,14 @@ +number_of_characters = 20 +stack = [] +stack.append(int(0)) +stack.append(int(1)) + +def fibonacci_function(number): #Je comprends toujours pas c'est quoi que je dois mettre dans le "()"; si je met rien ça marche pas; je peux mettre n'importe quoi et ça va marcher + current_stack_size = int(len(stack)) + new_number = stack[current_stack_size - 1] + stack[current_stack_size - 2] + stack.append(int(new_number)) + +while len(stack) < number_of_characters: + fibonacci_function(number_of_characters) + +print (stack) \ No newline at end of file diff --git a/1/hangman.py b/1/hangman.py new file mode 100644 index 0000000..639e32f --- /dev/null +++ b/1/hangman.py @@ -0,0 +1,45 @@ +# Problem: the game is currently case-sensitive. A != a +import getpass + +# Split the keyword into characters and put them into an array +def split(keyword): + return [char for char in keyword] + +# Identify the number of guesses allowed +def minimum(answer): + if len(answer) < 5: + return 5 + else: + return len(answer) + 2 + +# Check if the guessed letter matches something in the array, if so, replace the empty space in box +def check_answer(guess): + for index, letter in enumerate(answer): + if letter == guess: + box[index] = guess + +# Ask P1 to input a string for P2 to guess +keyword = getpass.getpass("Player 1: Please enter the word you want Player 2 to guess: ") +answer = split(keyword) +print ("Player 2: There are ",len(answer)," characters you need to guess") + +# Create a box to showcase P2 the empty places +box = [] +while len(box) < len(answer): + box.append("_") +print (box) + +# Loop for the main game +number_of_guesses_left = int(minimum(answer)) +while number_of_guesses_left > 0: + print ("You have ",number_of_guesses_left," chance left to guess. Please guess a character:") + guess = input() + check_answer(guess) + print (box) + if answer == box: + print ("Player 2 wins! The answer is: ", " ".join(answer)) + quit() + else: + number_of_guesses_left = number_of_guesses_left - 1 +else: + print ("You ran out of guesses! Player 1 wins! The answer is: ", " ".join(answer))