Re: [Tutor] decision loops
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 "", line 1, in 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 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] decision loops
Hello Daniel, and welcome! On Sat, Jan 31, 2015 at 08:54:48PM -0500, Daniel M wrote: > Hello. I'm a complete beginner and I’m trying to write a very basic script > to convert temperatures, just for some practice. I have that part down, but > 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 > > elif m == *char* > > print (*"What do you wish to convert to?"*) > > temp = raw_input(*">> "*) Why are there asterisks around parts of your code? That's a syntax error in Python. I'm guessing that you didn't type them yourself, and that your email program is doing it. You should set your email program to only send plain text, not "Rich Text" or formatted text or HTML or whatever silly thing it is trying to do. > but it seems useless regardless of where I put it. It gives me the error “ > return eval(raw_input(prompt)) In the code you show below, there is no mention of eval. So that is your first mistake: whether you intended to or not (I'm pretty sure it wasn't deliberate!) you are telling us falsehoods. The code you say you are running is not the code you are actually running. How can we tell what you are doing wrong when we cannot see what you are doing? Mistake number two is using eval. As a beginner, there are three rules you should remember about eval: (1) If you think you might need to use eval, you don't. (2) If you are positive that you really do need to use eval, you probably don't. (3) For experts only -- if you are sure that you need to use eval, you might. The problems with eval are: - its slow - its tricky to use right except for the simplest cases - it can be dangerous and introduce serious security holes in your code if you aren't careful > File "", line 1, in > > NameError: name 't' is not defined” when I enter a character. Mistake number three: I'm guessing that you didn't enter any old character. I'm guessing you entered 't' rather than 's' or '3' or '.' or some other character. Your mistake is to make us guess: when asking for help, you should tell us what you did specifically, not vaguely. In this case, I think I can reproduce your problem: py> eval(raw_input("Enter a character: ")) Enter a character: t Traceback (most recent call last): File "", line 1, in File "", line 1, in NameError: name 't' is not defined I don't get that error from any random character, only from t. If I type a different character, I get a different error. Why? Because of eval. You're telling Python to evaluate what you typed as if it were code: py> eval(raw_input("Enter a character: ")) Enter a character: len([1, 2, 3]) + 1000 1003 You don't need eval here. It does nothing useful. What you want is simply raw_input(prompt). Hope that helps. -- Steve ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor