Please help me with Python

Post » Wed Dec 07, 2011 9:07 am

Hi, so I have to take programming courses in my university. We already had the first class today, but I was absent since I had to go to the doctor. We already got our first homework assignment: Write a program that converts temperature from Celsius to Fahrenheit.

I have no experience in programming at all and since I was absent from the class, I have no idea how to do it. I know that there must be some programmers here, would you be so kind to explain to me step-by step, how to do it.

Thanks :)
User avatar
Shannon Lockwood
 
Posts: 3373
Joined: Wed Aug 08, 2007 12:38 pm

Post » Wed Dec 07, 2011 4:32 am

Hi, so I have to take programming courses in my university. We already had the first class today, but I was absent since I had to go to the doctor. We already got our first homework assignment: Write a program that converts temperature from Celsius to Fahrenheit.

I have no experience in programming at all and since I was absent from the class, I have no idea how to do it. I know that there must be some programmers here, would you be so kind to explain to me step-by step, how to do it.

Thanks :)


Not to be rude (and I'm sure you'll get your answer here) but can't you just go and see the lecturer and explain you missed the first lecture?? Maybe borrow a copy of the notes off a friend?? If there's other important information in the lecture notes, you'll probably want to grab them, and getting behind in the first week is not a good idea :D.
User avatar
Charles Mckinna
 
Posts: 3511
Joined: Mon Nov 12, 2007 6:51 am

Post » Wed Dec 07, 2011 12:00 am

Not to be rude (and I'm sure you'll get your answer here) but can't you just go and see the lecturer and explain you missed the first lecture?? Maybe borrow a copy of the notes off a friend?? If there's other important information in the lecture notes, you'll probably want to grab them, and getting behind in the first week is not a good idea :D.

yeah, I already got the notes and I'm trying to make sense of them. :) I think I've got it *almost* figured out. I mean I know the formula for converting, but I can't make python to do the operations in right order. I think I'll manage to do it by myself, still if anyone would like to share some advise, feel welcome to do so :)
User avatar
yessenia hermosillo
 
Posts: 3545
Joined: Sat Aug 18, 2007 1:31 pm

Post » Wed Dec 07, 2011 5:00 am

yeah, I already got the notes and I'm trying to make sense of them. :) I think I've got it *almost* figured out. I mean I know the formula for converting, but I can't make python to do the operations in right order. I think I'll manage to do it by myself, still if anyone would like to share some advise, feel welcome to do so :)


Posting what you've got would be a good start, so you can learn where you went wrong and why :D.
User avatar
Amy Cooper
 
Posts: 3400
Joined: Thu Feb 01, 2007 2:38 am

Post » Wed Dec 07, 2011 4:45 am

Help with Python ...?

Uh, what about a quick lobotomy, then starting to learn programming in a sane language? :D
User avatar
Eduardo Rosas
 
Posts: 3381
Joined: Thu Oct 18, 2007 3:15 pm

Post » Wed Dec 07, 2011 7:03 am

yeah, I already got the notes and I'm trying to make sense of them. :) I think I've got it *almost* figured out. I mean I know the formula for converting, but I can't make python to do the operations in right order. I think I'll manage to do it by myself, still if anyone would like to share some advise, feel welcome to do so :)

parenthesis are your friend ;)
User avatar
Samantha Jane Adams
 
Posts: 3433
Joined: Mon Dec 04, 2006 4:00 pm

Post » Tue Dec 06, 2011 10:29 pm

Hey everyone, I managed to do it by myself. Here's how I did it:
text1 = input ("Enter the temperature in Celsius")
a = int (text1) * 9/5
b= 32
Print ("The temperature in Fahrenheit is: ", end="")
print ( a+b )

This works for me. What do you think? Could it be done better?
Sorry to act like a noob, but this is the first time I've programmed anything :)
User avatar
Kelsey Anna Farley
 
Posts: 3433
Joined: Fri Jun 30, 2006 10:33 pm

Post » Wed Dec 07, 2011 12:55 am

Hey everyone, I managed to do it by myself. Here's how I did it:
text1 = input ("Enter the temperature in Celsius")
a = int (text1) * 9/5
b= 32
Print ("The temperature in Fahrenheit is: ", end="")
print ( a+b )

This works for me. What do you think? Could it be done better?
Sorry to act like a noob, but this is the first time I've programmed anything :)


And now try entering "5.3" Celsuis as the temperature. Or, for that matter, "Elephant".
User avatar
patricia kris
 
Posts: 3348
Joined: Tue Feb 13, 2007 5:49 am

Post » Wed Dec 07, 2011 10:02 am

