Python Forum
Completly lost about to fail my class!
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Completly lost about to fail my class!
#1
Ok, let first start I am currently overseas in Turkey. So, I am taking my courses online. I am doing introduction to coding. When is came to the basic math problems I was fine. (Keep in my mind I have not clue about coding at all). Now that I have moved to the function part of coding it wont do anything I type in. Sometime I type print "hello world" and that doesn't even work. My problem is we are supposed to do all the exercise in "Think Python: How to think like a scientist" I will type everything in to Phyton the exact way they do in the book and I get nothing but error.

Latest problem says to type (Below) Which I do....

def print_lyrics():
print "I'm a lumberjack, and I'm okay."
print "I sleep all night and I work all day."

If you type a function definition in interactive mode, the interpreter prints ellipses (...) to let you know that the definition isn’t complete:

>>> def print_lyrics():
... print "I'm a lumberjack, and I'm okay."
... print "I sleep all night and I work all day."
...
To end the function, you have to enter an empty line (this is not necessary in a script).
Defining a function creates a variable with the same name.

>>> print print_lyrics
<function print_lyrics at 0xb7e99e9c>
>>> type(print_lyrics)
<type 'function'>

The value of print_lyrics is a function object, which has type 'function'.
The syntax for calling the new function is the same as for built-in functions:

>>> print_lyrics()
I'm a lumberjack, and I'm okay.
I sleep all night and I work all day.

Once you have defined a function, you can use it inside another function. For example, to repeat the previous refrain, we could write a function called repeat_lyrics:

def repeat_lyrics():
print_lyrics()
print_lyrics()

And then call repeat_lyrics:

>>> repeat_lyrics()
I'm a lumberjack, and I'm okay.
I sleep all night and I work all day.
I'm a lumberjack, and I'm okay.
I sleep all night and I work all day.
But that’s not really how the song goes.


And I type it in verbatim and it will never Work I don't get the (...) when I get to Print Print_lyric() Nothing. I am so frustrated. Am even tried typing it in notepad and then pasting it. I have tried everyway I can... I have no idea what to do. I youtube it and people just type stuff in a bam works. I am about to fail my class any help would be greatly appreciated. I just do now anything about coding and all I have is this book. You can PM me or post here.
Reply
#2
We can't really help with this information. Could you provide a screenshot, or a screencap or something?
Reply
#3
Sorry, I can't figure out how to insert a screen shot. I used snipping tool to save screen shots. However I don't see where I upload a file. I am a mess, I'm sorry.
Reply
#4
Use imgur or anything else. We want to help but you gotta give us more information.
Reply
#5
First, let's be sure of the version of python you have. Type python at the prompt and tell us what you get on the first line. If it is version 2.x.x, type:
print "Hello world!"
If it is version 3.x.x, type:
print("Hello world!")
In both cases, you should get Hello world! as an answer.
Reply
#6
Remember python is case sensitive, so "print" and "Print" are different. When you are using the command prompt remember you need to add 4 spaces within functions, classes, etc. So it would look like this:

>>>def code()
...    code starts here
...
to exit the function, press ENTER at the blank "..." line.

When calling the function be sure and include the parenthesis, even if there is no arguments.

print code()

or 

print(code()) if using Python 3
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#7
I would like to add, when you program in any language make sure your code is case sensitive.

If you copy exercise word for word:
def print_lyrics():
print "I'm a lumberjack, and I'm okay."
print "I sleep all night and I work all day."
Then you type:
 print Print_lyric() 
The script would return an error.
When my code doesn't work I don't know why **think** and when my code works I don't know why **think**
Reply
#8
Hope this helps.  It's not exactly what u were doing, but I built a tiny program to take in lyrics.  I imagine it's clunky and not necessarily efficient (u generally should not go more than 3 nests deep...this goes 4 deep early on actually) but it does the job.  It runs on python3 (I imagine this is what u are also using.) Run it initially using the input option to input your lyrics, then run it again using the load option to read them.
 this is the class file...save it as lyrics1cl.py
#lyrics1cl.py

class L():
    def __init__(self):
        self._lyric_list =[]
    def adding(self,item):        
        self._item = item
        self._lyric_list.append(self._item)
    def create_file(self,f):
        self.f = f + '.txt'
        with open(self.f, 'a+') as tf:
            for thing in self._lyric_list:
                tf.write(thing+'\n')
        tf.close
    def print_file(self,f):
        self.f = f+'.txt'
        with open(self.f) as tf:
            tf.seek(0)
            print(tf.read())
        tf.close
Below is the 'main' file that you will run ...save this as whatever you want... I suggest placing both in 1 folder.
The program will build a .txt file for your lyrics and the text file should pop up in the same folder...that way you won't have to run the program for the print out - if you don't want to - Python is considered the easiest to learn but at our stage Python can pi** us off occasionally and sometimes not interacting with it is lovely - hence the text file...if you are frustrated just open than up..
#lyrics1.py

from lyrics1cl import L

Song= L()

print('1] - input\n2] - load')
choice=input('choose one the above options.')

selection = True

while selection:
    if choice =='1' or choice =='input':
        enter_lyric=str(input('enter line below [enter- to stop].'))
        if enter_lyric == '':
            selection = False    
            savefile=str(input('hit [enter] to save your lyrics.'))
            if savefile== '':
                    new_file=str(input('enter filename (w/out ext).'))
                    Song.create_file(new_file)
                    view = str(input('hit [enter] to view lyrics.'))
            if view == '':
                Song.print_file(new_file)
        Song.adding(enter_lyric.strip())
    elif choice =='2' or choice =='load':
        enter_file=str(input('enter file name (w/out ext).'))
        Song.print_file(enter_file)
        break
    else:
        break

print('thx. Good bye.')   
I'm very much a noob like you.  I'm not a particularly big fan of interactive.  IMHO it's a great tool to quickly write/run/debug, but only when you are experienced enough with python syntax and only when you are comfortable enough to navigate things like traceback errors without the initial freakout (my heart certainly skips a beat with tracebacks). At our level, I don't think interactive is very helpful.  My preference is very much ide.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  I am completely lost on this homework assignment a36 1 1,707 Feb-21-2022, 06:01 AM
Last Post: buran
  Very Lost vollynez 3 1,783 Oct-02-2020, 09:09 PM
Last Post: Yoriz
  I got lost in the program Gadev 1 1,842 Nov-11-2018, 05:50 PM
Last Post: j.crater
  I keep getting errors and I'm lost! samjonsnell 9 4,798 Oct-28-2018, 11:38 PM
Last Post: samjonsnell
  test..and I'm already lost sannixinc 1 2,435 Feb-22-2018, 09:09 PM
Last Post: glidecode

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020