Why won't this work?

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

Why won't this work?

Postby hatchet43 » Mon Nov 25, 2013 8:26 pm

Been trying to make script like this work for a few days now and I am frustrated. I don't want to have the raw input stuff, i just want have a list.
Code: Select all
def ctof():
   celsius = int(range(0,101,10))
   
   fahrenheit = c_to_f(celsius)
   print (str(celsius) + str(fahrenheit))
   print ("")
Last edited by stranac on Mon Nov 25, 2013 8:54 pm, edited 1 time in total.
Reason: Added code tags. Locked.
hatchet43
 
Posts: 4
Joined: Fri Nov 22, 2013 4:47 pm

Re: Why won't this work?

Postby stranac » Mon Nov 25, 2013 8:56 pm

What do you want it to do? What you're trying to do here makes no sense.

Also, read this to learn how to post code properly(and other useful stuff).
Friendship is magic!

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

Re: Why won't this work?

Postby hatchet43 » Tue Nov 26, 2013 2:44 am

I am trying to make it so when i run ctof it prints a list of celsius temperatures with them their fahrenheit equivalent, and now that I read it again, I realize I am missing a line that should be in there... c_to_f = 9.0/5.0 * (celsius) +32. More so i want to know why i can't use range in the celsius = int(range(0,101,10)) line.
hatchet43
 
Posts: 4
Joined: Fri Nov 22, 2013 4:47 pm

Re: Why won't this work?

Postby micseydel » Tue Nov 26, 2013 2:52 am

int() is a function which creates an int type from a string or a floating point number. To turn a list, such as one return by range(), into an int doesn't make sense. Here's how we use range() usually
Code: Select all
>>> for even_num in range(0, 11, 2):
   print even_num

   
0
2
4
6
8
10

You'll want to adapt this logic to your code.
Join the #python-forum IRC channel on irc.freenode.net!
User avatar
micseydel
 
Posts: 928
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Why won't this work?

Postby mckryall » Tue Nov 26, 2013 6:50 am

On line 5, do something similar to this:
Code: Select all
print celsius, fahrenheit

or, less efficiently, this:
Code: Select all
print "%d\n%d" % (celsius, fahrenheit)

Python is a language that prides itself in being simple. I'm not sure where you got your ideas on strings and integers, but you're overcomplicating things.
Edit: Also, what's with "print ("")"? You don't need to use parentheses for print and I have no idea why you're printing nothing in the first place.
Last edited by Mekire on Sat Nov 30, 2013 7:35 am, edited 1 time in total.
Reason: Locked
I'M ARCHITECTING THE SHIT OUT OF THESE PRISONS
Python 2.7
OSX; Ubuntu 12.10; Windows 7; iOS
MacBook air; (Nonfunctioning)Desktop PC
Steam; PSN
mckryall
 
Posts: 55
Joined: Sat Nov 23, 2013 6:13 pm

Re: Why won't this work?

Postby ochichinyezaboombwa » Tue Nov 26, 2013 7:17 am

mckryall wrote:Python is a language that prides itself in being simple. I'm not sure where you got your ideas on strings and integers, but you're overcomplicating things.
Edit: Also, what's with "print ("")"?


Well I am almost sure that the OP uses
Code: Select all
print ("")
to print a new line; and as long as (s)he uses (most likely) Python 3.X that's what is right thing to do.
BTW it works with Python 2.X too.
ochichinyezaboombwa
 
Posts: 200
Joined: Tue Jun 04, 2013 7:53 pm

Re: Why won't this work?

Postby micseydel » Tue Nov 26, 2013 7:35 am

mckryall wrote:
Code: Select all
print celsius, fahrenheit

or, less efficiently, this:
Code: Select all
print "%d\n%d" % (celsius, fahrenheit)

Firstly, those are not equivalent in their output. Secondly, I would not worry about that "efficiency" one single bit. Readability is significantly more of a concern here?

("Why?", might you ask? Because usually big-O notation is usually all that matters, and the operation above is I/O so even micro-optimizations aren't what they normally are. In fact, the first example does three separate I/O writes, whereas the second one just does two, so would likely be "faster" if you're concerned with a very, very minor difference.)
Join the #python-forum IRC channel on irc.freenode.net!
User avatar
micseydel
 
Posts: 928
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Why won't this work?

