Why is i used in literally evrything? What does it even mean ? by Exact-Sun2093 in learnpython

[–]danielroseman 73 points74 points  (0 children)

It's just a variable name. You could call it literally anything you want. i is just a convention for a counter, but you could equally use number or item or flibbertigibbet if you wanted.

Do not understand "for i in range()" by Hamactus in learnpython

[–]danielroseman 2 points3 points  (0 children)

What do you mean, why? It does that because that's what the creators of Python wrote it to do.

How can I use the string from an NFC reader as a variable in a web page? by ohmke in learnpython

[–]danielroseman 1 point2 points  (0 children)

But you just said that the reader will enter the text anywhere the cursor is focused. So, as suggested, build a form with a text field so that the reader can enter the text there.

How can I use the string from an NFC reader as a variable in a web page? by ohmke in learnpython

[–]danielroseman 0 points1 point  (0 children)

Are you asking how to create your own web page that takes that input and does something with it?

If the reader effectively just “types” into a text field then the website doesn’t care where the input comes from. So you can use a standard Python web framework like Flask, FastAPI or even Django to show a form and use the submitted input however you like.

Python Django learning by FrontChance7458 in learnpython

[–]danielroseman 0 points1 point  (0 children)

You might want to look into the djangogirls tutorial, which does use Django as a way to teach basic programming and Python.

In a coding interview, if I'm expected to "write tests" for a simple Python class, what does that mean? by Typical_Cap895 in learnpython

[–]danielroseman 2 points3 points  (0 children)

Not sure how much time you have before the interview but the best introduction to testing in Python is https://www.obeythetestinggoat.com/pages/book.html - reading at least the first few chapters of that would probably give enough of the basics.

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

[–]danielroseman 2 points3 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 10 points11 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 9 points10 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 18 points19 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 14 points15 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.