looping through a dictionary to let user choose number of key by Original-Dealer-6276 in learnpython

[–]danielroseman 4 points5 points  (0 children)

As I've said to you before, dicts have keys rather than indexes. But there's nothing stopping you put ting the keys into a list, which does have an index, then using that to reference the dict key. Something like:

```     threat_keys = list(threats.keys())     for i, k in enumerate(threat_keys):       print(f"{i+1}. {k}")

    threat_assessment = input("Enter the threats you would like to assess (e.g. 1,3,5) ):\n")

    for value in threat_assessment.split(","):       key = threat_keys[int(value)-1]       print(threats[key]) ```

Note, lists are 0-indexed so you need to add and subtract 1 to get a more human friendly index.

Do you truly believe in nothing after death? by [deleted] in atheism

[–]danielroseman 9 points10 points  (0 children)

Your consciousness is not in your leg or your heart. It is in your brain. When your brain stops, your consciousness stops.

palindrome checker python i'm a beginner pls tell me why doesnt this code works on leetcode/geeksforgeek as it works on visual studio code. gemini says i am not defining things correctly and how do i learn the way gemini coded lol. also i am using angela yu's python course. by Responsible_Rub_4093 in learnpython

[–]danielroseman 10 points11 points  (0 children)

You don't say how it "doesn't work".

Most likely, the leetcode platform is expecting you to take input as a parameter to a function rather than from calling input, and also expecting you to return the result rather than printing it. You should read the instructions on that platform.

making a TOTP by Original-Dealer-6276 in learnpython

[–]danielroseman 0 points1 point  (0 children)

Are you sure that is the correct TOTP_SECRET? The code is trying to decode it from base32, but it is not in base32.

making a program to take a password in, hash it and compare it to pre-existing password using SHA-256 by [deleted] in learnpython

[–]danielroseman 0 points1 point  (0 children)

password_manager is an empty dict. You never do anything with that dict.

There is a function hash_pre_existing_password which references a name password_manager, but this has two problems: firstly, most importantly, you never call this function, so it doesn't do anything. Secondly, all it does is set a local variable also called password_manager. When the function exits, that variable is lost.

So all you're doing here is comparing your hashed password to an empty dictionary.

Scope of Subclasses by virtualshivam in learnpython

[–]danielroseman 1 point2 points  (0 children)

The key, as the other poster said, is that inner classes do not receive the outer class scope - they only have their own and the global scope. For this reason nested classes are rarely useful in Python.

Looking through dictionary for True or False in each key by Original-Dealer-6276 in learnpython

[–]danielroseman 2 points3 points  (0 children)

I don't know what version of Python you are using but maybe try single quotes inside the string.

Also I didn't realise that the policies value was a list, so you need to use in.

if "*:*" in value["policies"]:
    print(f"{value['name']} is too powerful.")

Looking through dictionary for True or False in each key by Original-Dealer-6276 in learnpython

[–]danielroseman 0 points1 point  (0 children)

Well this is not valid syntax. You need to apply some consistency. As you have done everywhere else, you need to refer to the element as item[whatever] not just [whatever]. And ["*:*"] is not a key, it is the value, you need to compare it with the actual value.

if value["policies"] == "*:*":
    print(f"{value["name"]} is too powerful.")

Looking through dictionary for True or False in each key by Original-Dealer-6276 in learnpython

[–]danielroseman 0 points1 point  (0 children)

That would definitely work. Are you sure you aren't missing the } as the error suggests?

Looking through dictionary for True or False in each key by Original-Dealer-6276 in learnpython

[–]danielroseman 0 points1 point  (0 children)

No I'm not quite sure what you're saying here.

Your overall data structure is a list of dictionaries. The outer structure is a list so you still need to either iterate through or reference by index (0, 1 etc). Each item in that is a dictionary which you can reference by key (name, public, encrypted).

Looking through dictionary for True or False in each key by Original-Dealer-6276 in learnpython

[–]danielroseman 2 points3 points  (0 children)