And now try entering "5.3" Celsuis as the temperature. Or, for that matter, "Elephant".

Crap, you know I never even thought about it :banghead:
Edit: Okay, all it took was to change int to float. Well, at least I'm learning :D
Edit2: As for the "Elephant" part... is there really anything that can be done with it? I mean no-one can expect to get an answer if they enter words instead of numbers. Or am I missing anything? :cryvaultboy:
User avatar
Alberto Aguilera
 
Posts: 3472
Joined: Wed Aug 29, 2007 12:42 am

Post » Wed Dec 07, 2011 11:44 am

Edit2: As for the "Elephant" part... is there really anything that can be done with it? I mean no-one can expect to get an answer if they enter words instead of numbers. Or am I missing anything? :cryvaultboy:


Yes. You missed the part about exceptions and their handling. :)

The answer looks like follows (assuming you want to round the number you output):

import systext1 = raw_input ("Enter the temperature in Celsius: ")try:    a = float (text1) * 9.0 / 5.0except ValueError:    print ( "Sorry, but '" + text1 + "' is not a number I can recognise.")    sys.exit()b = 32.0print ( "The temperature in Fahrenheit is: " + str(int(a + b + 0.5)))


And seriously: Consider learning a sane language too, if you are at all interested in programming.
User avatar
Rachell Katherine
 
Posts: 3380
Joined: Wed Oct 11, 2006 5:21 pm

Post » Wed Dec 07, 2011 5:50 am

Yes. You missed the part about exceptions and their handling. :)

The answer looks like follows (assuming you want to round the number you output):

import systext1 = raw_input ("Enter the temperature in Celsius: ")try:    a = float (text1) * 9.0 / 5.0except ValueError:    print ( "Sorry, but '" + text1 + "' is not a number I can recognise.")    sys.exit()b = 32.0print ( "The temperature in Fahrenheit is: " + str(int(a + b + 0.5)))


And seriously: Consider learning a sane language too, if you are at all interested in programming.

Bad idea to do homework for someone, especially when you don't know how far along they are and may have done things that they haven't even come close to learning and therefore stinks of "someone else did this for me"

And as to your sane language comment... http://xkcd.com/353/

Edit2: As for the "Elephant" part... is there really anything that can be done with it? I mean no-one can expect to get an answer if they enter words instead of numbers. Or am I missing anything? :cryvaultboy:

That depends on how far along you are in programming. It's definitely expected once you are further along, but probably not in the beginning. Python will also handle "elephant" a lot more gracefully than many languages would in your situation.
User avatar
Abel Vazquez
 
Posts: 3334
Joined: Tue Aug 14, 2007 12:25 am

Post » Wed Dec 07, 2011 4:17 am

Bad idea to do homework for someone, especially when you don't know how far along they are and may have done things that they haven't even come close to learning and therefore stinks of "someone else did this for me"

And as to your sane language comment... http://xkcd.com/353/


That depends on how far along you are in programming. It's definitely expected once you are further along, but probably not in the beginning. Python will also handle "elephant" a lot more gracefully than many programs would in your situation.

Well, the first chapter doesn't say anything about exceptions, so I doubt they expect us to do that :)


And seriously: Consider learning a sane language too, if you are at all interested in programming.

I am interested in programming. Python is the first language we learn (because it's supposedly really simple), then we'll move to other ones. :celebration:

But I'm curious now, what's wrong with Python?
User avatar
Brooks Hardison
 
Posts: 3410
Joined: Fri Sep 07, 2007 3:14 am

Post » Wed Dec 07, 2011 3:56 am

Well, the first chapter doesn't say anything about exceptions, so I doubt they expect us to do that :)


In this case, just failing is fine. But don't use input() in Python, that's the equivalent of saying "... and I love surprise buttseks, too!". It evaluates whatever you put into it, including statements which can format your hard drive. :)

Use raw_input().

I am interested in programming. Python is the first language we learn (because it's supposedly really simple), then we'll move to other ones. :celebration:

But I'm curious now, what's wrong with Python?


Too long to mention here, really. :D Maybe another day, when I have some time.

Your program, as it stands, has another error, though you might not be expected to know how to fix it yet.

Hint 1: Temperatures below ?273.15 °C don't make sense.

Hint 2: There's an "if" expression in Python to help catch such errors. ;)

EDIT: Also, try using a really big number for your tests. What about "1e400" (a "1" followed by 400 "0")? :) Again: Only fixable with exception handling, really.
User avatar
Jennifer May
 
Posts: 3376
Joined: Thu Aug 16, 2007 3:51 pm


Return to Othor Games