Skip to content

Adding week 1 assignment#2

Open
szhan223 wants to merge 3 commits intomasterfrom
szhan223-week1
Open

Adding week 1 assignment#2
szhan223 wants to merge 3 commits intomasterfrom
szhan223-week1

Conversation

@szhan223
Copy link
Collaborator

@szhan223 szhan223 commented Aug 2, 2021

No description provided.

else:
pass

#try:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

always remove unused code

needle = 0

# Check whether the first two inputs are numbers and the last input is a sign
try:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note: lets learn how to use functions to organize code

Copy link
Owner

Choose a reason for hiding this comment

The 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

print ("Intern - Shu Wei")
print ("")

eArray = list(map(str, input("Please enter your input with spaces between them: ").split()))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need to call str your input split will already be strings

Copy link
Owner

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

eArray = list(map(str, input("Please enter your input with spaces between them: ").split()))

#Definitions
aCount = (len(eArray))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

outer parens is useless


#Definitions
aCount = (len(eArray))
fCount = 3
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you need more descriptive variable names.

checkN = int(eArray[CheckNCount-1])
except ValueError:
print ("Please make sure that the input #", CheckNCount, "is a number")
exit()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exit are extremely bad practice. avoid at all cost

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What should I use instead to make it stop running?

Copy link
Owner

Choose a reason for hiding this comment

The 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

exit()
CheckNCount = CheckNCount + 2
else:
pass
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

an else with pass is useless, you can remove both

else:
pass

if aCount >= 3:
Copy link
Owner

Choose a reason for hiding this comment

The 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

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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?

# exit()

# Calculation
import operator
Copy link
Owner

Choose a reason for hiding this comment

The 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

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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.

"/" : operator.truediv,
}

result1 = ops[eArray[fCount-1]](int(eArray[0]), int(eArray[1]))
Copy link
Owner

Choose a reason for hiding this comment

The 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

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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.

Copy link
Collaborator Author

@szhan223 szhan223 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Additional comments

aCount = (len(eArray))
fCount = 3
nCount = 4
needle = 0
Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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()))
Yes and I changed int to str :P

exit()

if aCount > 3:
CheckNCount = 4
Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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"

exit()

if aCount > 3:
CheckNCount = 4
Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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


if aCount > 3:
CheckNCount = 4
while (CheckNCount <= aCount):
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Originally, I tried to do it with "for" via:
checkNCount = 4, checkNCount < aCount, checkNCount++, but it didn't work

CheckNCount = 4
while (CheckNCount <= aCount):
try:
checkN = int(eArray[CheckNCount-1])
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh so the conversion stays?

# exit()

# Calculation
import operator
Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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.

Copy link
Owner

@delfu delfu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hangman is pretty good. decent structure,

you should enclose most of the code into a function and at the top level just call that function. But this will cause issues with box but it just means your pattern with box is not great (i left comments)

@@ -0,0 +1,45 @@
# Problem: the game is currently case-sensitive. A != a
import getpass
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

whats getpass?

import getpass

# Split the keyword into characters and put them into an array
def split(keyword):
Copy link
Owner

Choose a reason for hiding this comment

The 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

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if anything i think list(keyword) will return an array of chars


# Identify the number of guesses allowed
def minimum(answer):
if len(answer) < 5:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the whole thing can be max(len(answer) + 2, 5)

def check_answer(guess):
for index, letter in enumerate(answer):
if letter == guess:
box[index] = guess
Copy link
Owner

Choose a reason for hiding this comment

The 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")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use + instead of , . i think , prints extra spacing and looks weird


# Create a box to showcase P2 the empty places
box = []
while len(box) < len(answer):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can create box like so box = ["_" for i in range(len(answer))]

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.

print ("Player 2 wins! The answer is: ", " ".join(answer))
quit()
else:
number_of_guesses_left = number_of_guesses_left - 1
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can do number_of_guesses_left -= 1 does the same thing

quit()
else:
number_of_guesses_left = number_of_guesses_left - 1
else:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

avoid using else with a while. it's a python only thing.

instead just do a check for if number_of_gueses_left <= 0 and player dies

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

Comments