The Student Room Group

Cant find error in python code

I'm just getting used to python and I've written a program however there's loads of errors that pythons picked up on but I don't know what's wrong with it. I've attached the code exactly how I've put it into python.

Scroll to see replies

Reply 1
Some of your syntax is wrong. Whoever has taught you hasn't approached it very well. I'm doing the same piece as you and can guide you in the right direction.

Firstly, you cannot put a function inside of a function. A function is a piece of code that is designed to do a certain task, so naturally putting one inside of the other doesn't really make much sense.

A lot of your indentation is also wrong. When creating a loop, you cannot just type "while True:" as this will create an infinite loop (you never want these). I would recommend using functions instead of loops to do things such as enter survival rates, but that's just personal opinion. If you do want to use loops, follow this syntax (scroll down to the first example).

On your menu, you cannot just type "print("1: Set generation 0 values")" and assume that Python will know that the user is meant to enter. Instead, you should let Python know that the user is meant to enter something here. This can be done by typing "variableName = input("Text here").

I can't really tell you the code as that will get you disqualified. If you want more help, or want me to explain something, just reply :smile:
(edited 7 years ago)
Reply 2
I had the code working before I was just trying to validate it so when the user entered a letter it took them back to the menu, and about the function thing I was just trying to define multiple functions so I could call them(is there any point in doing that or should I just use variables)
Reply 3
I tried to upload the original file but it says its an invalid file
Reply 5
my teacher said we can use either. Could you rewrite the code then? Thanks you've probably been more helpful then my teacher has ever been :smile:
Reply 6
Original post by Pridejxjs
my teacher said we can use either. Could you rewrite the code then? Thanks you've probably been more helpful then my teacher has ever been :smile:


Just read up on AQA's rules and I can't exactly re-write everything for you. I can advise you though.

I would say rather than using while loops for everything, create a function for everything. I'd also recommend that you go through Codeacademy's Python tutorial as this will help you a lot. If you need something explaining, I can also help you with that :smile:
Reply 7
So I have to make one function for all of the options or one for each
Reply 8
Original post by Pridejxjs
So I have to make one function for all of the options or one for each


One function for each of the options. I.E. One function for menu option one, a second function for menu option two etc
Original post by iHammmy
Firstly, you cannot put a function inside of a function. A function is a piece of code that is designed to do a certain task, so naturally putting one inside of the other doesn't really make much sense.

Yeah you can - try it. It's useful for encapsulation, can reduce code duplication and be used as closure functions.
Original post by iHammmy
When creating a loop, you cannot just type "while True:" as this will create an infinite loop (you never want these).
You can use break to exit a loop.while True: if some_condition: break
Reply 10
OP's function inside a function had lots of syntax errors and therefor didn't work.

And I didn't see that OP has used a break statement, thanks for pointing out :smile:
Reply 11
Hi sorry to bother you again but how do you define two things in the same code as I'm trying to store the user input in a variable but it says I need to define it however I've already defined the menu on the first line and I've already defined it to the user input.
Reply 12
Original post by Pridejxjs
Hi sorry to bother you again but how do you define two things in the same code as I'm trying to store the user input in a variable but it says I need to define it however I've already defined the menu on the first line and I've already defined it to the user input.


If you've defined the variable inside a function, it will only work inside that function. This is called a local variable. You can get around this by typing;global (variablename) (without the brackets, just fill change variablename to whatever your variable is called) on the very top line of your code. Then when you want to use this variable later on, just typeglobal (variablename)on the line above where you want to use your variable.



For example, def menu():
menuOption = int(input("Please enter your option: "))

def anotherFunction():
if menuOption = "1":
pass
else:
pass
This would not work as we have not globalised our variable menuOption.
To get around this, we would:global menuOption

def menuOption():
global menuOption
menuOption = int(input("Please enter your option: "))

def anotherFunction():
global menuOption
if menuOption = "1":
pass
else:
pass
This should work :smile:
(edited 7 years ago)
Reply 13
how many global values can you define at the start of the code?
Reply 14
Original post by Pridejxjs
how many global values can you define at the start of the code?


As many as you want :smile:
Reply 15
kk thx I finally finished the first bit :smile:
(edited 7 years ago)
Reply 16
Original post by Pridejxjs
kk thx I finally the first bit :smile:


You're welcome :smile:

Anything else you need help on?
Reply 17
this isn't about the code but for task 2 when it says population for the three groups does that mean one number for each group or the total number.
Reply 18
Original post by Pridejxjs
this isn't about the code but for task 2 when it says population for the three groups does that mean one number for each group or the total number.


The total number.
Reply 19
I've defined choice in my code but when I run it,it says its not defined and I don't know whats wrong.https://www.dropbox.com/s/6gs1ruby488lnhl/Pythoncode.py?dl=0

Quick Reply

Latest

Trending

Trending