[Tutor] decision loops

Alan Gauld alan.gauld at btinternet.com
Sun Feb 1 15:19:53 CET 2015


On 01/02/15 01:54, Daniel M wrote:

> I can’t figure out how to make the script switch between the two. What I
> would like it to do is let the user go back to the “What do you wish to
> convert?” part when a character is entered instead of a number for
> “temperature?”. I tried using

That's quite a complex task you've set yourself. You need to 
differentiate numbers and characters even though you read
them all as characters from the users. It might be simpler
just to keep the numeric and character processing separate
for now. And maybe for good.

Also your subject says 'decision loops'
Those are two completely separate things.

decisions are made using if/elif/else constructs
loops are repetitions involving 'for' or 'while'
constructs (and a few more advamced ones later)

You probably need both to solve your problem but do not confuse
or merge the two ideas in your mind. At this stage we appear to only be 
dealing with decisions. The loops can wait till later.

> elif m == *char*
>      print (*"What do you wish to convert to?"*)
>      temp = raw_input(*">> "*)

The asterisks don't make sense, I'm guessing your mail program put them 
in because you made it bold or some-such? Please always use plain text 
when sending code.

However, we can't really make much sense of it even without asterisks 
because we have no context. We don't know what 'm' is, where it comes 
from etc.
Also m == char only makes sense if you have defined a variable called 
char somewhere, again we can't see it. Or are you simply trying to 
explain that you want to test m to see if it is a character?
Its not clear. Don't make us guess.

> but it seems useless regardless of where I put it. It gives me the error “
> return eval(raw_input(prompt))

And that line doesn't seem to appear in your code anywhere?
And its only a bit of an error message, please always send us
the whole error because its full of useful information.


>    File "<string>", line 1, in <module>
>
> NameError: name 't' is not defined” when I enter a character.

This suggests that you passed a 't' to eval. The 't' must have come from 
the raw_input() but again you didn't tell us that we have
to guess. I assume you get a slightly differnt error if you enter,
say, a 'v' or a 'w'?

> missing something very obvious but I can’t seem to figure it out.

Probably but you aren't giving us enough specific detail to be
able to help you reliably.


> def *ftoc*(x): #Fahrenheit to Celsius
>      x == float
>      y = x-32.0
>      z = y * 5.0
>      return z //9.0

x = float doesn't do anything useful here. It assigns a
new name (x) to the float type convertion function.
I'm guessing(again) that what you really meant was

x = float(x)

to force x to be a floating point number?

Also // produces integer division. I'm pretty sure
you want regular float division z/9.0

> def *ctof*(x):  #Celsius to Fahrenheit
>      x == float
>      y = x * 9.0
>      z = y // 5.0
>      return z + 32.0

Pretty much the same comments as above

> print (*"What do you wish to convert to?"*)
> temp = raw_input(*">> "*)

> while  temp == *"c"* or temp == *"f"* and not temp == *"q"*:
>      if temp == *"c"*:
>          m = float(input(*"temperature?"*))
>          print ftoc(m)
>          print *"Celcius"*
>      elif temp == *"f"*:
>          m = float(input(*"temperature?"*))
>          print ctof(m)
>          print (*"Farenheit"*)
>      elif temp == *"q"*:
>          break

OK, Now we see where 'm' fits in, although your char test above is missing.

Also we see you using input() instead of raw_input()
That's nearly always a mistake. Its better to use raw_input() and then 
explicitly convert to the type you want(which you do here anyway)

So use:

       m = float(raw_input("temperature?"))

Finally you probably want the unit selection inside the while loop
so try something like this skeleton code:

while  True:
      temp = raw_input("What do you wish to convert to?")
      if temp == 'q': break
      if temp == "c":
           m = float(raw_input("temperature?"))
           print ftoc(m)
           print "Celcius"
      elif temp == "f":
           m = float(raw_input("temperature?"))
           print ctof(m)
           print ("Farenheit")


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list