01BTC10
VIP
Hero Member
Offline
Activity: 756
Merit: 503
|
|
October 13, 2013, 02:22:32 AM |
|
By the way there is a computer networks course starting in 3 months: https://www.coursera.org/course/comnetworksCourse 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
|
|
|
|
gangplank (OP)
Newbie
Offline
Activity: 28
Merit: 0
|
|
October 13, 2013, 02:23:32 AM |
|
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
|
|
|
|
|
01BTC10
VIP
Hero Member
Offline
Activity: 756
Merit: 503
|
|
October 13, 2013, 03:47:26 AM |
|
You can call a function anywhere you want in your code, even inside another function.
|
|
|
|
jambola2
Legendary
Offline
Activity: 1120
Merit: 1038
|
|
October 13, 2013, 10:53:49 AM |
|
I recommend trying a guide called learn python the hard way. It is really helpful and easy.
|
No longer active on bitcointalk, however, you can still reach me via PMs if needed.
|
|
|
FirstAscent
|
|
October 13, 2013, 03:27:12 PM |
|
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. # 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. 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: # convert inches to cm
def inch_to_cm(inch): cm = 2.54 / 1 * inch return cm
Try this: def inch_to_cm(inch): return inch * 2.54
|
|
|
|
01BTC10
VIP
Hero Member
Offline
Activity: 756
Merit: 503
|
|
October 13, 2013, 03:33:37 PM Last edit: October 13, 2013, 04:20:38 PM by 01BTC10 |
|
Nice 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: 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"
|
|
|
|
FirstAscent
|
|
October 13, 2013, 04:22:19 PM |
|
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.
|
|
|
|
01BTC10
VIP
Hero Member
Offline
Activity: 756
Merit: 503
|
|
October 13, 2013, 04:24:15 PM |
|
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: def miles_to_km(miles): return miles * 1.60934 It prints "11" If use this line: def miles_to_km(miles): return float(miles) * 1.60934 The answer is correct.
|
|
|
|
FirstAscent
|
|
October 13, 2013, 04:27:11 PM |
|
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: def miles_to_km(miles): return miles * 1.60934 The answer is "11" If use this line: def miles_to_km(miles): return float(miles) * 1.60934 The answer is correct. This program prints 1.60934. def miles_to_km(miles): return miles * 1.60934
print miles_to_km(1)
Show me your entire program here.
|
|
|
|
01BTC10
VIP
Hero Member
Offline
Activity: 756
Merit: 503
|
|
October 13, 2013, 04:30:42 PM |
|
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: def miles_to_km(miles): return miles * 1.60934 The answer is "11" If use this line: def miles_to_km(miles): return float(miles) * 1.60934 The answer is correct. This program prints 1.60934. 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.pyI think it's because that I convert a string to a float. It is not integer --> float.
|
|
|
|
FirstAscent
|
|
October 13, 2013, 04:41:40 PM |
|
I'm pretty sure I know what your problem is. First, what version of Python are you using. Type python at the command prompt.
|
|
|
|
FirstAscent
|
|
October 13, 2013, 04:46:37 PM |
|
Everything I said is correct. You're writing bad code. Here is a fixed version of your 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)"
|
|
|
|
01BTC10
VIP
Hero Member
Offline
Activity: 756
Merit: 503
|
|
October 13, 2013, 04:46:48 PM |
|
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. Traceback (most recent call last): File "test.py", line 13, in <module> print miles_input + " miles = " + str(miles_to_km_no_float(miles_input)) + " km (no float)" TypeError: unsupported operand type(s) for +: 'int' and 'str'
|
|
|
|
FirstAscent
|
|
October 13, 2013, 04:48:25 PM |
|
And that's the problem. Your toy Python interpreter did not catch the problem.
|
|
|
|
01BTC10
VIP
Hero Member
Offline
Activity: 756
Merit: 503
|
|
October 13, 2013, 04:49:08 PM |
|
Everything I said is correct. You're writing bad code. Here is a fixed version of your 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.
|
|
|
|
FirstAscent
|
|
October 13, 2013, 04:50:35 PM |
|
The problem is not with the functions. This is pointless: def miles_to_km_with_float(miles): return float(miles) * 1.60934
|
|
|
|
01BTC10
VIP
Hero Member
Offline
Activity: 756
Merit: 503
|
|
October 13, 2013, 04:51:26 PM |
|
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.
|
|
|
|
FirstAscent
|
|
October 13, 2013, 04:58:29 PM |
|
Whatever the case, you don't need to convert your integer to a float in the function, because multiplying it by a float already does that.
Also, I can't get a program to print the Python version to work in the online version.
For example, this does not work:
import platform print(platform.python_version())
|
|
|
|
01BTC10
VIP
Hero Member
Offline
Activity: 756
Merit: 503
|
|
October 13, 2013, 05:07:27 PM |
|
Whatever the case, you don't need to convert your integer to a float in the function, because multiplying it by a float already does that.
Also, I can't get a program to print the Python version to work in the online version.
For example, this does not work:
import platform print(platform.python_version())
I think it's an incomplete version 2.
|
|
|
|
|