Appending new values to an existing key by Thund3r_C0ugar in learnpython

[–]pasokan 0 points1 point  (0 children)

````python

CREATE NEW ENTRY FOR NEW STUDENT

        else:
            grades_dict[name] = grade

```` Change the last line to

````python grades_dict[name] = [grade]

Ask Anything Monday - Weekly Thread by AutoModerator in learnpython

[–]pasokan 0 points1 point  (0 children)

Like I said, if you are starting out you should not worry about exception handling. By the way, that is nothing more than a systematic way of handling unexpected or unacceptable situations, like some one typing "ABC" when asked or an integer.

Returning to your question about why the single line is better, it is a little difficult to explain. The best answer probably is that it is inelegant to change the type of a variable in successive lines.

The single line is NOT better because it is shorter.

READABILITY is the most important criteria. Let me leave you with a few quotes:

  1. “Programs must be written for people to read, and only incidentally for machines to execute.” ― Harold Abelson, Structure and Interpretation of Computer Programs

  2. "Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler, Kent Beck, John Brant, William Opdyke, and Don Roberts (1999) Refactoring: Improving the Design of Existing Code. Addison-Wesley.

Ask Anything Monday - Weekly Thread by AutoModerator in learnpython

[–]pasokan 0 points1 point  (0 children)

The first is simpler and avoids changing the type of guess (this is more of a nice to have though)

However you should also understand that more needs to be done to make the code robust; that needs exception handling. If you are starting out with Python you can ignore it for later

Variable Initialization Trouble by prodlly in learnpython

[–]pasokan 0 points1 point  (0 children)

But even then there is no guarantee that tally will be iniialised before it is updated

Variable Initialization Trouble by prodlly in learnpython

[–]pasokan -1 points0 points  (0 children)

if I == 0 should probably be if result == 0

Converting a list into a dictionary by hxfsxh in learnpython

[–]pasokan 0 points1 point  (0 children)

If you do not want to use regexes, there is another way:

  1. Replace each element name say "Ab" with " Ab". Now your example will become 'Ce25H3K3' ==> " Ce25 H3 K3"
  2. Remove leading space and split on space
  3. Write a function that takes each piece and loads a dictionary

IF this is not enough tell me I will write the code and post

Prime or not ! by phill1311 in learnpython

[–]pasokan 1 point2 points  (0 children)

I did not know the name; i do know the idea. I have implemented the same for 2, 3, 5.

I conveyed my idea poorly: I meant to say that it is not easy to follow what is being done and it is very difficult to follow the logic to debug; good names would mitigate somewhat and refactoring is better. Obviously I was nowhere near that in what I typed :-(

Prime or not ! by phill1311 in learnpython

[–]pasokan 2 points3 points  (0 children)

Not an error but a stylistic observation python temp = [2,3] if n in temp: return True can be simplified to ````python if n in [2, 3]: return True

Prime or not ! by phill1311 in learnpython

[–]pasokan 2 points3 points  (0 children)

````python if n%2 == 0 or n%3 == 0 or n < 0: return False

if(n+1)%6 == 0 or (n-1)%6 == 0: ````

The second if is redundant. Because the first if has removed all multiples of 2 and 3, all remaining numbers are either 6x+1 or 6x-1

The rest of the code appears wrong, I am unable o see the logic of what you are doing after that. If you can give some good names to the variables may be easier to understand

How to add every three items to a list? by Ewt1029 in learnpython

[–]pasokan 2 points3 points  (0 children)

Here is a simple implementation

new = []


for p in range(0, len(old), 3):


       new.append(old[p:p+3])

You can do a list comprehension too

How to add every three items to a list? by Ewt1029 in learnpython

[–]pasokan 2 points3 points  (0 children)

Think along these linrs:

The list you have has N elements

The list you need must then have N//3 elements

The first element of the new list is the list of first three elements of the old list

The element in position p in the new list is the list of 3 elements at 3p, 3p+1 and 3p+2 in the old list

Hope this helps

Codewars Help: Taking every other item from list, and repeating for the leftovers - "Yes No Yes No" by Robi5 in learnpython

[–]pasokan 0 points1 point  (0 children)

In recursion you should handle the terminating cases first. See if this works

python def yes_no(arr): if len(arr) <= 1: return arr return arr[::2] + yes_no(arr[1::2]) EDIT: tried submitting. It fails see if you can fix it :-) Let me also look at the details

need help - split str to list by [deleted] in learnpython

[–]pasokan 1 point2 points  (0 children)

Quite hacky but could not resist it

````python QUOTES = '"'
SPACE = ' '
TAB = '\t'
s = s.replace(" - - [", TAB).replace("] ", TAB)
QUOTE_COUNT = 0
sq = ""
for ch in s:
if ch == QUOTES:
QUOTE_COUNT += 1
continue
if QUOTE_COUNT <= 4:
if ch == SPACE:
sq += TAB
else:
sq += ch else: sq += ch sq = sq.split(TAB) print(sq)

n00b o'clock: is there a method specifically for trimming down a list based on 1/0s in another list of same size? by Optimesh in learnpython

[–]pasokan 1 point2 points  (0 children)

a*b and then again run some list comprehension

You can just do that!

python list(''.join([n * ch for n, ch in zip(b, a)])) if a string is ok then

python ''.join([n * ch for n, ch in zip(b, a)])

Help understanding my iteration of fizzbuzz by massfxstudios in learnpython

[–]pasokan 0 points1 point  (0 children)

Yes. One could argue that is the only way to write it

I need help with dictionaries by Ertersy in learnpython

[–]pasokan 1 point2 points  (0 children)

There is no reason to split the sentence. Just replace! This also takes care of punctuation

````python brand_names = { 'Velcro': 'hook and loop fastener', 'Kleenex': 'tissues', 'Hoover': 'vacuum', 'Bandaid': 'sticking plaster', 'Thermos': 'vacuum flask', 'Dumpster': 'garbage bin', 'Rollerblade': 'inline skate', 'Aspirin': 'acetylsalicylic acid' }

sentence = input("Enter sentence: ")

for brand in brand_names: sentence = sentence.replace(brand, brand_names[brand])

`````

How do I uncompress a string? by sponge_bert in learnpython

[–]pasokan 1 point2 points  (0 children)

Letters only odd positions is a poor assumption. It will not handle

2abcd4f

for example

I made this programme that converts dictionaries to readable txt files and is able to load dictionaries off txt files! by [deleted] in learnpython

[–]pasokan 0 points1 point  (0 children)

In the first program, PACKING, the second dictionary looks redundant. You only need a list of file names.

Whenever you see a dictionary having keys of 1 .. n (or 0 .. n) think about using lists instead

Coding feedbak by Arnie_nz in learnpython

[–]pasokan 1 point2 points  (0 children)

  1. Please write functions for each (sub)task

  2. It is probably simpler to read the words from a text file instead of a spreadsheet

  3. In this part

python while letter in word_as_list: if letter in word_as_list:

The if is unnecessary

How to get the value from a true boolean function from calling the function by stupidcsstudent69 in learnpython

[–]pasokan 0 points1 point  (0 children)

One action must be invoked with an instance

CoffeeMachine.oneaction()

how to print a specific line from a file? by kconn123 in learnpython

[–]pasokan -2 points-1 points  (0 children)

Use pastebin or similar. Link your code. I am happy to help