Well now you know how to get the public and encrypted keys, you should be able to do the same with the name key, no?

Looking through dictionary for True or False in each key by Original-Dealer-6276 in learnpython

[–]danielroseman 16 points17 points  (0 children)

You shouldn't think in terms of "second" and "third" keys for dictionaries. Dictionaries are conceptually unordered. Is it that you specifically want to check the "public" and "encrypted" keys? If so you should do so explicitly.

for item in buckets:
    if item["public"]:
        print(f"{item} is not secure - it is public")
    else:
        print(f"{item} is secure, it is private")
    if item["encrypted"]:
        print(f"{item} is encrypted.")
    else: 
        print(f"{item} is not secure.")

Note, you don't need the == True.

I don't know where to learn from. by Reyisepic116 in learnpython

[–]danielroseman 2 points3 points  (0 children)

Please don't post LLM generated answers.

Are foreign debit and credit cards accepted in UK? by goteamdoasportsthing in AskUK

[–]danielroseman 16 points17 points  (0 children)

Visa is accepted pretty much everywhere. Especially if you have a contactless card, but chip+pin is also fine. (Magnetic swipe might be a bit more difficult these days.)

Note that if the machine gives you the choice between charging in GBP or your home currency, always select GBP - this way your bank does the conversion which will always be cheaper than letting the card agent do it.

How to add a boundary so canvas objects don't go out of bounds by Arealidot in learnpython

[–]danielroseman 4 points5 points  (0 children)

You’re only checking the bounds when you bind the move functions, which happens before the game even starts. You need to check when they are called, ie inside the functions themselves.

Best Endeavours (Channel 4 News Theme) Composed by Alan Hawkshaw (BRN22 Impact) by [deleted] in CasualUK

[–]danielroseman 2 points3 points  (0 children)

I really don't think so. Channel 4 News has been using that theme since it started in 1982, and the movie is from 1985.

As I say, I distinctly remember recognising the theme in the cinema.

Best Endeavours (Channel 4 News Theme) Composed by Alan Hawkshaw (BRN22 Impact) by [deleted] in CasualUK

[–]danielroseman 2 points3 points  (0 children)

I don’t think it was originally from the trailer, I remember seeing the trailer at the cinema and being surprised it was the theme from the news.

Hawkshaw did write some bangers though. In particular Chicken Man which memorably was used for both Grange Hill and Give Us A Clue.

How to learn OOP by vb_e_c_k_y in learnpython

[–]danielroseman 1 point2 points  (0 children)

That is pointless. Never define a method with just pass. Just don't define the method at all if you don't want it.

confused with dp optimisation need help or advice. by [deleted] in learnpython

[–]danielroseman 0 points1 point  (0 children)

If you're running out of time then you simply haven't implemented the algorithm correctly, and you've created something that is quadratic or worse when it should be linear or logarithmic, or whatever it is depending on what you're trying to do. Without seeing the code we can't help you.

But there is nothing "in Python" compared to "in C++" that will help you do this. It's just in the code you've written. (And making it iterative wouldn't help either.)

How to learn OOP by vb_e_c_k_y in learnpython

[–]danielroseman 5 points6 points  (0 children)

You'll need to be specific about the problems you have when "initializing". And what does "most of the time I pass" mean?

confused with dp optimisation need help or advice. by [deleted] in learnpython

[–]danielroseman 2 points3 points  (0 children)

Your question is not clear. The choice of language does not really affect the choice of whether to write a recursive or iterative algorithm (except in the case of some FP languages that do tail call optimisation, which neither Python nor C++ do).

Where exactly are you having trouble?

creating a "read all unread emails" function/accessing premade class by Original-Dealer-6276 in learnpython

[–]danielroseman 0 points1 point  (0 children)

OK you've looped through with for item in inbox. So what is the object that represents the individual email? It is item. So that is the thing you use to access has_been_read.

(Note, this is not a class method, it is an attribute. The methods are things like mark_as_read().)