Pages:
Author

Topic: Programming in Python - page 2. (Read 2734 times)

vip
Activity: 756
Merit: 503
October 13, 2013, 11:51:26 AM
#38
And that's the problem. Your toy Python interpreter did not catch the problem.
I know it's meant to make it easy for student to share the link for their homework but the python implementation lack some feature.
hero member
Activity: 812
Merit: 1000
October 13, 2013, 11:50:35 AM
#37
The problem is not with the functions. This is pointless:

Code:
def miles_to_km_with_float(miles):
    return float(miles) * 1.60934

vip
Activity: 756
Merit: 503
October 13, 2013, 11:49:08 AM
#36
Everything I said is correct.

You're writing bad code. Here is a fixed version of your code:

Code:
def miles_to_km_no_float(miles):
    return miles * 1.60934

def miles_to_km_with_float(miles):
    return float(miles) * 1.60934

miles_input=input('Please input miles to convert in km:')
print str(miles_input) + " miles = " + str(miles_to_km_no_float(miles_input)) + " km (no float)"

print str(miles_input) + " miles = " + str(miles_to_km_with_float(miles_input)) + " km (float)"

It works fine using python on my computer but still get the same old result with codeskulptor.
hero member
Activity: 812
Merit: 1000
October 13, 2013, 11:48:25 AM
#35
And that's the problem. Your toy Python interpreter did not catch the problem.
vip
Activity: 756
Merit: 503
October 13, 2013, 11:46:48 AM
#34
I'm pretty sure I know what your problem is. First, what version of Python are you using. Type python at the command prompt.

There is no command prompt. It's a web browser implementation for the course. It works fine with codeskulptor but throw an error with python 2.7.3 on my computer.

Code:
Traceback (most recent call last):
  File "test.py", line 13, in
    print miles_input + " miles = " + str(miles_to_km_no_float(miles_input)) + " km (no float)"
TypeError: unsupported operand type(s) for +: 'int' and 'str'
hero member
Activity: 812
Merit: 1000
October 13, 2013, 11:46:37 AM
#33
Everything I said is correct.

You're writing bad code. Here is a fixed version of your code:

Code:
def miles_to_km_no_float(miles):
    return miles * 1.60934

def miles_to_km_with_float(miles):
    return float(miles) * 1.60934

miles_input=input('Please input miles to convert in km:')
print str(miles_input) + " miles = " + str(miles_to_km_no_float(miles_input)) + " km (no float)"

print str(miles_input) + " miles = " + str(miles_to_km_with_float(miles_input)) + " km (float)"
hero member
Activity: 812
Merit: 1000
October 13, 2013, 11:41:40 AM
#32
I'm pretty sure I know what your problem is. First, what version of Python are you using. Type python at the command prompt.
vip
Activity: 756
Merit: 503
October 13, 2013, 11:30:42 AM
#31
An integer multiplied by a float returns a float. Since 1.60934 is a float, even if miles is an integer, the result will be a float.

If miles == 1 with this code:
Code:
def miles_to_km(miles):
    return miles * 1.60934

The answer is "11"  Huh

If use this line:

Code:
def miles_to_km(miles):
    return float(miles) * 1.60934

The answer is correct.

This program prints 1.60934.

Code:
def miles_to_km(miles):
    return miles * 1.60934

print miles_to_km(1)

Show me your entire program here.

http://www.codeskulptor.org/#user19_9Zu5nZ2ctm_3.py

I think it's because that I convert a string to a float. It is not integer --> float.
hero member
Activity: 812
Merit: 1000
October 13, 2013, 11:27:11 AM
#30
An integer multiplied by a float returns a float. Since 1.60934 is a float, even if miles is an integer, the result will be a float.

If miles == 1 with this code:
Code:
def miles_to_km(miles):
    return miles * 1.60934

The answer is "11"  Huh

If use this line:

Code:
def miles_to_km(miles):
    return float(miles) * 1.60934

The answer is correct.

This program prints 1.60934.

Code:
def miles_to_km(miles):
    return miles * 1.60934

print miles_to_km(1)

Show me your entire program here.
vip
Activity: 756
Merit: 503
October 13, 2013, 11:24:15 AM
#29
An integer multiplied by a float returns a float. Since 1.60934 is a float, even if miles is an integer, the result will be a float.

If miles == 1 with this code:
Code:
def miles_to_km(miles):
    return miles * 1.60934

It prints "11"  Huh

