Hey everyone, I decided to learn to code in Python recently however my first attept to do some coding has not quite worked. It's a pretty basic task i'm trying to do but I just can't seem to work out how to fix it.
This is the code.
[CODE]#Speed Seperation table
#Speed Seperation groups
LIGHT=("8")
MEDIUM_SMALL=("7")
LOWER_MEDIUM=("7")
UPPER_MEDIUM=("7")
HEAVY=("6")
A380=("4")
# Welcome and question
question=input("Welcome to seperation finder.\n\n\nPlease enter the aircraft type\nfollowing the A380\n")
#Fucntion
def is_equal(choice, answer):
if choice==LIGHT:
answer=LIGHT
elif choice==MEDIUM_SMALL:
answer=MEDIUM_SMALL
elif choice==LOWER_MEDIUM:
answer=LOWER_MEDIUM
elif choice==UPPER_MEDIUM:
answer=UPPER_MEDIUM
elif choice==HEAVY:
answer=HEAVY
elif choice==A380:
answer=A380
else :print("Please enter a valid aircraft type")
[/CODE]
The issue I have is when I run it the first time the program gives me no answer, however the second time it works.
This is what I get when I run the program.
[CODE]
>>> ================================ RESTART ================================
>>>
Welcome to seperation finder.
Please enter the aircraft type
following the A380
LIGHT
>>> LIGHT
'8'
>>> [/CODE]
I hope someone can help me. Thanks
What tutorial is that? im trying to learn python too!
[QUOTE=HTMLfreak;35037446]What tutorial is that? im trying to learn python too![/QUOTE]
Well the tutorial is from this guy:
[url]http://codingclub.cuteseal.co.uk/book1.php[/url]
However after doing the first three turtorials I felt able to do a mini project of my own to test my skill, looks like i'm not good enougth yet haha.
Well to be perfectly honest there is a LOT of issues with this code.
To answer your main question, you never actually plug anything into the is_equal function that you defined.
Here's what happening. You prompt the user for the input, which works fine. Then you never plug that input back into the function. THEN the second time you type an answer, you are actually recalling the variable itself that you assigned earlier on, which you happened to assign the value "8" to. So it returns '8'.
I'm not going to fix the code for you because it's clear that you lack some basic understanding. If this post isn't enough to help you feel free to send me a PM and I'll clear some things up for you. I suggest doing some more reading.
Ok thanks for the help, any chance you could give me a keyword to google which would help me fix this mess, i'm struggling to find much about pluging inputs into functions on google (although I understand what your talking about).
Apart from repeating what Socram said i would give [URL="http://learnpythonthehardway.org/"]Learn python the hard way[/URL] a look, it was made for python 2.7 but there is little basic changes from 2.7-3.2 and is very in depth and easy to understand.
There's also [url]http://www.udacity.com/[/url] (CS101 - Building a Search Engine), it's not "complete" yet, but we're learning python and will eventually write up our own silly little web crawler and search engine :3
Yes it's free, just sign up with name email password and start (I don't even think you have to verify your email to start the courses). Some stuff will seem like it's taking too long to get through if you already have programming knowledge because it's a "no prerequisites" course.
My wording of "plugging in" might be a little confusing, and probably won't get all that much out of google as it isn't really a programming term per se. I'll write some code that might help you (although bear in mind Python isn't my main language).
[CODE]
#This is a simple function that uses takes 1 argument (the thing in parentheses, nameToGreet), which in this case will be a name,
#and prints "Hello, {nameToGreet}"
def helloName( nameToGreet ):
print("Hello, " + nameToGreet)
#Here we prompt the user for a name and store it in a variable called 'name'.
name = input("What is your name sir?")
#We now call our function helloName and we want to use whatever was stored in the variable name as the name we say hello to,
#so we put the existing name variable in where that one argument is.
helloName( name )
[/CODE]
Some example output would be:
[CODE]
What is your name sir?
"John"
"Hello, John"
#or
What is your name sir?
"Socram"
"Hello, Socram"
[/CODE]
Whats key to understanding is that the function won't know about whatever the name is UNTIL we use the variable name ("plug it in") in the function.
Hope that helps :/. I remember being in your shoes, don't give up!
Thanks Socram, that makes it much clearer, I'm going to carry on with the "Learning Python the hard way" tutorial as it's already helping me with the codeing.
Cheers everyone.
Sorry, you need to Log In to post a reply to this thread.