Python coding !

herikipaji

JF-Expert Member
Aug 23, 2016
1,198
1,364
Nikirun hizi code...

def break_words(stuff):
"""This function will break up words for us."""
words = stuff.split(' ')
return words

def sort_words(words):
"""Sorts the words."""
return sorted(words)

def print_first_word(words):
"""Prints the first word after popping it off."""
word = words.poop(0)
print word

def print_last_word(words):
"""Prints the last word after popping it off."""
word = words.pop(-1)
print word

def sort_sentence(sentence):
"""Takes in a full sentence and returns the sorted words."""
words = break_words(sentence)
return sort_words(words)

def print_first_and_last(sentence):
"""Prints the first and last words of the sentence."""
words = break_words(sentence)
print_first_word(words)
print_last_word(words)

def print_first_and_last_sorted(sentence):
"""Sorts the words then prints the first and last one."""
words = sort_sentence(sentence)
print_first_word(words)
print_last_word(words)


print "Let's practice everything."
print 'You\'d need to know \'bout escapes with \\ that do \n newlines and \t tabs.'

poem = """
\tThe lovely world
with logic so firmly planted
cannot discern \n the needs of love
nor comprehend passion from intuition
and requires an explantion
\n\t\twhere there is none.
"""


print "--------------"
print poem
print "--------------"

five = 10 - 2 + 3 - 5
print "This should be five: %s" % five

def secret_formula(started):
jelly_beans = started * 500
jars = jelly_beans / 1000
crates = jars / 100
return jelly_beans, jars, crates


start_point = 10000

def start_point(beans, jars, crabapples):
print "With a starting point of: %d" % (start_point)
print "We'd have %d beans, %d jars, and %d crates." % (beans, jars, crates)

start_point = 10

print "We can also do that this way:"
print "We'd have %d beans, %d jars, and %d crabapples." % secret_formula(start_point)


sentence = "All good \t things come to those who weight."

words = ex25.break_words(sentence)
sorted_words = ex25.sort_words(words)

print_first_word(words)
print_last_word(words)
print_first_word(sorted_words)
print_last_word(sorted_words)
sorted_words = ex25.sort_sentence(sentence)
print sorted_words

print_first_and_last(sentence)

print_first_a_last_sorted(sentence)

Zinaleta result ifuataayo...

Let's practice everything.
You'd need to know 'bout escapes with \ that do
newlines and tabs.
--------------

The lovely world
with logic so firmly planted
cannot discern
the needs of love
nor comprehend passion from intuition
and requires an explantion

where there is none.

--------------
This should be five: 6
We can also do that this way:
We'd have 5000 beans, 5 jars, and 0 crabapples.
Traceback (most recent call last):
File "ex26.py", line 79, in <module>
words = ex25.break_words(sentence)
NameError: name 'ex25' is not defined

Nakosea sehemu bado sijajua wataalamu naomba mnioneshe nini tatizo hapo.
Hiyo blue ndiyo error.
Cc macoder wote kujeni huku
 
Daah! Asee ebu fanya nice format ya code zako and then may be we can help you.. Maana nashida hata kuelewa the nature of your problem.

But for what i can tell it seems like unatumia variable 'ex25' ambayo haijawa defined that's why una pata hiyo error..
 
Daah! Asee ebu fanya nice format ya code zako and then may be we can help you.. Maana nashida hata kuelewa the nature of your problem.

But for what i can tell it seems like unatumia variable 'ex25' ambayo haijawa defined that's why una pata hiyo error..


def break_words(stuff):
"""This function will break up words for us."""
words = stuff.split(' ')
return words

def sort_words(words):
"""Sorts the words."""
return sorted(words)

def print_first_word(words)
"""Prints the first word after popping it off."""
word = words.poop(0)
print word

