## 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?

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.
hatchet43

Posts: 4
Joined: Fri Nov 22, 2013 4:47 pm

### Re: Why won't this work?

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.

stranac

Posts: 1790
Joined: Thu Feb 07, 2013 3:42 pm

### Re: Why won't this work?

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?

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   0246810`

Due to the reasons discussed here we will be moving to python-forum.io on October 1, 2016.

This forum will be locked down and no one will be able to post/edit/create threads, etc. here from thereafter. Please create an account at the new site to continue discussion.

micseydel

Posts: 3000
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

### Re: Why won't this work?

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
Python 2.7
Mac Mini (Desktop/server)
Macbook Air
Laptop w/ Ubuntu 14.04
mckryall

Posts: 116
Joined: Sat Nov 23, 2013 6:13 pm

### Re: Why won't this work?

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: 203
Joined: Tue Jun 04, 2013 7:53 pm

### Re: Why won't this work?

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.)
Due to the reasons discussed here we will be moving to python-forum.io on October 1, 2016.

This forum will be locked down and no one will be able to post/edit/create threads, etc. here from thereafter. Please create an account at the new site to continue discussion.

micseydel

Posts: 3000
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

### Re: Why won't this work?

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?

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.010 50.020 68.030 86.040 104.050 122.060 140.070 158.080 176.090 194.0100 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.010 50.020 68.030 86.040 104.050 122.060 140.070 158.080 176.090 194.0100 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.010 50.020 68.030 86.040 104.050 122.060 140.070 158.080 176.090 194.0100 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.010 50.020 68.030 86.040 104.050 122.060 140.070 158.080 176.090 194.0100 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.
Due to the reasons discussed here we will be moving to python-forum.io on October 1, 2016.

This forum will be locked down and no one will be able to post/edit/create threads, etc. here from thereafter. Please create an account at the new site to continue discussion.

micseydel

Posts: 3000
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

### Re: Why won't this work?

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?

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_tempmy_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.03 37.47 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: 689
Joined: Thu Apr 04, 2013 1:17 pm
Location: Germany, Europe

### Who is online

Users browsing this forum: Google [Bot], Yahoo [Bot] and 8 guests