#π problem with try-except removing values from my variables
289 messages Β· Page 1 of 1 (latest)
@versed glacier
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
That's definitely a really bad usage of try:except.
Normally in a try/except we only put the singular line that can throw the error. Not a massive block of code
i asked chatgpt how to solve it
it told me to try try-except
bcs else didnt work
A key part of the Python philosophy is to ask for forgiveness, not permission. This means that it's okay to write code that may produce an error, as long as you specify how that error should be handled. Code written this way is readable and resilient.
try:
number = int(user_input)
except ValueError:
print("failed to convert user_input to a number. setting number to 0.")
number = 0
You should always specify the exception type if it is possible to do so, and your try block should be as short as possible. Attempting to handle broad categories of unexpected exceptions can silently hide serious problems.
try:
number = int(user_input)
item = some_list[number]
except:
print("An exception was raised, but we have no idea if it was a ValueError or an IndexError.")
For more information about exception handling, see the official Python docs, or watch Corey Schafer's video on exception handling.
!paste
If your code is too long to fit in a codeblock in Discord, you can paste your code here:
https://paste.pythondiscord.com/
After pasting your code, save it by clicking the Paste! button in the bottom left, or by pressing CTRL + S. After doing that, you will be navigated to the new paste's page. Copy the URL and post it here so others can see it.
the problem is that i want the code if it doesnt locate "failed_upload" to skip the part of the code and go straight to look for "clue_post"
Please read this embed
You should consider reducing the scope of your try block. The larger it is, the more difficult it will be to determine what the error was.
especially when you use a bare except like you are
yea but i kinda need all the things that are within
otherwise its gonna be useless for me
is there any other way to make it skip the part other than try-except?
use your words.
try:
do_thing()
except SomeError:
failed = True
else:
failed = False
do_something_else()
if failed:
do_if_failed()
i see
but try devalued the variables as well
see the "location_title"
its defined well and it worked before try-except
try didn't "devalue" them. If the variables are undefined, it means an exception was raised before they could be assigned.
oh yeah something like this said chatgpt too
forget about chatgpt for a bit.
well how do i assign them bcs i was told to assign them within the try
but the problem is my code has 23 processes like the one i showed in example
!e ```py
try:
if True:
raise Exception
foo = "bar"
except:
print(foo) # Undefined
like 3000 lines of code
@timber beacon :x: Your 3.12 eval job has completed with return code 1.
001 | Traceback (most recent call last):
002 | File "/home/main.py", line 3, in <module>
003 | raise Exception
004 | Exception
005 |
006 | During handling of the above exception, another exception occurred:
007 |
008 | Traceback (most recent call last):
009 | File "/home/main.py", line 6, in <module>
010 | print(foo) # Undefined
011 | ^^^
... (truncated - too many lines)
Full output: https://paste.pythondiscord.com/I3U6RYYQGMD2HJNQOBPDGC3UKU
NameError: name 'foo' is not defined
so if i wanted to assign them within try
the whole code would have to be within try
no?
Assign them a default value, like None
!e ```py
foo = None
try:
if True:
raise Exception
foo = "bar"
except:
print(foo)
@timber beacon :white_check_mark: Your 3.12 eval job has completed with return code 0.
None
yes i see
and there isnt a way to assign them at the top of the code
bcs the whole code is little bit long
That's what I just did. foo = None
oh i see
so basically just to recap this, i set the value of foo
and in the try if it finds out that the value is different than i set before
it will do something different
that i set
if the value is as i set it before it will continue?
maybe?
wdym maybe
you can check if foo is None if that's what you mean
I would actually suggest wraping everything that might raise in a try/except, setting the default in the except
if i set foo as pizza and then the if is gonna be false bcs (in my script the py auto gui might not find the image) it will print pizza but if it finds the image like in my script it will print bar
ex: ```py
try:
number = int(x)
except ValueError:
number = 0
Gonna be honest with you. Your script (snippet) is way too complex.
i wonder how much of it is chatgpt
yeah....
its for reddit posting
if you had written it on your own and actually learned how it works, it most likely would be far simpler
why not use the API?
yes thats the point bcs i barely understand programming
praw bans new created accs
i cant use praw on new created accs
so i had to come up with different solution
i can use praw on aged ones
it's free for personal use i believe
but if you use praw to post you get shadowban
yes
reddit allows 3 scripts for use and gives you api
You should make a lemmy bot instead.
never heard of this
I don't know what that is.
That's nodejs
ohh you mean platform lemmy
well thats the thing kinda
i need to make bot for reddit
Can you please stop with the monkey gifs?
okay im sorry
so this is the solution?
also i have like 65 variables there
all of them i need to list like that?
wdym refactor
refactor means to change the code to make it better without changing the functionality. This can involve renaming symbols, extracting functions, reorganization, etc
Exactly what is your "bot" going to do?
also, your bot probably doesn't get banned because it uses praw. It probably gets banned because you spam.
nah
i posted one post
BAN
i tried on several accs
same result
What did you post?
post on reddit
some cute dog pic
That's a very generic answer for what is assumed to be a not generic question
well
i cannot go too much into details but
i can say its gonna automate posting into few subreddits to bring more into the reddit community
im just lazy posting myself
so i wanted something that will do it for me
So you're advertizing your community to other subreddits?
nah
i dont have community
im posting images
thats why you see the coordinates
What do you mean "bring more into the reddit community"?
So you're spamming
It could've been your ip got shadow banned
i post posts that other people do
the proxies got shadow banned too
!rule 5
5. Do not provide or request help on projects that may violate terms of service, or that may be deemed inappropriate, malicious, or illegal.
at what point is this illegal?
breaking ToS, ban evasion, etc
If what you're doing is getting your ips banned then clearly they don't want you doing what you're doing
i never got ip banned
I'm saying your proxies got banned
they didnt wdym
Anyway doesn't reddit have an API for posts anyway?
idk what exactly violates ToS bcs if 200 people post into subreddit r/aww cute picture of their dog, its okay, if i do it - its okay but if i create a bot that will do it for me its illegal?
it's not illegal, it's against TOS
We're discussing why he can't use praw, which exposes that api
nah reddit gives you api if you wanna create bot which is allowed
in apps/prefs
(yes)
He basically said "isn't the sky blue?"
yes but this script im writing is not for the accounts that get banned for posting through praw
i was saying that new created accs
get banned
if you post through api
Anyway you can debate this further with <@&831776746206265384> if you like
and also that api cant handle flairs thats why i have this script
wdym
Meaning it violates the rules here. Which you've been told but you seem to disagree so the mods can make a ruling
flairs are for users only (probably)
automating bot posting like this is against TOS
can you show me the rule?
5. Do not provide or request help on projects that may violate terms of service, or that may be deemed inappropriate, malicious, or illegal.
I think he means the reddit tos
no
but i cant create a bot that will slowly post images instead of me doing it?
yes
i agree
thats why im not doing that
but this script is slow
and would do what i do
The api exists to provide you a means of accessing the service programmatically. Accessing it via other means violates their terms
ye but if this bot literally does what i would do
and what im doing
for couple months
the difference is that it's being done programatically with the bot.
and reddit seems to like it
yes
and hows that bad i got things to do
right, that's not you doing it.
family needs to be taken care of
maybe automod is zapping you?
wdym
okay, but that doesn't mean you're doing it in a legit way if you cant do it thru the API.
automod in some communities requires a minimum karma to post.
i have 12k karma
and about 300 post
in your new accounts, that would be 0
yes
thats why im not doing this for new accs
Then why bring this up?
bcs i tried it
So you tried it, your posts got removed for no karma, so you assume reddit was banning praw?
reddit cant tell the difference thats the thing, if its me, or my dog, or my pc reddit cant tell
i know it
praw even lets you change the user-agent
we can, hence rule 5
the only thing they know is that you're using the api.
ye
So there is this in reddits terms:
Access, search, or collect data from the Services by any means (automated or otherwise) except as permitted in these Terms or in a separate agreement with Reddit (we conditionally grant permission to crawl the Services in accordance with the parameters set forth in our robots.txt file, but scraping the Services without Redditβs prior written consent is prohibited)
Under section 7: https://www.redditinc.com/policies/user-agreement
If you aren't using the API, you would not be using the services as permitted
then why api cant handle creating posts
i meant
setting flairs
sry
Probably because they don't want you to do that automatically
You could send a request asking for it to be added
Meanwhile lemmy: "I'm going to send everything I know about everything to you at /inbox. Ready? I'm sending it. I hope your bandwidth is enough"
Otherwise, you would be bypassing their defined methods of interacting with the service
huh
it's technically the api for server-server connections
do you know what "otherwise" means
bcs if its allowed for me to get a VA to do it
It means whatever is most convenient for reddit's lawyers
it doesnt make sense its not allowed for me to get python script to do it
I'm not a reddit lawyer, but it seems pretty clear the project you are seeking help with would potentially violate reddits terms. Therefore we won't provide help with it on this server
ye i get that but what about the VA argument?
We can help with praw, but not pyautogui or selenium
I have no idea what abbreviation you are using
VA - virtual assistant
Wouldn't that fall under automated?
yes
i guess
but this is were it stops making sense the rules
reddit is basically saying if you wanna contribute to our community YOU MUST SPENT YOUR TIME DOING IT
like why
reddit doesnt see the difference
Or use their API appropriately
Because bots don't view ads
Because that's how most communities work
Same reason why we don't want bots on this discord
(unapproved bots)
well i cant tell if im talking to bots 100% and yall cant tell if yall are speakin to one 100%
bad bot
that's enough for r5
yall dont see the point?
you obviously have personal investment on this so of course you'll be biased and refuse to see the point
we literally gave the arguments
Try using praw to post to your /u/ page
you ignored them.
no you don't, you just have biased arguments
literally
okay so: can you tell for 100% who are you speaking to?
almost choked on water on that one, saving it
.bm
I think this has probably run its course. Brad already said we believe this violates the TOS, and Rule 5 says what it says, and Chewbacca is a wooky lives on the planet endor.
Try to find a higher quality one ;/
The Chewbacca defense. Classic
For the rarest of legal moments.
ye but im explaining to yall that the rule doesnt make sense itself and cannot be even used so why to obey it
You're welcome to DM modmail, but let's move on, ok?

naaah thats gonna be too much time
Ok, then I think we're done
i wasnt even supposed to argue this whole convo but it was fun
!close
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.