Conversation
| else: | ||
| pass | ||
|
|
||
| #try: |
| needle = 0 | ||
|
|
||
| # Check whether the first two inputs are numbers and the last input is a sign | ||
| try: |
There was a problem hiding this comment.
note: lets learn how to use functions to organize code
There was a problem hiding this comment.
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())) |
There was a problem hiding this comment.
no need to call str your input split will already be strings
There was a problem hiding this comment.
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)) |
|
|
||
| #Definitions | ||
| aCount = (len(eArray)) | ||
| fCount = 3 |
There was a problem hiding this comment.
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() |
There was a problem hiding this comment.
exit are extremely bad practice. avoid at all cost
There was a problem hiding this comment.
What should I use instead to make it stop running?
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
an else with pass is useless, you can remove both
| else: | ||
| pass | ||
|
|
||
| if aCount >= 3: |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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])) |
There was a problem hiding this comment.
what makes aCount <= 4 special? you can totally fold this line into the while loop and avoid the if/else all together
There was a problem hiding this comment.
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.
szhan223
left a comment
There was a problem hiding this comment.
Additional comments
| aCount = (len(eArray)) | ||
| fCount = 3 | ||
| nCount = 4 | ||
| needle = 0 |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
check n count is just to check whether numbers are inputted at places where numbers are "allowed"
| exit() | ||
|
|
||
| if aCount > 3: | ||
| CheckNCount = 4 |
There was a problem hiding this comment.
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): |
There was a problem hiding this comment.
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]) |
There was a problem hiding this comment.
oh so the conversion stays?
| # exit() | ||
|
|
||
| # Calculation | ||
| import operator |
There was a problem hiding this comment.
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.
delfu
left a comment
There was a problem hiding this comment.
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 | |||
| import getpass | ||
|
|
||
| # Split the keyword into characters and put them into an array | ||
| def split(keyword): |
There was a problem hiding this comment.
usually there's no need to split a string into chars. it's already essentially a array of chars
There was a problem hiding this comment.
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: |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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") |
There was a problem hiding this comment.
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): |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
you can do number_of_guesses_left -= 1 does the same thing
| quit() | ||
| else: | ||
| number_of_guesses_left = number_of_guesses_left - 1 | ||
| else: |
There was a problem hiding this comment.
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
No description provided.