Need Simple Python Code! Help!

This is the place for queries that don't fit in any of the other categories.

Need Simple Python Code! Help!

Postby gusgus » Mon Jan 20, 2014 8:36 pm

Okay, here's the thing... I simply can't get my head around Python and this code would sort of help me to understand how on Earth it works! This is my first time here so please... Be gentle! (I do need this code for a lesson and I will tell my teacher I couldn't understand it so I asked for help...)

Anyway, I have put together a very simple and basic idea on what the code needs to be, but first here's the "question":

"A taxi company needs a program to work out how much to charge their customers. The first kilometer is £3 and every other kilometer after that is £2. If the number of passengers in the vehicle is above 4 another £2 is added for each customer. Write an algorithm for Python that successfully meets the needs of the company. The distance must be entered as integers. (Or your program may round up the amount by itself for extra marks!)"

So... Here's a very simple pseudocode on what I intended to do, (I did not include the rounding up part at the end because then my brain would simply fry up!)...

"psgnumb" = Passenger Number;
"distkm" = Distance (In kilometers.)

Code: Select all
INPUT psgnumb
INPUT distkm


IF distkm > 1 THEN
   subtotal = distkm * 2 + 1
ELSE
   subtotal = 3
ENDIF


IF psgnumb > 4 THEN
   total = subtotal + ((psgnumb - 4) * 2)
ELSE
   total = subtotal
ENDIF

PRINT "Journey price: £", total


I know everyone has better things to do but I do need help! :D I realize for the top programmers - which most of you guys on here are - (at least compared to me... Ha!), this is easy.

ALL AND ANY HELP IS WELCOME! THANK YOU VERY MUCH IN ADVANCE!
Last edited by micseydel on Mon Jan 20, 2014 9:09 pm, edited 1 time in total.
Reason: Locked OP.
Beginning to understand Python... New to the forum... :D
gusgus
 
Posts: 5
Joined: Mon Jan 20, 2014 8:02 pm

Re: Need Simple Python Code! Help!

Postby stranac » Mon Jan 20, 2014 9:20 pm

What are you having trouble with specifically? What have you tried?
Python is pretty pseudo-code-like, so translating the pseudo code shouldn't be hard.
The way you posted this question, it looks like you're just too lazy to do your own homework...
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1245
Joined: Thu Feb 07, 2013 3:42 pm

Re: Need Simple Python Code! Help!

Postby gusgus » Mon Jan 20, 2014 10:40 pm

I realize it does seem I am too lazy to do my homework and I do understand Python isn't the hardest of coding languages. I have tried to "translate" the whole pseudocode to Python but nothing I do works. I was only just introduced to programming and I just don't see what it is I'm doing wrong.

By the way, can you use "end if" in Python? I have searched on where and how to use it but I didn't find anything clear.

Here's what I last attempted:
Code: Select all
distkm = input ("How many KM have you travelled? ")

if distkm > 1:
    subtotal = distkm * 2 + 1
    else subtotal = 3

psgnumb = input ("How many passengers have travelled? ")

if psgnumb > 4:
    total = subtotal + ((psgnumb - 4) * 2)
    else total = subtotal

print ("Journer price: £"), total


Maybe it might just be the way I try to "run" it... I first typed it up on Python "IDLE" and then opened the file with Python "Command Line."

It opens and simply closes...

I really don't know what happens... I changed the variable names... Changed the order of stuff and it keeps doing the same thing. I'm kind of frustrated. >.< My teacher did it in like 30 seconds and it worked perfectly.

The thing is thought, my teacher gave us about 1 hour to go through some simple Python exercise PDFs (To learn commands such as "print") gave us a few more lessons on general coding and more on pseudo code than anything else and now he expects us to write a program with something completely new to me and everyone else in my class.

I am prepared to tell him I didn't do it because I simply didn't understand it but, I do want to learn at least something about Python and maybe even learn to use it really well. I just can't do it when every time I run my program it closes in my face and I simply don't understand "WHY!?"
Last edited by gusgus on Mon Jan 20, 2014 10:48 pm, edited 1 time in total.
Beginning to understand Python... New to the forum... :D
gusgus
 
Posts: 5
Joined: Mon Jan 20, 2014 8:02 pm

Re: Need Simple Python Code! Help!

Postby gusgus » Mon Jan 20, 2014 10:41 pm

Thank you for replying anyway. :)
Beginning to understand Python... New to the forum... :D
gusgus
 
Posts: 5
Joined: Mon Jan 20, 2014 8:02 pm

Re: Need Simple Python Code! Help!

Postby Yoriz » Mon Jan 20, 2014 11:05 pm

Indentation is important in python a matching else should be at the same indentation as its if and else should be followed by :
endif is not required in python.
Input returns a str, you are comparing the input strings to int's
The items you want to print should be inside the brackets
Becasue you have a £ in your code you need to have the following in your first line
Code: Select all
# encoding: utf-8