Postby hatchet43 » Tue Nov 26, 2013 8:14 pm

Thanks for the help. I understand now that when i use "range" it needs to be part of a for loop. But I closer to my goal. I am using python v2.7. I am trying to make a print out list of Celsius temperatures on the left side, with the Fahrenheit equivalent on the right. For example

celsius = 10
fahrenheit = celsius*(9/5)+32
print celsius, fahrenheit
10 42

I was trying to make a range for the Celsius temperatures (0-100, step 10) - is there a way to do this with out putting it into a loop? and being a bit more direct and simple?
hatchet43
 
Posts: 4
Joined: Fri Nov 22, 2013 4:47 pm

Re: Why won't this work?

Postby micseydel » Tue Nov 26, 2013 9:26 pm

Python doesn't have implicit loops as a language like R does (and maybe Matlab?). The closet thing is something like this, which uses a comprehension, a form of embedded for loop.
Code: Select all
>>> print('\n'.join('{} {}'.format(c, ctof(c)) for c in range(0, 101, 10)))
0 32.0
10 50.0
20 68.0
30 86.0
40 104.0
50 122.0
60 140.0
70 158.0
80 176.0
90 194.0
100 212.0

That code is equivalent* to
Code: Select all
>>> lines = ['{} {}'.format(c, ctof(c)) for c in range(0, 101, 10)]
>>> print('\n'.join(lines))
0 32.0
10 50.0
20 68.0
30 86.0
40 104.0
50 122.0
60 140.0
70 158.0
80 176.0
90 194.0
100 212.0

which is further equivalent to
Code: Select all
>>> lines = []
>>> for c in range(0, 101, 10):
   lines.append('{} {}'.format(c, ctof(c)))

   
>>> print('\n'.join(lines))
0 32.0
10 50.0
20 68.0
30 86.0
40 104.0
50 122.0
60 140.0
70 158.0
80 176.0
90 194.0
100 212.0

If you're familiar with functional programming, the comprehension is equivalent to
Code: Select all
>>> print '\n'.join(map(lambda c: '{} {}'.format(c, ctof(c)), range(0, 101, 10)))
0 32.0
10 50.0
20 68.0
30 86.0
40 104.0
50 122.0
60 140.0
70 158.0
80 176.0
90 194.0
100 212.0
Comprehensions are syntactic sugar for map().

* Do note that some of these solutions use different amounts of memory in a meaningful way. Don't worry about it for now, but do know that it can come up for large inputs where memory can matter.
Join the #python-forum IRC channel on irc.freenode.net!
User avatar
micseydel
 
Posts: 928
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Why won't this work?

Postby hatchet43 » Wed Nov 27, 2013 5:38 am

I have not done any programming since high school and that's a long damn time ago. (15 years ish) and I find previous skill set has been in hindrance to learning python. I am now spending some quality time with python for kids book. Thanks so much!
hatchet43
 
Posts: 4
Joined: Fri Nov 22, 2013 4:47 pm

Re: Why won't this work?

Postby Kebap » Wed Nov 27, 2013 11:19 am

I can relate to that high school part, so I offer another solution, which will try and take it more step-by-step:

Code: Select all
def ctof(c_temp):
  f_temp = c_temp * (9 / 5.0) + 32
  return f_temp

my_celsius = [0, 3, 7]
my_fahrenheit = []

for celsius_temperature in my_celsius:
  my_fahrenheit.append(ctof(celsius_temperature))

my_results = zip(my_celsius, my_fahrenheit)
for c, f in my_results:
  print c, f


Results in:
Code: Select all
0 32.0
3 37.4
7 44.6

I wanted to show, how a function should do one thing only. If you want to feed it a list of values to calculate, it makes sense for me to do it where you call the function. So I prepare the list there, then call the function for each value in the list, then use the results to populate the other list, finally combine and print the results.

In python, "direct and simple" means more easily readable and writable code (for humans). That is why I don't bother if my solution has more lines than the above one-liners. I tend to get brain freeze, when I try and decipher too complex one-liners. Can only imagine how overwhelmed beginners must feel.
Learn: How To Ask Questions The Smart Way
Join the #python-forum IRC channel on irc.freenode.net and chat with uns directly!
Kebap
 
Posts: 282
Joined: Thu Apr 04, 2013 1:17 pm
Location: Germany, Europe


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 2 guests