最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python - import script value in different script with new results (diceroller) - Stack Overflow

programmeradmin2浏览0评论

I'm learning to write python to create scripts to automate stuff for DD. And I'm positive that there most likely is a built in solution for this.

I have certain functions from external scripts that I would like to call several times.

For example, let's say I have a script called d6.py that says.

import random
d6 = random.randint(1,6)

I then import that script in my other script called diceroller.py to use.

from d6 import *

print (d6)
print (d6)

How do I make the print statement return different values?

I have tried to search using several search terms, but I think I lack the programming "vocabulary" to know how to find the solution.

I'm learning to write python to create scripts to automate stuff for DD. And I'm positive that there most likely is a built in solution for this.

I have certain functions from external scripts that I would like to call several times.

For example, let's say I have a script called d6.py that says.

import random
d6 = random.randint(1,6)

I then import that script in my other script called diceroller.py to use.

from d6 import *

print (d6)
print (d6)

How do I make the print statement return different values?

I have tried to search using several search terms, but I think I lack the programming "vocabulary" to know how to find the solution.

Share Improve this question edited yesterday Mureinik 311k54 gold badges351 silver badges388 bronze badges asked yesterday skrapsanskrapsan 111 silver badge1 bronze badge New contributor skrapsan is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
Add a comment  | 

1 Answer 1

Reset to default 1

Your code executes random.randint(1,6) once, and assigns the result to d6. Once assigned, d6 is just a number, no matter how many times you import it. Instead, you should define it as a function:

def d6():
    return random.randint(1,6)

and then call it as needed:

from d6 import *

print (d6())
print (d6())
发布评论

评论列表(0)

  1. 暂无评论