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
There are no loops in your code
The condition
(num < 0 && num > 0)
is alway false. A number can't be: less than zero AND greater than zeroWrong 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
Replace Single Quote With Double Quote in a String Python
How to Get Max Output from a While Loop
How to Convert Datetime by Removing Nanoseconds
How to Start a Background Process in Python
Copy All Values in a Column to a New Column in a Pandas Dataframe
Programme to Print Mulitples of 5 in a Range Specified by User
In Python, How to Split a String and Keep the Separators
Print the Student Name and the Score of Student in Python3
Max Value of List Without Max() Method
How to Get Text from Span Tag in Beautifulsoup
Python Db-Api: Fetchone VS Fetchmany VS Fetchall
How to Do a Conditional Count After Groupby on a Pandas Dataframe
Json.Decoder.Jsondecodeerror: Expecting Value: Line 1 Column 1 (Char 0) Python
Update Json Element in Json Object Using Python
Move Files Between Two Aws S3 Buckets Using Boto3