Python Forum
Hey guys can you please help me with some basic coding
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Hey guys can you please help me with some basic coding
#8
Thanks for the clarification on that ichabod. It is very helpful with reading code understanding the syntax and style.

Quote:I get an error if I set the user and comp outside the game function.
def fn1():
   user,comp = 1,2
   print(j)
   return x,y,z

def fn2():
   j = 'outside function'
  return j

fn2()
fn1()
print(user,comp)
If you run this, you will get an error that "j" isn't defined. Even though in fn2() it is. The reason is because variables are only defined in the function. The variable "j" in fn2() is a different variable than the "j" in fn1(). return j means that the value is available to be called in global or different functions. Once you define j in fn1(), the same thing will happen with user and comp since I never defined them at global and never defined them by calling their values from fn1().
If you failed to define user and comp in game() by calling the values ala user, comp = function_where_you_defined() or game(user,comp), you will get a trace back error.

Quote:I tried returning comp and user after adding 1 in my if and elif statements but that just broke the loop
Copied from the Python Doc 7.6. The return statement: "return leaves the current function call with the expression list (or None) as return value... In a generator function, the return statement indicates that the generator is done and will cause StopIteration to be raised."

From what I understand, it's standard convention to define your primary function as main(). Doesn't mean you have to, but reviewers will understand that is the function that is the main purpose of your program. If you do decide to implement separate functions, it's something you'll want to remember.

I hope someone other than me learned something from this reply.
Reply


Messages In This Thread
RE: Hey guys can you please help me with some basic coding - by tozqo - Jul-16-2017, 05:59 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Basic Coding Question: Exit Program Command? RockBlok 3 677 Nov-19-2023, 06:31 PM
Last Post: deanhystad
  guys please help me , pycharm is not able to identify my xlsx file CrazyGreenYT7 1 2,085 Jun-13-2021, 02:22 PM
Last Post: Larz60+
  hi guys, i'm back to learning python ! astral_travel 6 3,028 Oct-05-2020, 10:02 AM
Last Post: astral_travel
  Hi Guys, please help me to write SAS macro parameter equivalent code in Python Manohar9589 2 2,687 Jun-14-2020, 05:07 PM
Last Post: Larz60+
  [split] Very basic coding issue aary 4 2,549 Jun-03-2020, 11:59 AM
Last Post: buran
  Very basic coding issue mstichler 3 2,663 Jun-03-2020, 04:35 AM
Last Post: mstichler
  Basic example Coding gudlur46 2 2,129 Dec-19-2019, 01:55 PM
Last Post: gudlur46
  Could you guys help with this bug. I would post on StackOverflow but I got tempbanned ryder5227 2 2,484 Sep-26-2019, 08:01 PM
Last Post: metulburr
  Basic coding question with Python Than999 3 3,207 Jul-17-2019, 04:36 PM
Last Post: jefsummers
  Help Please guys AbuSiraj 2 2,770 Feb-14-2019, 09:29 AM
Last Post: AbuSiraj

Forum Jump:

User Panel Messages

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