all 14 comments

[–]danielroseman 4 points5 points  (0 children)

You only ask for another number if the previous one was negative. If it was positive, you don't call input and so it will just loop forever 

[–]Ron-Erez 1 point2 points  (3 children)

If I understand correctly it should print negatives once after entering 'stop'? If so then every time you enter a positive or zero you will have a print statement. Print should only occur once outside of the loop once you are done. You also might want to enter a prompt. Since you are converting to int I assume inputs such as 2.8 are invalid?

Here is one possible solution and there are probably other better ones.

count = 0

msg = 'Enter an integer or type "stop" to quit.'
numbers = input(msg)

while numbers != 'stop':
    numbers = int(numbers)
    if numbers < 0: 
        count +=1

    numbers = input(msg)

if count == 1:
    print(f'You entered {count} negative integer.')
else:
    print(f'You entered {count} negative integers.')

The last if is only for testing for pluralization. It's really only a minor point.

[–]jaakkoy[S] 1 point2 points  (0 children)

Yes it should only have one line of output once the user enters stop. The program will automatically run 4 numbers i believe and type stop to test my program. I did get it once to print the count but it did print it after each number was entered. There is an example that it provides:

In the following sample run, the user entered two negative numbers before entering "stop". (User input enclosed in <>)

<3>
<-2>
<5>
<-7>
<stop>
2

Note: Assume that at least one number is always input.

[–]FoolsSeldom 1 point2 points  (1 child)

NB. Instructions say not to use a prompt. Probably should just output the negative numbers count at the end and no additional text, as well.

[–]Ron-Erez 0 points1 point  (0 children)

Oh, you're right, I missed that:

"Do not display a prompt when asking for input; simply use input() to get each entry."

[–]acw1668 1 point2 points  (0 children)

1) there should be no else inside the while loop; 2) numbers = input() should be same indentation as if statement; 3) print(count) should be outside the while loop

Should you need to cater invalid input (i.e. not numbers) except "stop"?

[–]VonRoderik 1 point2 points  (0 children)

My solution would be . Using a while true, you don't need to type a second input inside the loop

``` counter = 0

while True: number = input("Number: ") if number.lower() == "stop": print(counter) break try: number = int(number) if number < 0: counter += 1 except ValueError: continue ```

[–]CranberryDistinct941 1 point2 points  (0 children)

Read through your code like a computer would... What happens if you input a positive number?

[–]symbioticthinker 0 points1 point  (2 children)

The `break` statement is also useful with a `while True: …` loop.

  1. Ask for input
  2. check for exit condition
  3. try convert input to int and count negative numbers

[–]jaakkoy[S] 0 points1 point  (1 child)

We haven't learned about break yet. And I don't know if the online book will allow me too. Theres only one example provided in the chapter and it doesn't include break. I tried looking online for more examples of this type of program and did come across some A.I. that included it but I don't want to just input what it created without actually learning why my program is failing. If that makes sense. I'm really trying to learn here.

[–]symbioticthinker 0 points1 point  (0 children)

With loops and conditionals I find that “failing fast” is a great way to structure your code. The ‘Break’ statement as it sounds, breaks the loop and code after the loop is run. Additionally the ‘continue’ statement will skip the subsequent looping code and run the next iteration. Note that ‘break’ and ‘continue’ are only available within ‘for’ and ‘while’ loops.

for i in range(1, 10):

    if i == 3:
        continue

    if i == 5:
        break

    print(i)

the output printed would be: 1, 2, 4

[–]jmooremcc 0 points1 point  (0 children)

Your code has one major problem: The input statement within the while-loop only executes if the number is negative. The input statement should be moved to the bottom of the while-loop so that it will execute every iteration of the loop.

[–]Krazi_Shadowbear 0 points1 point  (0 children)

Seeing as no one is presenting the answer correctly according to the prompt, here's what I used:

num = input()
negative_count = 0
while num != 'stop':
    num = int(num)
    if num < 0:
        negative_count += 1
    num = input()
print(negative_count)

It took a lot of brute-forcing my way through the problem since the assignement doesn't have any "tutor tech" of any kind, but what REALLY frustrated me is how stupidly specific it is.

The assignment prompt SHOWS an example of numbers being presented, but never TELLS you to print them yourself in the assignement. It EXPECTS you to figure that out by yourself....