-
Notifications
You must be signed in to change notification settings - Fork 0
Adding week 1 assignment #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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)) | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. outer parens is useless |
||
| fCount = 3 | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you need more descriptive variable names. |
||
| nCount = 4 | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. python variable names are usually snake cases snake_case_variable iDioTcAsEVariabLE
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Question. If a variable is being called in a very frequent basis, wouldn't it be bad to have a long name since you'll need to type that long name every time? |
||
| needle = 0 | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. lol ima assume you copied this code from somewhere
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. list(map(str, input("Please enter your input with spaces between them: ").split())) |
||
|
|
||
| # Check whether the first two inputs are numbers and the last input is a sign | ||
| try: | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. note: lets learn how to use functions to organize code
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we dont need to check for validity. for all assignments we'll assume well formed inputs |
||
| 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 | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. as pointed out above, python variable names are snake_cased. so this will be
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. check n count is just to check whether numbers are inputted at places where numbers are "allowed"
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same thing, check s count is to check whether the operators are inputted at places where operators are allowed |
||
| while (CheckNCount <= aCount): | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. a
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if anything, in general, you should prefer
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Originally, I tried to do it with "for" via: |
||
| try: | ||
| checkN = int(eArray[CheckNCount-1]) | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. since you were doing validation earlier on
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oh so the conversion stays? |
||
| except ValueError: | ||
| print ("Please make sure that the input #", CheckNCount, "is a number") | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. read up on template strings (extra curricular) |
||
| exit() | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What should I use instead to make it stop running?
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This whole thing should be in a function and you can return from the function. Or if you're expecting the validation to work, you can just throw an error and let the program crash |
||
| CheckNCount = CheckNCount + 2 | ||
| else: | ||
| pass | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. an else with pass is useless, you can remove both |
||
|
|
||
| if aCount >= 3: | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i see what you're doing. this is not good. Your code will only be able to handle 2 operations. What if you need to handle > 2 ? Part 2 of the assignment was hoping you'd arrive at a generic solution
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I coded it a way that it can handle n operations. What's stopping it from running more than 2 operations? |
||
| 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: | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. always remove unused code |
||
| # 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 | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. usually, in almost all languages, you'd import at the beginning. just a best of practice. Python lets you do it wherever, and there are cases where you'd have it inside some function but usually, imports at the top
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. by the way, I have no idea what import does. I was trying to find a way to convert the text (+,-,*,/) into operations and this was one of the solution. |
||
| ops = { | ||
| "+" : operator.add, | ||
| "-" : operator.sub, | ||
| "*" : operator.mul, | ||
| "/" : operator.truediv, | ||
| } | ||
|
|
||
| result1 = ops[eArray[fCount-1]](int(eArray[0]), int(eArray[1])) | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what makes aCount <= 4 special? you can totally fold this line into the while loop and avoid the if/else all together
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the problem that I saw is that in the array, item 1 and 2 are number, then afterwards, the order is even = number, odd = operator. I split the calculation in two parts because of this. It was originally <= 3, but for some reasons it doesn't work, and <=4 works. |
||
| 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) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| # Problem: the game is currently case-sensitive. A != a | ||
| import getpass | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. whats getpass? |
||
|
|
||
| # Split the keyword into characters and put them into an array | ||
| def split(keyword): | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. usually there's no need to split a string into chars. it's already essentially a array of chars
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if anything i think |
||
| return [char for char in keyword] | ||
|
|
||
| # Identify the number of guesses allowed | ||
| def minimum(answer): | ||
| if len(answer) < 5: | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the whole thing can be |
||
| 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 | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok this works but is a pattern we want to avoid. either pass in box as an argument or dont use box at all. The reason is your function is now accessing a variable that's outside its scope and can easily create bugs |
||
|
|
||
| # 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") | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use |
||
|
|
||
| # Create a box to showcase P2 the empty places | ||
| box = [] | ||
| while len(box) < len(answer): | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you can create box like so this means, you are creating box to be an array and you're initializing its values to be "_" for each position in a range of 0 to length of 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 | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you can do |
||
| else: | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. avoid using instead just do a check for |
||
| print ("You ran out of guesses! Player 1 wins! The answer is: ", " ".join(answer)) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no need to call
stryour input split will already be stringsThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
in python you probably dont need to call list. there are only a few cases you need to call list after a map. we might cover that later