You are doing your calcualations on str and int, the str as above needs converting to a int

Consider this pseudocode to do the same thing as your original pseudocode
Code: Select all
INPUT psgnumb
INPUT distkm

total = 3

IF distkm > 1 THEN
   total = distkm * 2 + 1

IF psgnumb > 4 THEN
   total = total + ((psgnumb - 4) * 2)

PRINT "Journey price: £", total
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Image
User avatar
Yoriz
 
Posts: 1164
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: Need Simple Python Code! Help!

Postby gusgus » Mon Jan 20, 2014 11:19 pm

You have no idea how many tutorials I've read through to try and see what I had wrong and nothing helped...

Thank you very much. I'll quickly try that now before I go to sleep... I'll be back on tomorrow to check for more replies and tell you if it's working!

Thanks again! :)
Beginning to understand Python... New to the forum... :D
gusgus
 
Posts: 5
Joined: Mon Jan 20, 2014 8:02 pm

Re: Need Simple Python Code! Help!

Postby stranac » Tue Jan 21, 2014 8:48 am

gusgus wrote:Maybe it might just be the way I try to "run" it... I first typed it up on Python "IDLE" and then opened the file with Python "Command Line."

It opens and simply closes...

Sounds like you're doing something wrong...
Here's what I get when running your code from a terminal(and I'm pretty sure IDLE should show the error as well if you use it to run your code):
Code: Select all
  File "a.py", line 5
    else subtotal = 3
       ^
SyntaxError: invalid syntax
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1245
Joined: Thu Feb 07, 2013 3:42 pm

Re: Need Simple Python Code! Help!

Postby micseydel » Tue Jan 21, 2014 9:34 am

It "simply closes" because you're double-clicking it on Windows. That's normal Windows behavior that any good tutorial or instructor should warn you of if there's any mention of a Windows environment.

You can run your code with IDLE by pushing F5 when you're viewing your source code. I recommend against this though, since you're a beginner and it won't be as descriptive in telling you where your syntax error is as would be best for you.

Instead, you need to open a command line window, navigate using the cd command to your source code, and then run the command "python your_file.py" at the >. If it complains that it can't find Python then you need to add Python to your path.

You've written much more code than you should have assuming you wrote it each line one after another. Your fourth line is a clear syntax error, and before writing more code you should have discovered that (via testing), ceased coding, and fixed the problem before moving on. As a seasoned programmer, I am telling you this with your best interest: don't ever do again what you've done here. I've seen students with unmanageably large amounts of code which were unsalvageable in spite of the tens of hours of time painfully wasted.

Instead, test your code each line you write*. When considering that fourth line, you should look at similar code that you know works, and that you can even get to work. Take the code you find here which is very close to what you want in terms of structure. Get it to work on your computer so that you have a reference. Then, look at differences with your code and tweak as necessary. You won't be told you look too lazy to do your homework if you write
Hypothetical Student wrote:I got this code offer the internet
Code: Select all
# four lines

and I tried writing this code
Code: Select all
# four similar-ish lines

but I get a syntax error on the last line. Perhaps I don't have a programmer's eye for detail yet, could someone point out what I'm missing?

* With an if or else, it's illegal to have them without something to do in that case. You can use the pass statement to overcome this.
Code: Select all
if True:
    pass

Even veteran software engineers test code one line at a time when it's something they're not familiar with, because it's a lesson learned by at some point having not done it and regretted it. I assume you're in a course now through March. I seriously recommend you follow my advice. Whether you want to be a software engineer one day, or the course is simply a requirement and you just want it to be over with, it's worth learning to do right because it'll save you a lot of time.

By the way, you look like you might be confusing Python 2 and 3. It's hard to tell which version you're using from the code you've written. I'll leave it to you to Google the differences (the important ones are found easily) or to talk to your instructor about it. When you're looking at tutorials on the internet though, be careful to use one that is relevant to which version is used in your class unless you're confident you can understand the differences.
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1497
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Need Simple Python Code! Help!

Postby gusgus » Tue Jan 21, 2014 8:31 pm

Thanks a lot guys... I had a talk with my teacher today and he explained briefly what was going on. He did tell me that my code wasn't "elegant" and he explained why. Our next 5 lessons are going to be on Python! :D I will from now on test my code one line at time because I have noticed once you do something wrong you just have to completely discard what you've done because it's so difficult to put everything right, especially when I do indents which do nothing but cause problems!

Regarding Python 2 and 3... I have seen some differences and I think I use Python 3 in class and I'm sure I do at home. I am doing some more research to consolidate what makes them different.

I am looking forward to developing my understanding of Python and I will be using this forum... Forever. I am just about to retry answering the question and with everything you've said in mind, it shouldn't be extremely hard.

Thanks again everyone who answered.
Beginning to understand Python... New to the forum... :D
gusgus
 
Posts: 5
Joined: Mon Jan 20, 2014 8:02 pm


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 2 guests