Question by the digits in Python

Hi there.

I can not solve this problem:

there is a property - own[“digit”] == 5
It is necessary to make so that in a script was - 99999

if own[“digit”] == 2
then - 99

That is, the number of nines in the script match to the number in the property - own[“digit”] == 1 then 9, own[“digit”] == 6 then 999999…

Thank you for attention.

python do not have a “then” statement


if own["digit"] == 2:
    own["digit"] = 9999

or maybe this is what you want


if len(own["digit"]) == 2:
    own["digit"] = 99

or maybe this if its the number of digits in a int or float (the dot in a float count as a digit)


if len(str(own["digit"])) == 2:
    own["digit"] = 99

  • sorry, maybe I misunderstood the assignment. I did not write the code in the question, it’s a simple text of the job.
    I need to automatically generate a number of 9 in the script from the number in the property.
    For each value to write code, not very professional and uncomfortable. In my example, I can still write 10 values, but if I need 100, the script will be terrible.

Thank you.

is this it ?


digit = 99.1

out = ""
for c in str(digit):
    out += "9"
    
print(int(out))

outputs: 9999

  • no
    I need: (simplify)

digit = 10

out = ""
for c in str(digit):
    out += "9"
    
print(int(out))

outputs: 9999999999


digits = int(owner['digits'])
code = '9' * digits


digit = 10
out = ""

for c in range(digit):
   out += "9"

print(out)

this outputs : 9999999999

i have forgotten about that.

yah that works to

TBH we just do not understand what you want…it seems like we could do it easily…but I, myself, have no clue what you are trying to do…

do you want it to output a ‘9’ for the numerical value…so the value of the property is the quantity of placeholders/digits?

1 would be one 9
2 would be two 9’s
3 would be three 9’s
etc…

so 1 = 9
2 = 99
3 = 999?

I already tried it myself:


digit = 3

out = ""
for c in str(digit):
    out += "9"*digit
    
print(int(out))

this outputs : 999

  • but if the number is greater than ten, the number of nines is multiplied by two. it will be necessary to divide (reduce the number of nines by half) - expand the function (approximately):
import bge

cont = bge.logic.getCurrentController()
own = cont.owner

digit = own["digit"]

if own["digit"] <= 9:
    out = ""
    for c in str(digit):
        out += "9"*digit
    
if own["digit"] >= 10:
    out = ""
    for c in str(digit):
        out += "9"*digit ???

print(int(out))

-I do not yet know how to solve the problem with the number >= 10

  • well, yes, exactly, but do not prescribe every value, but automatically - for the future already (post #10), up to 10 everything works …

Here:

you need to for loop the variable you set, then append to a list then joint that list into a string.


count = 10
output = []


for i in range(count):
    output.append(9)


end_result = ''.join(str(entry) for entry in output)  
print(end_result)

change count to any number you like, it will add that many 9’s to the end_result

I guess I made my example (future) a bit complicated:
so to speak:
I have a 1234
how to make them? - 9999

or 123456
to do 999999
so it is easier to use - minus one property.


digits = 20
out = '9' * digits

print(int(out))


outputs 20 digits: 99999999999999999999

that’s easy


digits = len(str(own["digit"]))
own["digit"] = "9" * digits


  • the goal! Working. I thank everyone for their help.

Сan help with this (I simplified the example) - #13

actually i did not read the whole topic and Wknight02/edderkop has an faster/better solution


digits = 20
out = '9' * digits

print(int(out))

does actually the same thing as my script does

  • here is the working solution for the main question:

import bge
cont = bge.logic.getCurrentController()
own = cont.owner

digits = int(own["digit"])
own["prop"] = '9' * digits

print(own["prop"])

if own[“digit”] = 20
output = 99999999999999999999

I thank everyone for solving the main issue.

  • this is a solution for the main question, but not for an additional one - #13

Here use this for you other question


digits = 532
count = len(str(digits))
out = '9' * count


print(int(out))

this wil convert 532 into 999 so one 9 per digit.
or 123456789 to 999999999, etc