Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python- Adding string variable into an empty string using 'for in range' function

yut.py is written as following

import random

random.seed(10)

def throw_yut1():
if random.random() <= 0.6 :
return ‘등’
else:
return ‘배’

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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 :((

>Solution :

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!

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading