Receiving Integers from the User Until They Enter 0

Receiving integers from the user until they enter 0 [closed]

try:
count = 0
while True:
user = int(input('Insert Number: '))
count += user
if user == 0:
break
print(count)
except ValueError:
print('Please Insert Numbers Only!')

How can I keep prompting the user for input until the user enters an integer and non-0 value on Python using While loop?

You could do something like this:

wrong = True
while wrong:
try:
number = input("please enter a non-zero number: ")
if isinstance(int(number), int) and int(number) != 0:
print('You entered: ' + number)
y = int(number)
wrong = False
else:
print("make sure it's a nonzero number only!")
except:
print("try again please.")

z = x/y

print('The answer is: ' + str(z))

How to continue receiving numbers until a zero is entered and count total positive and negative numbers the user entered.in the textfield

UPDATE

To achieve this, you can create a positive and negative variable outside of the count function and then check each input value; if the value entered zero, you can call your alert function and print the result.

Here is the working code:

var posCount = 0;
var negCount = 0;

function printOutput () {
alert("Positive numbers: " + posCount + "\nNegative numbers: " + negCount);

}

function check(){

let btnClear = document.querySelector('button');
let inputs = document.querySelectorAll('input');



var x = parseInt(document.getElementById("num").value);
document.getElementById("num").value = ''

if (x > 0) {
posCount++;
}else if (x < 0){
negCount++;
} else {
printOutput();
// re-initiate value
posCount = 0;
negCount = 0;
}


}
<!DOCTYPE html>
<html>
<head>
<title>Exercise Two</title>
<link href="ex2.css" rel="stylesheet">
<script type="text/javascript" src="ex2.js"></script>
</head>
<body>

Enter number (Press 0 to stop)
<input type="text" id="num">
<button id="submit-btn" onclick="check()">Submit</button>

</body>
</html>

Java program that reads user input until 0 is entered, also makes a few calculations (evens/odds, average, etc.)

When reading the first numbers to populate large and small we only need to do that once. And without re-reading the numbers with in.nextInt() because that will eat up the next inputs that are entered, which was probably causing the not terminating at zero error.

while ((input = in.nextInt()) != 0) {

if (counter == 0)
small = large = input;

if (input != 0)
sum = input + sum;
counter++;

if (input > large)
large = input;

if (input < small)
small = input;

if (input % 2 == 0)
even = even + 1;
else
odd = odd + 1;

}

Java program that accepts int values until the user enters a zero, and then finds the minimum positive integer

I would recommend using while loop instead of dowhile loop

System.out.println("Write a list of integers and type 0 when you are finished");
Scanner kb=new Scanner(System.in);
int input=kb.nextInt();
int smallestValue=Integer.MAX_VALUE;

while(input!=0){//loop until user inputs 0
if(input>0) {//check for inputs greater than 0 and ignore -ve values
if(input<smallestValue) // save the smallest value
smallestValue = input;
}
input=kb.nextInt();
}
System.out.println(smallestValue+">0");

C loop until 0 is given

  1. There are no loops in your code

  2. The condition (num < 0 && num > 0) is alway false. A number can't be: less than zero AND greater than zero

  3. Wrong call of scanf

You probably want:

printf("Give me a number \n");
if (scanf("%d", &num) != 1) exit(1);
while(num != 0){
total = total + num;
i = i + 1;
printf("Give me a number \n");
if (scanf("%d", &num) != 1) exit(1);
}

The above solution will exit the program if the user inputs a non-integer, e.g. a letter.

An alternative solution that will end the loop on non-integer input is:

printf("Give me a number \n");
while(scanf("%d", &num) == 1 && num != 0){
total = total + num;
i = i + 1;
printf("Give me a number \n");
}

This last solution can even be written a little more compact - like:

while(printf("Give me a number \n"), scanf("%d", &num) == 1 && num != 0){
total = total + num;
i = i + 1;
}

It's a bit harder to read but avoids duplicated lines.



Related Topics



Leave a reply



Submit