Bitcoin Forum
May 25, 2024, 10:46:17 AM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: « 1 [2] 3 »  All
  Print  
Author Topic: Programming in Python  (Read 2652 times)
01BTC10
VIP
Hero Member
*
Offline Offline

Activity: 756
Merit: 503



View Profile
October 13, 2013, 02:22:32 AM
 #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
gangplank (OP)
Newbie
*
Offline Offline

Activity: 28
Merit: 0


View Profile
October 13, 2013, 02:23:32 AM
 #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
gangplank (OP)
Newbie
*
Offline Offline

Activity: 28
Merit: 0


View Profile
October 13, 2013, 03:39:04 AM
 #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
01BTC10
VIP
Hero Member
*
Offline Offline

Activity: 756
Merit: 503



View Profile
October 13, 2013, 03:47:26 AM
 #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.
jambola2
Legendary
*
Offline Offline

Activity: 1120
Merit: 1038


View Profile
October 13, 2013, 10:53:49 AM
 #25

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
Hero Member
*****
Offline Offline

Activity: 812
Merit: 1000


View Profile
October 13, 2013, 03:27:12 PM
 #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
01BTC10
VIP
Hero Member
*
Offline Offline

Activity: 756
Merit: 503



View Profile
October 13, 2013, 03:33:37 PM
Last edit: October 13, 2013, 04:20:38 PM by 01BTC10
 #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"

FirstAscent
Hero Member
*****
Offline Offline

Activity: 812
Merit: 1000


View Profile
October 13, 2013, 04:22:19 PM
 #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.
01BTC10
VIP
Hero Member
*
Offline Offline

Activity: 756
Merit: 503



View Profile
October 13, 2013, 04:24:15 PM
 #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.
FirstAscent
Hero Member
*****
Offline Offline

Activity: 812
Merit: 1000


View Profile
October 13, 2013, 04:27:11 PM
 #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.
01BTC10
VIP
Hero Member
*
Offline Offline

Activity: 756
Merit: 503



View Profile
October 13, 2013, 04:30:42 PM
 #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.
FirstAscent
Hero Member
*****
Offline Offline

Activity: 812
Merit: 1000


View Profile
October 13, 2013, 04:41:40 PM
 #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.
FirstAscent
Hero Member
*****
Offline Offline

Activity: 812
Merit: 1000


View Profile
October 13, 2013, 04:46:37 PM
 #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)"
01BTC10
VIP
Hero Member
*
Offline Offline

Activity: 756
Merit: 503



View Profile
October 13, 2013, 04:46:48 PM
 #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 <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
Hero Member
*****
Offline Offline

Activity: 812
Merit: 1000


View Profile
October 13, 2013, 04:48:25 PM
 #35

And that's the problem. Your toy Python interpreter did not catch the problem.
01BTC10
VIP
Hero Member
*
Offline Offline

Activity: 756
Merit: 503



View Profile
October 13, 2013, 04:49:08 PM
 #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.
FirstAscent
Hero Member
*****
Offline Offline

Activity: 812
Merit: 1000


View Profile
October 13, 2013, 04:50:35 PM
 #37

The problem is not with the functions. This is pointless:

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

01BTC10
VIP
Hero Member
*
Offline Offline

Activity: 756
Merit: 503



View Profile
October 13, 2013, 04:51:26 PM
 #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.
FirstAscent
Hero Member
*****
Offline Offline

Activity: 812
Merit: 1000


View Profile
October 13, 2013, 04:58:29 PM
 #39

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 Offline

Activity: 756
Merit: 503



View Profile
October 13, 2013, 05:07:27 PM
 #40

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.
Pages: « 1 [2] 3 »  All
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!