help with python plz

Page 1 of 1

11 Replies - 795 Views - Last Post: 16 May 2010 - 08:22 AM Rate Topic: -----

#1 Guest_zee*


Reputation:

help with python plz

Posted 16 May 2010 - 04:53 AM

i donut know what i doing wrong i keep trying different things can any one help me with my program?

#the main function
def main():
    endprogram = 'no'
    while endprogram == 'no':
        totalbottles = getbottles()
        totalpayout = calcpayout(totalbottles)
        printinfo (totalbottles, totalpayout)
        endprogram = raw_input ('do you want to end the program? (enter yes or no): ')

        #this function will get the number of bottles returned
        def getbottles():
            totalbottles = 0
            todaybottles = 0
            counter = 1
            while counter <= 7:
                todaybottles = input ('enter number of bottles for today: ')
                totalbottles = totalbottles + todaybottles
                counter = counter + 1
                return totalbottles

            #this function will display the information
            def printinfo(totalbottles, totalpayout):
                print 'the total number of bottles collected was', totalbottles
                print 'the total paid out was $', totalpayout

#calls main
main()


Traceback (most recent call last):
  File "C:/Documents and Settings/Owner/Desktop/lab5.4.py", line 32, in <module>
    main()
  File "C:/Documents and Settings/Owner/Desktop/lab5.4.py", line 10, in main
    totalbottles = getbottles()
UnboundLocalError: local variable 'getbottles' referenced before assignment

This post has been edited by JackOfAllTrades: 16 May 2010 - 05:37 AM
Reason for edit:: Added code tags.


Is This A Good Question/Topic? 0

Replies To: help with python plz

#2 Guest_Sag*


Reputation:

Re: help with python plz

Posted 16 May 2010 - 05:05 AM

Please Post your code
 
tags
Was This Post Helpful? 0

#3 Guest_Sag*


Reputation:

Re: help with python plz

Posted 16 May 2010 - 05:06 AM

Plese post your code between code tag
Was This Post Helpful? 0

#4 Guest_zee*


Reputation:

Re: help with python plz

Posted 16 May 2010 - 05:12 AM

ok heres the tag thing
#the main function
def main():
    endprogram = 'no'
    while endprogram == 'no':
        totalbottles = getbottles()
        totalpayout = calcpayout(totalbottles)
        printinfo (totalbottles, totalpayout)
        endprogram = raw_input ('do you want to end the program? (enter yes or no): ')

        #this function will get the number of bottles returned
        def getbottles():
            totalbottles = 0
            todaybottles = 0
            counter = 1
            while counter <= 7:
                todaybottles = input ('enter number of bottles for today: ')
                totalbottles = totalbottles + todaybottles
                counter = counter + 1
                return totalbottles

            #this function will display the information
            def printinfo(totalbottles, totalpayout):
                print 'the total number of bottles collected was', totalbottles
                print 'the total paid out was $', totalpayout

#calls main
main()

Was This Post Helpful? 0

#5 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6079
  • View blog
  • Posts: 23,550
  • Joined: 23-August 08

Re: help with python plz

Posted 16 May 2010 - 05:42 AM

Moved to Python

You've defined your functions within the scope of the main function. Move them out of there into the global scope.

#!/usr/bin/env python

#this function will get the number of bottles returned
def getbottles():
    totalbottles = 0
    todaybottles = 0
    counter = 1
    while counter <= 7:
        todaybottles = input ('enter number of bottles for today: ')
        totalbottles = totalbottles + todaybottles
        counter = counter + 1
        return totalbottles

#this function will display the information
def printinfo(totalbottles, totalpayout):
    print 'the total number of bottles collected was', totalbottles
    print 'the total paid out was $', totalpayout

#the main function
def main():
    endprogram = 'no'
    while endprogram == 'no':
        totalbottles = getbottles()
        totalpayout = calcpayout(totalbottles)
        printinfo (totalbottles, totalpayout)
        endprogram = raw_input ('do you want to end the program? (enter yes or \
no): ')

#calls main
main()



Was This Post Helpful? 0
  • +
  • -

#6 Guest_zee*


Reputation:

Re: help with python plz

Posted 16 May 2010 - 05:52 AM

View PostJackOfAllTrades, on 16 May 2010 - 04:42 AM, said:

Moved to Python

You've defined your functions within the scope of the main function. Move them out of there into the global scope.

thanks still got a few bugs work out it seems but that got me moving again
Was This Post Helpful? 0

#7 Guest_zee*


Reputation:

Re: help with python plz

Posted 16 May 2010 - 06:11 AM

    while counter <= 7:
        todaybottles = input ('enter number of bottles for today: ')
        totalbottles = totalbottles + todaybottles
        counter = counter + 1
        return totalbottles 


this part of the code i trying to have the program ask how many bottles 7 times be for it gives the pay out but it only asks one time
Was This Post Helpful? 0

#8 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6079
  • View blog
  • Posts: 23,550
  • Joined: 23-August 08

Re: help with python plz

Posted 16 May 2010 - 06:37 AM

The return will cause you to exit the while loop and the containing scope.
Was This Post Helpful? 0
  • +
  • -

#9 Guest_zee*


Reputation:

Re: help with python plz

Posted 16 May 2010 - 06:50 AM

View PostJackOfAllTrades, on 16 May 2010 - 05:37 AM, said:

The return will cause you to exit the while loop and the containing scope.


if i remove
return totalbottles
then the program stops working after i put in the numbers i try ed moving it but that just makes the count zero how do i put in the return i keep looking throw my manual but its not helping and thats only part not working now
Was This Post Helpful? 0

#10 Guest__gt_viewer*


Reputation:

Re: help with python plz

Posted 16 May 2010 - 07:36 AM

dont remove it just deindent one step
hope it helps
    while counter <= 7:
        todaybottles = input ('enter number of bottles for today: ')
        totalbottles = totalbottles + todaybottles
        counter = counter + 1
    return totalbottles


Was This Post Helpful? 0

#11 Guest_Guest*


Reputation:

Re: help with python plz

Posted 16 May 2010 - 07:58 AM

View Post_gt_viewer, on 16 May 2010 - 06:36 AM, said:

dont remove it just deindent one step
hope it helps
    while counter <= 7:
        todaybottles = input ('enter number of bottles for today: ')
        totalbottles = totalbottles + todaybottles
        counter = counter + 1
    return totalbottles



what is deindent one step?
Was This Post Helpful? 0

#12 Guest_zee*


Reputation:

Re: help with python plz

Posted 16 May 2010 - 08:22 AM

thanks every one my program works now here it is
#this function will get the number of bottles returned
def getbottles():
    totalbottles = 0
    todaybottles = 0
    counter = 1
    while counter <= 7:
        todaybottles = input ('enter number of bottles for today: ')
        totalbottles = totalbottles + todaybottles
        counter = counter + 1
    return totalbottles

#this function will calculate the payout
def calcpayout(totalbottles):
    totalpayout = 0
    totalpayout = totalbottles * .10
    return totalpayout

#this function will display the information
def printinfo(totalbottles, totalpayout):
    print 'the total number of bottles collected was', totalbottles
    print 'the total paid out was $', totalpayout

#the main function
def main():
    endprogram = 'no'
    while endprogram == 'no':
        totalbottles = getbottles()
        totalpayout = calcpayout(totalbottles)
        printinfo (totalbottles, totalpayout)
        endprogram = raw_input ('do you want to end the program? (enter yes or \
no): ')

#calls main
main()


Was This Post Helpful? 0

Page 1 of 1