yut.py is written as following
import random
random.seed(10)
def throw_yut1(): if random.random() <= 0.6 : return '등' else: return '배'
def throw_yut4(): result = ''
for i in range(4):
result = result throw_yut1()
return result
p1.py is written as following
import yut
counts = {}
for i in range(1000):
result = yut.throw_yut4()
back = throw_yut4.count('등')
belly = throw_yut4.count('배')
if back == 3 and belly == 1:
counts['도'] = counts.get('도', 0) 1
elif back == 2 and belly == 2:
counts['개'] = counts.get('개', 0) 1
elif back == 1 and belly == 3:
counts['걸'] = counts.get('걸', 0) 1
elif back == 0 and belly == 4:
counts['윷'] = counts.get('윷', 0) 1
elif back == 4 and belly == 0:
counts['모'] = counts.get('모', 0) 1
for key in ['도','개','걸','윷','모']: print(f'{key} - {counts[key]} ({counts[key] / 1000 * 100:.1f}%)')
I keep getting error when I run the second module p1.py It says there is trouble defining throw_yut4 help please :((
CodePudding user response:
To fix your problem, you can add the statement
from yut import *
Alternatively, keep
import yut
But use
yut.throw_yut4
if you want to access/use that function from yut
.
That will fix your name 'throw_yut4' is not defined
error. Also, do
back = yut.throw_yut4().count('등')
belly = yut.throw_yut4().count('배')
You need to call throw_yut4
. You were not before. That is why it was saying the function object has no attribute count
. But strings do and your function returns a string when called!
CodePudding user response:
What error are you getting?
Your code runs perfectly fine for me. It could be because of improper indentation in your case. Try running the following snippet...
import random
random.seed(10)
def throw_yut1():
if random.random() <= 0.6:
return 'A'
else:
return 'B'
def throw_yut4():
result = ''
for i in range(4):
result = result throw_yut1()
return result
print(throw_yut4())
Are you considering getting the output "AAAA" everytime you run the code an error?
This is happening because of the seed that you have set. Everytime you'll run the python file, you'll get the same sequence of float values from random because of the set seed and for seed = 10, the first 4 values are all less than 0.6 therefore giving the output "AAAA".