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

In summary, the program should exit if the user types "exit" and it should also exit if the user types anything else (like a word).
  • #1
antoniacarol
1
0
TL;DR Summary
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
  • #2
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().
 
  • #3
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 WWGD and jedishrfu
  • #4
Moderator's note: Moved to programming forum, and added Python tag.
 
  • Like
Likes jedishrfu
  • #5
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.
 
  • #6
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.)
 
  • #7
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.
 
  • #8
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.
 

1. How do I identify a ValueError in my Python code?

A ValueError is a type of error that occurs when there is an issue with the value or data type being used in the code. To identify a ValueError, you can use the try-except block in Python. This will catch any errors that occur and allow you to handle them accordingly.

2. What causes a ValueError when trying to terminate a while loop?

A ValueError can occur when trying to terminate a while loop if the condition being used to end the loop is not being met. This means that the loop will continue to run indefinitely, resulting in a ValueError being thrown.

3. How can I fix a ValueError in my while loop?

To fix a ValueError in a while loop, you can check the condition being used to terminate the loop and ensure that it is being met. You can also use the break statement to exit the loop if the condition is not met, preventing a ValueError from occurring.

4. Can I use a try-except block specifically for ValueError in my while loop?

Yes, you can use a try-except block specifically for ValueError in your while loop. This allows you to catch and handle any ValueErrors that occur within the loop without affecting other types of errors that may occur.

5. Is there a way to prevent a ValueError from occurring in a while loop?

Yes, there are a few ways to prevent a ValueError from occurring in a while loop. One way is to carefully check the condition being used to terminate the loop and ensure that it is being met. Another way is to use the try-except block to catch any errors that may occur and handle them appropriately within the loop.

Similar threads

  • Programming and Computer Science
2
Replies
55
Views
4K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
3
Views
894
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
8
Views
882
  • Programming and Computer Science
Replies
14
Views
31K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
513
  • Programming and Computer Science
2
Replies
66
Views
4K
  • Programming and Computer Science
Replies
6
Views
1K
Back
Top