If use this line:

Code:
def miles_to_km(miles):
    return float(miles) * 1.60934

The answer is correct.
hero member
Activity: 812
Merit: 1000
October 13, 2013, 11:22:19 AM
#28
An integer multiplied by a float returns a float. Since 1.60934 is a float, even if miles is an integer, the result will be a float.
vip
Activity: 756
Merit: 503
October 13, 2013, 10:33:37 AM
#27
Nice  Smiley

Code:
def miles_to_km(miles):
    return miles * 1.60934

def km_to_miles(km):
    return km / 1.60934

print miles_to_km(10)
print km_to_miles(16.0934)

Edit:
Improved version that assumes that the user only input numbers:

Code:
def miles_to_km(miles):
    return float(miles) * 1.60934

def km_to_miles(km):
    return float(km) / 1.60934

miles_input = input('Please input miles to convert in km:')
print miles_input + " miles = " + str(miles_to_km(miles_input)) + " km"

km_input = input('Please input km to convert in miles:')
print km_input + " km = " + str(km_to_miles(km_input)) + " miles"

hero member
Activity: 812
Merit: 1000
October 13, 2013, 10:27:12 AM
#26
Working on the functions lesson at the moment, I didn't really understand it at first. I think I've got it now as I tried to write some of my own and it outputs the correct answer. Maybe someone can check that my thinking is right on these functions I wrote.

Quote
# converts cm to inches

def cm_to_inches(cm):
    inch = 1 / 2.54 * cm
    return inch

a1 = cm_to_inches(25.4)
print a1

Less lines are better when it doesn't reduce clarity. The reason is it allows more lines visible on the screen, which allows greater comprehension while examining the code. But don't sacrifice white space to achieve that.

Quote
def cm_to_inches(cm):
    return cm / 2.54

And there's no point in dividing by 1 or multiplying by 1 or zero when they are constants:

Quote
# convert inches to cm

def inch_to_cm(inch):
    cm = 2.54 / 1 * inch
    return cm

Try this:

Quote
def inch_to_cm(inch):
    return inch * 2.54
legendary
Activity: 1120
Merit: 1038
October 13, 2013, 05:53:49 AM
#25
I recommend trying a guide called learn python the hard way.
It is really helpful and easy.
vip
Activity: 756
Merit: 503
October 12, 2013, 10:47:26 PM
#24
Is this about right for creating a function and then creating another which calls the previous function?

http://www.codeskulptor.org/#user19_v3vzRwYgMq_1.py
You can call a function anywhere you want in your code, even inside another function.
newbie
Activity: 28
Merit: 0
October 12, 2013, 10:39:04 PM
#23
Is this about right for creating a function and then creating another which calls the previous function?

http://www.codeskulptor.org/#user19_v3vzRwYgMq_1.py
newbie
Activity: 28
Merit: 0
October 12, 2013, 09:23:32 PM
#22
Yep you got it
Just two remarks: do you know you can "print cm_to_inches(1.0)" directly? And why writing ones in the formulas? Why not just "cm=2.54*inch"?

The example used in the course is fahrenheit to celsius but I couldn't seem to grasp it. I tried thinking of a few different ways I might use a function and those are just the things that came to mind. Obviously they are not useful as you've provided better options but for the purposes of understanding how a function operates, it seems to have worked for me Smiley
vip
Activity: 756
Merit: 503
October 12, 2013, 09:22:32 PM
#21
By the way there is a computer networks course starting in 3 months: https://www.coursera.org/course/comnetworks

Quote
Course Syllabus:
Introduction, Protocols and Layering
Physical and Link layers
Retransmissions, Multiple access, Switching
Network layer, Internetworking
Intra- and Inter-domain Routing
Transport layer, Reliability
Congestion Control
DNS, Web/HTTP, Content Distribution
Quality of Service and Real-time Apps
Network Security
vip
Activity: 756
Merit: 503
October 12, 2013, 09:18:04 PM
#20
do you know you can "print cm_to_inches(1.0)" directly?
You are right but he did it the way it is taught in the video. I guess it is easier for beginner to understand.
vip
Activity: 756
Merit: 503
October 12, 2013, 09:15:51 PM
#19
You can share your code with CodeSkulpor for easier review: http://www.codeskulptor.org/#user19_HWWqVRQDa8_0.py
I don't know the exact formula but the code looks good to me.

I just finished everything for this week.  Cheesy
Pages:
Jump to: