Ask coding questions

← Back to all posts
[Python] Convert None to int
Bookie0 (5956)

Hi!

I'm back (from this). How do you convert a variable from None to int? I first defined a variable to None because I need something like in js: var variableName; I need to declare a variable but not put any value in it.

(This is because I'm using a database (repl.it DB) and I'm using variables inside, but I need to define those variables and consequently put a value in them. But each time I run the repl, the variable resets).

So that's why, after searching on google how to use var variableName in python (if you have a different solution please do tell), I found out that I should use variable_name = None.

This shouldn't give it any value, and would also not give it a data type.

Unfortunately, I then use that said variable (I increment it), but I get this error:

Thank you for any help, lmk if you need more details.

Thanks, have a good day! :)

edit:, yes like @19wintersp says:

I'd probably describe None as a placeholder value.

Answered by 19wintersp (1121) [earned 5 cycles]
View Answer
Comments
hotnewtop
19wintersp (1121)

What do you mean? In JavaScript, that's like asking to convert null to a number - you can change it to another value, but not convert it*. If you want to convert Nones to zeros, you can use this:

intValue = int(someValue) if someValue != None else 0

I don't really understand your question.

19wintersp (1121)

Also, just looking at what you said:

This shouldn't give it any value, and would also not give it a data type.

I think you misunderstand; None is a type. It needs to be converted just like any other object. It represents the absence of a value, yes, but it still has a "value" itself. Try this:

print(type(None))

You should get (with some extra bits) "NoneType".

I need something like in js: var variableName

I think Python initialises to None. It might be more similar to undefined in that sense? I'd probably describe None as a placeholder value.

Bookie0 (5956)

@19wintersp oh hum okay.

yea sorry, kinda hard to explain. but basically, I have a database using variables. Those variables have to be defined. To define them I have to give them a value. If I run the program (not the repl I linked below, that will give an error), it'll work fine. But when I click stop and run it again, it'll run through the program again, and then when going through the code that defines the variable, it'll be reset. This will mess the values inside the DB.

Bookie0 (5956)

@19wintersp Ah ok, None is a type. And yes, I am trying to find a placeholder, without anything inside it though.

19wintersp (1121)

@Bookie0 Can I just confirm: you want to create a variable without a value, and then treat it as an integer?

Bookie0 (5956)

@19wintersp yeaaa I know it's not really well explained (probably not even possible), but yea the aim is to create the variable without a value, so that each time I run the code, it doesn't get set back to that initial value.

19wintersp (1121)

@Bookie0 You want to preserve data in an ephemeral memory-stored variable between executions??

Bookie0 (5956)

@19wintersp Now that you say it, it sounds wrong lol..

but yea that's my goal, seems like it isn't a very good idea. Any suggestions (preferably using repl.it DB)?

19wintersp (1121)

@Bookie0 Why not just dump and load any data you want, maybe with a dictionary?

from replit import db

state = db["state"]

# do stuff, modifying state in the process...

db["state"] = state

It's not great, but you would set up your database to have "state" set to "{}". state would be a dictionary for you to modify.

Bookie0 (5956)

@19wintersp Which I think takes me back to my main question here: https://repl.it/talk/ask/Python-Replit-DB-variables-not-updating-when-program-is-run-again/125459

If you could, could you look at my code (I added comments and specified the lines)

Thanks! :D

19wintersp (1121)

@Bookie0 Rather than resetting them at the start, only set them if the user asks to create an account?

Bookie0 (5956)

@19wintersp And uh btw the code might not be very efficient/pretty, so please lmk if you have any questions! :D

Bookie0 (5956)

@19wintersp ..hmm lemme try that, thanks! :D

Bookie0 (5956)

@19wintersp Okay, I'm tried to do that. So it kinda works, but try to do the following. Make an account, play around and do some typing tests, and remember your highest score. Then stop the repl, and run it again. Login using the same credentials. The first wrong thing is that it doesn't show the stats, second wrong thing is that when you press tab to do another test, there'll be an error. I think it's because when the user logins, it doesn't define the variables times, average_wpm, highest_wpm, etc. But if we define it, they'll be set back to a value..

