How can I fix a ValueError when trying to terminate a while loop in Python?

Click For Summary

Discussion Overview

The discussion revolves around troubleshooting a Python program designed to convert Celsius to Fahrenheit and vice versa, specifically focusing on handling user input and terminating a while loop correctly. Participants are addressing a ValueError encountered when the user inputs "exit".

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant describes the intended functionality of the program, including how it should respond to user inputs like "exit" and other strings.
  • Another suggests using a break statement or sys.exit(0) to terminate the loop, but the effectiveness of this approach is debated.
  • A participant questions why the interpreter attempts to convert the string "exit" to a float, indicating a misunderstanding in the code's flow.
  • It is noted that the ValueError arises from trying to convert the string "exit" to a float, and that checks for special values should occur before the conversion attempt.
  • Some participants clarify that the program does not need to call sys.exit() if it can exit the loop and reach the end of the code naturally.
  • There is confusion expressed regarding the while-else construct in Python, with one participant indicating a lack of familiarity with this feature.
  • Concerns are raised about the fundamental issues in the code that may require more than minor adjustments.

Areas of Agreement / Disagreement

Participants express differing views on the best approach to handle the termination of the loop and the handling of user input. There is no consensus on the specific solution to the ValueError issue, and the discussion remains unresolved.

Contextual Notes

Participants highlight that the error occurs due to the timing of input handling and conversion attempts, suggesting that the checks for specific input values should be placed strategically in the code.

Who May Find This Useful

This discussion may be useful for individuals learning Python programming, particularly those interested in input handling and error management in loops.

antoniacarol
Messages
1
Reaction score
0
TL;DR
Creating a python code--need help terminating a while loop
Please help!

I am trying to create a Python program for an assignment that will convert Celsius to Fahrenheit and vice versa depending on the input from the user. The numeric conversion portion of the code is working, however, if the user inputs "exit" the while loop is supposed to terminate and output "Goodbye". Additionally, if the user inputs anything else (like a word) the program is supposed to output "I do not understand. Please try again.".

I have tried moving the exit statement before temp value, I have tried saying "if temp =="exit" " and these have all resulted with the same error.
Whenever I input "exit" there is a ValueError stating it cannot convert string to float: "exi".

Please see the code I have so far in the photo below:
Screen Shot 2019-11-25 at 11.21.59 AM.png
 
Technology news on Phys.org
Try placing a break statement or a sys.exit(0) statement following the print("Goodbye") statement. The break will do stuff after the loop where the sys.exit(0) will terminate the script then and there.

An example of break and continue statements:

https://www.tutorialspoint.com/python/python_loop_control.htm

An example, of sys.exit():

https://www.programcreek.com/python/example/2/sys.exit

Just typing exit while your program is running will do nothing unless the program recognizes it and calls sys.exit().
 
If you post your code by copying and pasting text between [CODE=python][/CODE] tags instead of a screenshot it makes it much easier to help.

Read what the error message is telling you. Why do you think the interpreter is trying to convert a string "exi" to a numerical float value when you respond to the input prompt with the string "exit"? How do you think you can fix this?

Look at the line if input == "exit":. What do you think the value of input could be at this point?

Where do you think the code will go after the line print("... please try again")? Is that what you want to happen?
 
  • Like
Likes   Reactions: WWGD and jedishrfu
Moderator's note: Moved to programming forum, and added Python tag.
 
  • Like
Likes   Reactions: jedishrfu
Immediately after you take the input you treat it as numeric (float) but if the input is the character string 'exit', it isn't numeric, so you get a data type exception.
 
antoniacarol said:
Whenever I input "exit" there is a ValueError stating it cannot convert string to float: "exi".

That's because at the line of code tempvalue=float(temp[:-1]), which is where the ValueError is being raised, the value of the temp variable is the string "exit", because that's what you typed. And Python can't convert that string to a float, which is what the line of code is telling it to do, so it raises ValueError.

What you need to do is check for the special values of the temp variable before you try converting it to a float. This is more or less what the code in your else block is trying to do, but those checks don't belong there, they belong in between the line where the temp variable is filled in with what the user types, and the line I quoted above where the float conversion is done. (Also, checking the value of input in an if statement makes no sense, because input is a built-in function. The string the user types in is in the variable temp.)
 
jedishrfu said:
Just typing exit while your program is running will do nothing unless the program recognizes it and calls sys.exit().

The program doesn't need to call sys.exit(), it just needs to exit whatever loop it is in and reach the end of the code. The interpreter will exit automatically when that happens.
 
jedishrfu said:
Try placing a break statement or a sys.exit(0) statement following the print("Goodbye") statement.

This would be a no-op since after that print statement the program will reach the end of its code and exit anyway. That's not the problem the OP is having.
 
  • #10
antoniacarol said:
I am trying to create a Python program for an assignment...
Not sure why the thread has been moved here from a homework forum?

As I hinted in my post #3 there are some pretty fundamental problems here which need more than a couple of tweaks - but the OP hasn't been back anyway.
 
  • #11
pbuk said:
Not sure why the thread has been moved here from a homework forum?

It wasn't, it was moved from Computing and Technology because it's specifically about programming.
 
  • #12
PeterDonis said:
It wasn't, it was moved from Computing and Technology because it's specifically about programming.
Sorry, my bad.
 

Similar threads

Replies
55
Views
7K
Replies
1
Views
2K
  • · Replies 28 ·
Replies
28
Views
5K
Replies
2
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 0 ·
Replies
0
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
Replies
3
Views
2K
Replies
6
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K