Python Forum
Put Print Statement Wrong Somehow...
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Put Print Statement Wrong Somehow...
#1
I have my first ever bit of code going alright and then I put some print() statements in at the bottom and they don't print and the system doesn't tell me why, just does a restart in the Python shell.

Stripped down this is essentially what I've got:
globallist = []

def function(n,m,p):
       
       return(n,m)
    else:
       return( function(n,m,p))


    function(325000000,85000000000000,0)
And all that works fine. Then these print statements don't:
    
    print("Global list :",globallist)
    print(globallist[p-1])
    print(globallist[p-2])
    print(globallist[p-3])      
    print(globallist[p-4])
What am I doing wrong here? I can find no mentions anywhere....
Reply
#2
This code couldn't possibly run.
show complete runable code snippet
Reply
#3
Ah.. sorry about that. I was thinking I was showing it not as code, really, but just a representation of what I'd done.

Thinking my error is probably so egregious it'll be totally obvious just from that.

My fault.

Okay. Here's the code. I learned more. It runs on Python 3.7 on a machine there. I put it on this machine with Python 3.6 this morning, for this exercise. And it doesn't run here !

Just get three 'reset' messages. No output at all.

The code runs fine on 3.7 until I put in the 'list' stuff. The globallist[] and the print statements.

If Python gets as finicky as that: runs on 3.7 but not 3.6 - I can see you'd want everything as 'pure' as possible before trying to help people.

Well that's it. Win10. 3.7 there, okay, except with lists, when it produces its normal output but then just restarts when it should be printing.

3.6 here no good with lists at all. Produces only restarts.
globallist = []

def recurse(n,m,p):
    
    print("recurse has been called with population = " +"{:.2f}".format(n) + "and wealth =" + "{:,.2f}".format(m))
    if n <= 2:
       
       return(n,m)
    else:
        
       p=p+1
       n = n*0.2
       m = m * 0.8
       globallist.append(n)
       print("The 20% is:","{:.2f}".format(n), "The wealth they have :$","{:,.2f}".format(m))
       print("The number of calls is :",p)
       print("Globallist entry 11",globallist[p-1])
       return( recurse(n,m,p))

# america's wealth 85 trillion
# america's population 325 million

    recurse(325000000,85000000000000,0)
    
    print("Global list :",globallist)
    print(globallist[p-1])
    print(globallist[p-2])
    print(globallist[p-3])      
    print(globallist[p-4])      
Hope that's okay.

:)[/python]

oh dear... doesn't look good.. i wrote the tags myself and onscreen i see no highlighting or such so i edited and used your tag function to do it and still no highlighting, border or evidence of the tags.... hope it's okay..
Reply
#4
it's not possible for anything beyond line 18 to execute.
reasons
  • lines 23 through 29 are indented, which means they are part of the recurse function
  • one of the two conditions of the if ... else ... clause will execute. Both have return statements which will cause an exit from the function
Reply
#5
Thanks for that. Thought so. Very simple problem, me being so new. Just indents.
I hadn't know how critical the positioning on the page everything is.

So now I've got something working much better but it just reckons p is undefined so I guess I've got to somehow make my parameters, n,m,p global? Or pass them to global vars within the function?

I just want to work on the list I've produced. Do some additions.

Here it is now:

print("pytest3")
globallist = []

def recurse(n,m,p):
    
    print("recurse has been called with population = " +"{:.2f}".format(n) + "and wealth =" + "{:,.2f}".format(m))
    if n <= 2:
       return(n,m,p)
    else:
       print("in the else") 
       p=p+1
       n = n*0.2
       m = m * 0.8
       globallist.append(n)
       print("The 20% is:","{:.2f}".format(n), "The wealth they have :$","{:,.2f}".format(m))
       print("The number of calls is :",p)
       print("Globallist entry 11",globallist[p-1])
    return(recurse(n,m,p))

# america's wealth 85 trillion
# america's population 325 million

recurse(325000000,85000000000000,0)
    
print("Global list :",globallist)
print(globallist[p-2])
print(globallist[p-3])      
print(globallist[p-4])      
Reply
#6
Quote:Or pass them to global vars within the function?
This is what you should be doing.
Global variables should be used very sparingly if at all. Sooner or later they will get you into trouble.
Reply
#7
thank you for that.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Am I wrong or is Udemy wrong? String Slicing! Mavoz 3 2,535 Nov-05-2022, 11:33 AM
Last Post: Mavoz
  Why doesn't this print statement work? stylingpat 10 5,723 Mar-23-2021, 07:54 PM
Last Post: buran
  print function output wrong with strings. mposwal 5 3,107 Feb-12-2021, 09:04 AM
Last Post: DPaul
  Runs perfect in Python but fails to print last statement when converted to .exe. Help sunil422 3 2,804 Aug-13-2020, 01:22 PM
Last Post: deanhystad
  what is wrong with this 'if' statement!? neshom_imilas 1 1,576 Jul-27-2020, 03:58 PM
Last Post: bowlofred
  Taking brackets out of list in print statement pythonprogrammer 3 2,391 Apr-13-2020, 12:25 PM
Last Post: perfringo
  capture print statement written in Stored Procedure in SQL Server brijeshkumar_77 0 2,553 Feb-18-2020, 03:22 AM
Last Post: brijeshkumar_77
  printing a list contents without brackets in a print statement paracelx 1 2,115 Feb-15-2020, 02:15 AM
Last Post: Larz60+
  python gives wrong string length and wrong character thienson30 2 2,990 Oct-15-2019, 08:54 PM
Last Post: Gribouillis
  Embedding return in a print statement Tapster 3 2,268 Oct-07-2019, 03:10 PM
Last Post: Tapster

Forum Jump:

User Panel Messages

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