Would you mind taking another look? Thanks! :D

(Repl here) :)

19wintersp (1121)

@Bookie0 What you should do is copy the values out of the database into the variables. You've set the variables in the sign up part, and put them into the database, but you don't get them back when they log in. You would put these just after line 73. For example, for "times":

times = db[name]["DBTimes"]
Bookie0 (5956)

@19wintersp okay, I'll add that! :)

Bookie0 (5956)

@19wintersp hey! alright, I've added it. Works pretty well now, when you make a new account, do a typing test, and remember highest score. Next run repl again, login with same credentials. You'll see that it will show the same highest score as before, but when you play again and do another typing speed, and you have a lower wpm, it'll show that new score as the high score even if it's lower than the first one.

Do you know why? Thank you :)

19wintersp (1121)

@Bookie0 Are you making sure to only update the high score if the new one is better than the old one?

Bookie0 (5956)

@19wintersp hm lemme check. and wait, the code you helped me with (https://repl.it/talk/ask/Bookie0-What-you-should-do-is-copy-the/125517/464983) with the times = db[name]["DBtimes"], is that the place where I update the variables? or is it somewhere else?

19wintersp (1121)

@Bookie0 That's where you load the data from the database into the variables. At the end of your program, where you want to save, you would do the reverse (which it seems like you're already doing).

Bookie0 (5956)

@19wintersp oh ok, so where should I make sure to update the high score only if the new one is better than the old one? also, I'm using, a dict called stats to store the highest score as well as the number of words/sentences. So second of all, how would I incorporate that in the DB? I think I already have the highest score ("DBhighest_wpm", and highest_wpm for the variable), but it doesn't update correctly (I think like you said it's updating even if the score/wpm is smaller than the previous one)

Thanks! :)

19wintersp (1121)

@Bookie0 You should set it just before the program exits, or wherever you want to save the state. At this point, check whether the new high score is higher than the current one; if it is, overwrite it. You can store dictionaries in the database just as you would with other pieces of data.

Bookie0 (5956)

@19wintersp Hmm ok, could I perhaps invite you to the repl some time or another? I think it would help a great deal! :)

Bookie0 (5956)

@19wintersp aight, tomorrow not sure if I can, perhaps after tomorrow on Wednesday? I can invite later and you can come when you can :)

Bookie0 (5956)

@19wintersp thanks for your great help! :D

Bookie0 (5956)

@19wintersp Hi, I'm encountering a problem (trying to do a leaderboard), could you hop in for a bit to help please (same repl invite)? :)

FlaminHotValdez (433)

I believe that none cannot be converted to another type,maybe you should change the value of the variable from None to an integer or a string or smthn. Python lets you change variables between types.

python is also bad because you can't initialize a variable without giving it a value.

Bookie0 (5956)

python is also bad because you can't initialize a variable without giving it a value.

yea that's annoying.. @FlaminHotValdez

LukeWright (114)

You said that you do not want it to reset to zero each time, but what if you had something like this:

change_var = False
num_to_change = 0
if change_var:
  num_to_change = 1
  db[var] = int(db[var]) + num_to_change

and then change the value of num_to_change back to None with

num_to_change = None

or something like that

Bookie0 (5956)

@LukeWright but how would change_var be True?

LukeWright (114)

nvm the only way to do that would be an infinite loop that checks it every time the loop runs. @Bookie0

LukeWright (114)

BUT if instead of

db[var] = num_to_change

you did

db[var] += num_to_change

and always stored the database variable as an integer, then it wouldn't change the db variable unless num_to_change != 0.
@Bookie0

Bookie0 (5956)

@LukeWright but I would have to define num_to_change before no?

LukeWright (114)

you could define it as 0 and then change it throughout the code. @Bookie0

Bookie0 (5956)

where would I define it..?
@LukeWright

LukeWright (114)

just at the top of your code when you define your other variables. @Bookie0

