Perhaps you would accomplish this with something to the effect of
text = raw_input("please give 2 numbers to multiply separated with a comma:")
split_text = text.split(',')
a = int(split_text[0])
b = int(split_text[1])
# The last three lines could be written: a, b = map(int, text.split(','))
# but you may find the code I used a bit easier to understand for now.
if b > 0:
num_times = b
else:
num_times = -b
total = 0
# While loops with counters basically should not be used, so I replaced the loop
# with a for loop. Using a while loop at all is rare.
for i in xrange(num_times):
total += a
# We do this a times, giving us total == a * abs(b)
if b < 0:
# If b is negative, adjust the total to reflect this.
total = -total
print total
or maybe
a * b
Answer from Mike Graham on Stack OverflowTutorial Gateway
tutorialgateway.org › python-program-to-print-negative-numbers-in-a-list
Python Program to Print Negative Numbers in a List
September 4, 2018 - Fourth Iteration: for 3 in range(0, 4) – Condition is True if( -17 < 0 ) – Condition is True This Negative Number will print. Fifth Iteration: for 4 in range(0, 4) – Condition is False So, it exits from Python For Loop · This program for Negative numbers in a list is the same as the above. We just replaced the Python for Loop with the Python while loop.
PyTutorial
pytutorial.net › home › python program to print negative numbers in list
Python Program to Print Negative Numbers in List - 5 Ways
July 27, 2022 - a = [10,- 20, -30, 4, 0, -40, 60, -50] n = 0 while n < len(a): if a[n] < 0: print(a[n], end = ' ') n = n + 1 ... The below example allows users to enter the size and items. Next, the printLstNegNum(a) function finds and prints negative numbers in a list. def printLstNegNum(a): for n in a: if ...
Top answer 1 of 8
9
Perhaps you would accomplish this with something to the effect of
text = raw_input("please give 2 numbers to multiply separated with a comma:")
split_text = text.split(',')
a = int(split_text[0])
b = int(split_text[1])
# The last three lines could be written: a, b = map(int, text.split(','))
# but you may find the code I used a bit easier to understand for now.
if b > 0:
num_times = b
else:
num_times = -b
total = 0
# While loops with counters basically should not be used, so I replaced the loop
# with a for loop. Using a while loop at all is rare.
for i in xrange(num_times):
total += a
# We do this a times, giving us total == a * abs(b)
if b < 0:
# If b is negative, adjust the total to reflect this.
total = -total
print total
or maybe
a * b
2 of 8
4
Too hard? Your TA is... well, the phrase would probably get me banned. Anyways, check to see if numb is negative. If it is then multiply numa by -1 and do numb = abs(numb). Then do the loop.
Rensselaer Polytechnic Institute
cs.rpi.edu › ~sibel › csci1100 › spring2014 › course_notes › lec12_while.html
Lecture 12 — While Loops — Course Notes for CSCI-1100 1.0 documentation
sum = 0 while True: x = int( raw_input("Enter an integer to add (0 to end) ==> ")) if x == 0: break; sum += x print sum ... The while condition of True essentially means that the only way to stop the loop is when the condition that triggers the break is met. Suppose we want to skip over negative ...
TutorialsPoint
tutorialspoint.com › python-program-to-print-negative-numbers-in-a-list
Count positive and negative numbers in a list in Python program
July 4, 2020 - numbers = [1, -2, -4, 6, 7, -23, ... += 1 index += 1 print("Positive numbers in the list:", pos_count) print("Negative numbers in the list:", neg_count) Positive numbers in the list: 5 Negative numbers in the list: 3 · We ...
Programiz
programiz.com › python-programming › examples › positive-negative-zero
Python Program to Check if a Number is Positive, Negative or 0
To understand this example, you should have the knowledge of the following Python programming topics: ... num = float(input("Enter a number: ")) if num > 0: print("Positive number") elif num == 0: print("Zero") else: print("Negative number")
GeeksforGeeks
geeksforgeeks.org › python › python-program-to-print-negative-numbers-in-a-list
Python program to print negative numbers in a list - GeeksforGeeks
... Iterates through each element ... based on a condition efficiently. ... A traditional for loop can be used to iterate through the list and print negative numbers directly....
Published: November 13, 2025
Top answer 1 of 5
3
Using a while/else loop produces your desired behaviour.
- The code in the else doesn't run if the break in the while loop is encountered
Code
price= int(input("Enter the price: "))
price_list=[]
while price!= 0:
price_list.append(price)
if price< 0:
print("Wrong entry")
break
price=int(input())
price_sum= sum(price_list)
else:
print(f"Avg price is: {price_sum / len(price_list)}")
2 of 5
1
If you don't want to run rest of code when getting negative number, you can do something like this:
price= int(input("Enter the price: "))
ok = True
price_list=[]
while price!= 0:
price_list.append(price)
if price< 0:
print("Wrong entry")
ok = False
break
price=int(input())
if ok:
price_sum= sum(price_list)
print(f"Avg price is: {price_sum / len(price_list)}")
Stack Overflow
stackoverflow.com › questions › 74514540 › how-do-i-make-my-function-work-on-negative-numbers
python - How do I make my function work on negative numbers? - Stack Overflow
while b != 0: #loop while 'b' is odd number if (b % 2 != 0): answer = answer + a #<--------------- THE INSTRUCTION a = a*2 #double every 'a' integers # b = b//2 #halve the 'b' integers b = int(b/2) print(a, b) answer = answer + a #<--------------- THE INSTRUCTION · since you get your answer by just adding up A, you will have these two wrong scenarios ----> a positive and b negative ->gives you positive (the sign of A) when it should be negative ----> a negative and b negative -> gives you negative (the sign of A) when it should be positive
Tutorial Gateway
tutorialgateway.org › python-program-to-put-positive-and-negative-numbers-in-separate-list
Python Program to Put Positive and Negative Numbers in Separate List
December 19, 2024 - We just replaced the For Loop with While loop. # Python Program to Put Positive and Negative Numbers in Separate List NumList = [] Positive = [] Negative = [] j = 0 Number = int(input("Please enter the Total Number of List Elements : ")) for i in range(1, Number + 1): value = int(input("Please enter the Value of %d Element : " %i)) NumList.append(value) while(j < Number): if(NumList[j] >= 0): Positive.append(NumList[j]) else: Negative.append(NumList[j]) j = j + 1 print("Element in Positive List is : ", Positive) print("Element in Negative List is : ", Negative)
YouTube
youtube.com › codestack
how to print negative numbers in python - YouTube
Download this code from https://codegive.com Certainly! Printing negative numbers in Python is a straightforward process. In Python, you can use the print() ...
Published: January 18, 2024
Views: 23
CodeRivers
coderivers.org › blog › python-print-negative-number
Python Print Negative Numbers: A Comprehensive Guide - CodeRivers
February 22, 2026 - Python supports various data types for representing numbers, such as integers (int) and floating-point numbers (float), and both can be negative. # Integer negative number negative_integer = -5 print(type(negative_integer)) # Output: <class 'int'> # Floating-point negative number negative_float = -10.5 print(type(negative_float)) # Output: <class 'float'>
GeeksforGeeks
geeksforgeeks.org › videos › python-program-to-print-negative-numbers-in-a-range
Python Program to Print Negative Numbers in a Range - GeeksforGeeks | Videos
In this video, we will write a python program to print negative numbers in a range. Below is the list of approaches that we will cover in this section: 1. Program to find the negative numbers using loop 2. Program to find the negative numbers using lambda 4. Program to find the negative numbers using list comprehension To Print all negative numbers we will use different approaches such as loop, lambda function and list comprehension.
Published: October 10, 2022
Views: 1K
HCL GUVI
studytonight.com › python-programs › python-program-to-print-negative-numbers-in-a-list
HCL GUVI | Learn to code in your native language
Supports JavaScript, Python, Ruby, and 20+ programming languages.Explore IDE