I was presented with this question as part of my Intro to Prog & Logic class.
Problem 5: Write a simple program that the user can enter any number of positive and negative integer values and display the total number of positive values entered and the total number of negative values entered. We are going to use a sentinel value to control the loop. If the user wants to quit counting positive and negative numbers, the user will enter 'q'. The first line should display a title that says something like "Problem 5: Counting the number of positive and negative values entered by the user".
The trouble I am having is getting q to stop the loop. My professor does not like us using breaks (he takes points off every time you do) so thats where the trouble is coming in.
This is my code so far:
count_pos = 0
count_neg = 0
n = int(input('Enter the first number'))
while n != 'q':
if n > 0:
count_pos += 1
elif n < 0:
count_neg += 1
n = int(input('Enter the next number'))
print('The number of positive values entered is: ',count_pos)
print('The number of negative values entered is: ',count_neg)
Everything goes well until I try to stop the program and then I get the error "ValueError: invalid literal for int() with base 10: 'q'" If i switch the sentinel value to 0 there are no issues.
Any help would be appreciated, thank you!
Your code has:
n = int(input('Enter the first number'))
When the user enters "q", your program will attempt to call int("q")
. Python tries to convert "q" into an integer and finds that it can't do that because "q" is not a number. Therefore, Python raises a ValueError.
You can fix this by only casting the input into an integer once you are sure it is not "q".
# ...
raw_input = input('Enter the first number'))
while raw_input != 'q':
int_input = int(raw_input) # <-- Python will blow up here if raw_input can't be converted to an integer!
# ... check if int_input is positive or negative, etc.
raw_input = input('Enter the next number'))
# ...
Edit for clarification:
When coding in Python, it helps to remember the types of your variables. Consider the following line:
output = input * 2
What happens if input
is a number, such as 5
? Python will say, "input
is an integer, so * 2
should be interpreted as "multiply input
by 2". Therefore, output
will be 10
.
What happens if input
is a string, such as "hello"
? Python will instead say "input
is a string, so * 2
should be interpreted as "join the string together 2 times", resulting in hellohello
.
Problems will happen if you try to call operations that do not exist on that type, such as asking Python what the output is for "hello" < 5
. What does it mean if a string is less than 5? Python has no idea, so it will complain with a TypeError.
In a similar vein, if you tell Python to convert between types (like casting via int()
) but a conversion doesn't exist, Python will also complain. Imagine if someone asked you to treat "hello"
like a number. You'd probably be confused, just like Python.
If you need to brush up on what types exist in Python, you can find them online (i.e. here).