Bookie0 (5956)

@LukeWright but each time the program runs it would be reset back to 0.

LukeWright (114)

if it was a database value then it would only change when you add or subtract something from it @Bookie0

LukeWright (114)

define the variable as

num_to_change = db[num_to_change]

then it wouldn't reset to zero everytime the code runs but it would reset to the last value you set num_to change to as long as you remember to store it in the database

catspython (27)

maybe you could put an if statement that asks if they're new then if they are new you can set the vars to 0

Bookie0 (5956)

@catspython but in the else statement what would I do? If I don't put anything it would put an undefined variable error

catspython (27)

@Bookie0 with database you are somehow retrieving the values, though I don't know what database is, you could put in the else statement something to define the values from that account

catspython (27)

I just keep posting POSSIBLE answers, here's another, I think that in the place your using it you should JUST USE ZERO so can you tell me why you can't use 0 in the typing test?

Bookie0 (5956)

@catspython as then each time I'd run the program it'd be reset back to 0

catspython (27)

try this:
var = none
var = (int)
don't do
var = none
var = int(var)
though at this point I think you know that you can't convert none to an int

Bookie0 (5956)

@catspython yea I think I know now lol

catspython (27)

maybe you could set it to zero, because it's an int, but I think I need to know why it needs to be blank.

Bookie0 (5956)

@catspython wouldn't work as it'd reset back to 0 each time I press run.

catspython (27)

@Bookie0 most of these are past revisions

catspython (27)

@Bookie0 the one with the if statement is the current one

LD1 (52)

You can use typhinting to declare the type of a variable without giving it a value. However, python will not recognize it as a valid variable until you assign it a value.

my_int: int
my_float: float

print(my_int)  # Results in error

I can think of two reason why python wouldn't let you set an int as None:
1. None is it's own type (NoneType) -- Just like an integer can't be a string, and integer cannot by None
2. Say I did set an int as None: my_int = int(None). What would happen if I tried using that variable? my_int > other_int wouldn't work, my_int += 1 wouldn't work.

I hope this helps.

Coder100 (16869)

that's a recipe for disaster
step 1: define it with no value at all
step 2: add 1 to it

doesn't make sense right?

Try this instead:

some_value = some_value + 1 if some_value != None else 0
19wintersp (1121)

@Coder100

some_value = if some_value == None: 0 else: some_value + 1

print("Python ternary operation go", "oof" if False else "brrr")
Bookie0 (5956)

@Coder100 but I don't want to set it to anything, 0 or 1.

Coder100 (16869)

then you can't increment it to anything, 0 or 1. @Bookie0

Bookie0 (5956)

@Coder100 hmm. So do you have a workaround?

this is what I'm trying to achieve:

yea sorry, kinda hard to explain. but basically, I have a database using variables. Those variables have to be defined. To define them I have to give them a value. If I run the program (not the repl I linked below, that will give an error), it'll work fine. But when I click stop and run it again, it'll run through the program again, and then when going through the code that defines the variable, it'll be reset. This will mess the values inside the DB.

LukeWright (114)

can you link the code?

LukeWright (114)

if you want to declare a variable without any value in it i guess you could try

variable_name = ""

@Bookie0

Bookie0 (5956)

@LukeWright nah that's a string, and if I have values in the DB, when I run the repl again it'll be reset back to that empty string.

LukeWright (114)

You should be able to just do:

variable_name = int(0)

and that would convert the variable to an integer with a value of 0.
or if you just wanted to change the type, you could do something like:

variable_name = int(variable_name)

@Bookie0

Bookie0 (5956)

@LukeWright Yea but I don't want an initial value.

LukeWright (114)
variable_to_update = int(db["variable_in_db"])
variable_to_update += 1
db["variable_in_db"] = variable_to_update

are you trying to do something like this? I'm confused at what you are trying to do.

@Bookie0

LukeWright (114)

or you could just put something like

db["initial_value"] = whatever you want

and then remove that code so that db[initial_value] doesn't get changed whenever you run the code?