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 OverflowPerhaps 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
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.
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)}")
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)}")
The abs function expects an int, but in the first code block you pass it a string. In the second code block, you convert the string to an int -- this is missing in the first code block.
So combine the two:
ival = abs(int(rawstr))
A second issue is that isnumeric is a method for strings, not for numbers, so don't use that as you did in the first code block, and do ival >= 0 as if condition.
So:
rawstr = input('enter a number: ')
try:
ival = abs(int(rawstr))
except:
ival = -1
if ival >= 0:
print('nice work')
else:
print('not a number')
The downside is that with abs you really have a non-negative number and lost the sign.
Merge the two parts and do:
rawstr = input('enter a number: ')
try:
ival = int(rawstr)
print('nice work')
except:
print('not a number')
# Here you can exit a loop, or a function,...
Python inputs are strings by default, we need to convert them to relevant data types before consuming them.
Here in your code:
at the line ival = abs(rawstr), the rawtr is always a string, but abs() expects an int type.
So you will get an exception at this line, so if ival.isnumeric() always receives ival as boob which was not a numeric, so you're getting not a number output.
Updated code to fix this:
rawstr = input('enter a number: ')
try:
ival = str(abs(int(rawstr)))
except:
ival = 'boob'
if ival.isnumeric():
print('nice work')
else:
print('not a number')