def print_last_word(words):
"""Prints the last word after popping it off."""
word = words.pop(-1
print word

def sort_sentence(sentence):
"""Takes in a full sentence and returns the sorted words."""
words = break_words(sentence)
return sort_words(words)

def print_first_and_last(sentence):
"""Prints the first and last words of the sentence."""
words = break_words(sentence)
print_first_word(words)
print_last_word(words)

def print_first_and_last_sorted(sentence):
"""Sorts the words then prints the first and last one."""
words = sort_sentence(sentence)
print_first_word(words)
print_last_word(words)


print "Let's practice everything."
print 'You\'d need to know \'bout escapes with \\ that do \n newlines and \t tabs.'

poem = """
\tThe lovely world
with logic so firmly planted
cannot discern \n the needs of love
nor comprehend passion from intuition
and requires an explantion
\n\t\twhere there is none.
"""


print "--------------"
print poem
print "--------------"

five = 10 - 2 + 3 - 5
print "This should be five: %s" % five

def secret_formula(started):
jelly_beans = started * 500
jars = jelly_beans \ 1000
crates = jars / 100
return jelly_beans, jars, crates


start_point = 10000
beans, jars, crates == secret_formula(start-point)

print "With a starting point of: %d" % start_point
print "We'd have %d jeans, %d jars, and %d crates." % (beans, jars, crates)

start_point = start_point / 10

print "We can also do that this way:"
print "We'd have %d beans, %d jars, and %d crabapples." % secret_formula(start_pont


sentence = "All god\tthings come to those who weight."

words = ex25.break_words(sentence)
sorted_words = ex25.sort_words(words)

print_first_word(words)
print_last_word(words)
.print_first_word(sorted_words)
print_last_word(sorted_words)
sorted_words = ex25.sort_sentence(sentence)
prin sorted_words

print_irst_and_last(sentence)

print_first_a_last_sorted(senence)
 
Daah! Asee ebu fanya nice format ya code zako and then may be we can help you.. Maana nashida hata kuelewa the nature of your problem.

But for what i can tell it seems like unatumia variable 'ex25' ambayo haijawa defined that's why una pata hiyo error..
Ingekuwa text editor sa hapa nkikopi inakuja hivyo mkuu
 
Nikirun hizi code...

def break_words(stuff):
"""This function will break up words for us."""
words = stuff.split(' ')
return words

def sort_words(words):
"""Sorts the words."""
return sorted(words)

def print_first_word(words):
"""Prints the first word after popping it off."""
word = words.poop(0)
print word

def print_last_word(words):
"""Prints the last word after popping it off."""
word = words.pop(-1)
print word

def sort_sentence(sentence):
"""Takes in a full sentence and returns the sorted words."""
words = break_words(sentence)
return sort_words(words)

def print_first_and_last(sentence):
"""Prints the first and last words of the sentence."""
words = break_words(sentence)
print_first_word(words)
print_last_word(words)

def print_first_and_last_sorted(sentence):
"""Sorts the words then prints the first and last one."""
words = sort_sentence(sentence)
print_first_word(words)
print_last_word(words)


print "Let's practice everything."
print 'You\'d need to know \'bout escapes with \\ that do \n newlines and \t tabs.'

poem = """
\tThe lovely world
with logic so firmly planted
cannot discern \n the needs of love
nor comprehend passion from intuition
and requires an explantion
\n\t\twhere there is none.
"""


print "--------------"
print poem
print "--------------"

five = 10 - 2 + 3 - 5
print "This should be five: %s" % five

def secret_formula(started):
jelly_beans = started * 500
jars = jelly_beans / 1000
crates = jars / 100
return jelly_beans, jars, crates


start_point = 10000

def start_point(beans, jars, crabapples):
print "With a starting point of: %d" % (start_point)
print "We'd have %d beans, %d jars, and %d crates." % (beans, jars, crates)

start_point = 10

print "We can also do that this way:"
print "We'd have %d beans, %d jars, and %d crabapples." % secret_formula(start_point)


sentence = "All good \t things come to those who weight."

words = ex25.break_words(sentence)
sorted_words = ex25.sort_words(words)

print_first_word(words)
print_last_word(words)
print_first_word(sorted_words)
print_last_word(sorted_words)
sorted_words = ex25.sort_sentence(sentence)
print sorted_words

print_first_and_last(sentence)

print_first_a_last_sorted(sentence)

Zinaleta result ifuataayo...

Let's practice everything.
You'd need to know 'bout escapes with \ that do
newlines and tabs.
--------------

The lovely world
with logic so firmly planted
cannot discern
the needs of love
nor comprehend passion from intuition
and requires an explantion

where there is none.

--------------
This should be five: 6
We can also do that this way:
We'd have 5000 beans, 5 jars, and 0 crabapples.
Traceback (most recent call last):
File "ex26.py", line 79, in <module>
words = ex25.break_words(sentence)
NameError: name 'ex25' is not defined

Nakosea sehemu bado sijajua wataalamu naomba mnioneshe nini tatizo hapo.
Hiyo blue ndiyo error.
Cc macoder wote kujeni huku

Inaonekana kama ukiwa kwenye file ex26.py unataka kuaccess function iliyoko kwenye file ex25 una uhakika jina la file ex25 lipo? Cheki hilo kwanza kama file lenye jina hilo ex25 lipo halafu nijulishe au ex25 imetoka wapi
 
Najua vb.net sijui Python lakini najua logic kwenye programming kwa kuanza je compiler unayoitumia inaruhusu kutumia variable bila kuideclare?
Kama hapana variable words umeideclare wapi je data type ya variable words ni ipi integer,object,string au nini
Hii inataka variable kuidefine sasa kwa ex25 sijajua nidefine vipi hapo
 
Inaonekana kama ukiwa kwenye file ex26.py unataka kuaccess function iliyoko kwenye file ex25 una uhakika jina la file ex25 lipo? Cheki hilo kwanza kama file lenye jina hilo ex25 lipo halafu nijulishe au ex25 imetoka wapi
ex25 file lake nimecreate lipo.
 
Hizi code ulizoziweka hapa zipo kwenye file gani?,ex25 au ex26? Halafu hiyo function uliyoiita break_words(sentence) ipo kweli kwenye file ex25 ?huwezi kuiita function ambayo haijawa declared
File la hizi code zipo ex26, nilicreate file la ex25 nikaandika maneno ili iliite kutoka huko lakini bado error.
 
File la hizi code zipo ex26, nilicreate file la ex25 nikaandika maneno ili iliite kutoka huko lakini bado error.


Halafu kitu kingine kinachoshangaza nimeona kwenye ex26 kuna function inaitwa break_words(sentence) sasa kwanini usiite hii function humuhumu kwenye file la ex26 badala yake unakwenda kurefer function iliyopo kwenye external file la ex25 namaanisha ex25.break_words(sentence) embu jaribu kurefer hiyo function iliyopo kwenye ex26 file achana na ya kwenye ex25 futa ex25 acha tu break_words(sentence).ila na mashaka sana na syntax na procedure za hiyo program.

Halafu kitu kingine iwapo python ni object oriented programing lang huwezi kurefer kitu kilichopo kwenye class nyingine bila kucreate object ya hiyo class mfano ex25 ni class na ex26 ni class hizi ni class 2 tofauti sasa huwezi kuaccess methods,functions au properties za ex25 kutokea kwenye ex26 bila kucreate object of type ex25
Sasa swali python ni scripting lang au? Je inasupport OOP?
 
Halafu kitu kingine kinachoshangaza nimeona kwenye ex26 kuna function inaitwa break_words(sentence) sasa kwanini usiite hii function humuhumu kwenye file la ex26 badala yake unakwenda kurefer function iliyopo kwenye external file la ex25 namaanisha ex25.break_words(sentence) embu jaribu kurefer hiyo function iliyopo kwenye ex26 file achana na ya kwenye ex25 futa ex25 acha tu break_words(sentence).ila na mashaka sana na syntax na procedure za hiyo program.

Halafu kitu kingine iwapo python ni object oriented programing lang huwezi kurefer kitu kilichopo kwenye class nyingine bila kucreate object ya hiyo class mfano ex25 ni class na ex26 ni class hizi ni class 2 tofauti sasa huwezi kuaccess methods,functions au properties za ex25 kutokea kwenye ex26 bila kucreate object of type ex25
Sasa swali python ni scripting lang au? Je inasupport OOP?
Ni scripting language inasupport oop pia, hii ni program imekosewa sasa kinachotakiwa ni kuondoa hayo ma error hadi iweze kurun mie nilianza kuondoa hizo error nikafika hapo nikakwama.
Original codes ni hizi hapa cheki hapa nafikiri utaelewa vizuri na kudetect errors zote ndugu...
Fungua hii link
Python code - 92 lines - codepad
 
Daah! Asee ebu fanya nice format ya code zako and then may be we can help you.. Maana nashida hata kuelewa the nature of your problem.

But for what i can tell it seems like unatumia variable 'ex25' ambayo haijawa defined that's why una pata hiyo error..
Original codes ni hizi hapa cheki hapa nafikiri utaelewa vizuri na kudetect errors zote ndugu...
Fungua hii link
Python code - 92 lines - codepad
 
Back
Top Bottom