My python loop is either freezing up or running once - python

The purpose of this program is to have a user input numbers until they enter done, at which point the program outputs the maximum & minimum numbers. If the user enters something that is not a number, the program says "invalid input" and skips to the next time the loop runs. I have a try/except as well. This program is written in Python 3. when I run without the continue at the bottom, my screen freezes & I have to restart. With it, I get a pop-up saying SyntaxError: bad input on line 25.
largest = 0
largest = float(largest)
smallest = 0
smallest = float(smallest)
num = input('Enter a number: ')
num = float(num)
while True:
if num == "done":
break
try:
float(num)
except:
print('Invalid Input')
continue
if num > largest:
largest = num
if smallest == 0:
smallest= num
if smallest > num:
smallest = num
print(num)
continue
print("Maximum is", largest)
print("Minimum is", smallest)

Your input is outside of your loop so it only asks once and since it doesn't ever change its value to "done", it loops forever.
largest = 0
largest = float(largest)
smallest = 0
smallest = float(smallest)
# num = input("Enter a number: ")
while True:
num = input("Enter a number: ")
if num == "done":
break
try:
num = float(num)
float(num)
except:
print("Invalid Input")
continue
if num > largest:
largest = num
if smallest == 0:
smallest = num
if smallest > num:
smallest = num
print(num)
continue
print("Maximum is", largest)
print("Minimum is", smallest)

Related

Python3 ParseError

I was trying to do a simple task, a script that figures out which of numbers inputed were largest of smallest. Here is the code:
largest = None
smallest = None
while True:
num = input("Enter a number: ")
if num == "done" :
break
try :
num = int(num)
except :
print('Invalid input')
continue
if smallest is None :
smallest = num
elif:
smallest > num
smallest = num
if largest is None :
largest = num
elif :
largest < num
largest = num
print("Maximum", largest)
When I am trying to run the code, it tells me 'ParseError: bad input on line 15'
Could anybody help me?
The correct syntax for elif is the same as for normal if statements:
Instead of:
elif:
smallest > num
smallest = num
Do:
elif smallest > num:
smallest = num
Same for your other elif further down.

Finding the largest and the smallest in Python 2.7

I had an assignment, where I let users choose random numbers, and this program would find the smallest and the largest between all these numbers, and if a number is not entered it would consider it an invalid input.
Here's the code I wrote:
largest = None
smallest = None
while True:
inp = raw_input("Enter a number: ")
if inp == 'done': break
if len(inp) < 1: break
try:
num = int(inp)
except:
print "Invalid input"
continue
if smallest is None:
smallest = num
elif inp < smallest:
smallest = num
if largest is None:
largest = num
elif inp > largest:
largest = num
print "Maximum is %r" % (largest)
print "Minimum is %r" % (smallest)
The question is, why is it not working ?
Here's some random numbers I tried and got something weird.
Here I executed the code in the first part and got weird answers, and I executed it again with different numbers and got it right.
You've mixed up < and > and you're comparing inp which is a string to smallest and largest instead of num.
Your indentation is wrong and you made a lot of misprints. Look at this code.
In if-then-else you have to compare num with smallest and largest but not inp.
largest = None
smallest = None
while True:
inp = raw_input("Enter a number: ")
if inp == 'done': break
try:
num = int(inp)
except:
print "Invalid input"
continue
print num
if smallest is None: smallest = num
elif num < smallest: smallest = num
if largest is None: largest = num
elif num > largest: largest = num
print "Maximum is %r" % (largest)
print "Minimum is %r" % (smallest)

Python 2.7 - Trying to find the min and max of numbers input by the user

The code below is printing the values of max_num and min_num as "None". I am an absolute beginner so any info is much appreciated.
Thanks in advance!
while True:
number = raw_input("Please enter a number: ")
num_list = list()
max_num = None
min_num = None
if number == "done":
print "The maximum number is:", max_num
print "The minimum number is:", min_num
break
try:
number = int(number)
num_list.append(number)
for num in num_list:
if max_num is None or num > max_num:
max_num = num
if min_num is None or num < min_num:
min_num = num
except:
print "You did not enter a number"
You set num_list, max_num and min_num to None at the beginning of your loop.
while True:
number = raw_input("Please enter a number: ")
num_list = list() #these
max_num = None #three
min_num = None #lines
This means that every iteration (i.e. after you enter each number) they get reset to None.
Move these outside the loop.
Note: since you don't actually use the list elements elsewhere, you might as well not keep track. (try block omitted in following code snippet)
max = None
min = None
while True:
number = int(input("Enter a number: "))
if max is None or number > max:
max = number
if min is None or number < min:
min = number
Your mistakes are:
inistialization of max_num and min_num inside the loop
saving numbers in a list is not necessary to calculate max and min.
you can replace the try instruction by using isdigit test.
Fixes for your code:
max_num = 0
min_num = 9999999999
while True:
number = raw_input("Please enter a number: ")
if number == "done":
break
if number.isdigit():
number = int(number)
if min_num> number:
min_num = number
if max_num < number:
max_num = number
else:
print "You did not enter a number"
print "The maximum number is:", max_num
print "The minimum number is:", min_num
--
An alternative solution using a list:
num_list = []
while True:
number = raw_input("Please enter a number: ")
if number == "done":
break
if number.isdigit():
num_list.append(int(number))
else:
print "You did not enter a number"
print "The maximum number is:", max(num_list)
print "The minimum number is:", min(num_list)

Python 2.7 debugging basic code

So I submitted this for a Cousera course on basic python (I'm very very new to writing code). It worked. However, I found a bug in it (which I'm both glad I can see and heartbroken because it exists).
The problem is that if the smallest number in a string of numbers is the first number given it gets absorbed by the largest variable and not into the smallest variable. How would I prevent this from happening?
Thanks in advance!
Code below:
largest = -1
smallest = None
while True:
try:
num = raw_input('number\n')
float(num)
if num > largest:
largest = num
elif smallest is None:
smallest = num
elif smallest > num:
smallest = num
except:
if num =="Done":
print "Maximum is", largest
print "Minimum is", smallest
break
print "Invalid input"
Welcome to the world of programming!
Usually when I do min/max comparisons, I start with negative and positive infinity and not None/null or the first element of the list as that has given me headaches in the past.
This code seems to work fine to me.
largest = float("-inf")
smallest = float("inf")
while True:
try:
num = raw_input('number: ')
num = float(num) # I reassign num for good-measure
if num > largest:
largest = num
if num < smallest:
smallest = num
except:
if num == "Done":
print "Maximum is", largest
print "Minimum is", smallest
break
print "Invalid input"
Purely as an educational exercise, you could use some of python's features (generators) to make this code more generic.
This creates a generator called numbers which returns a sequence of input numbers, that is collected as a list in a and then you can use the builtin min(a), max(a) methods:
def numbers():
while True:
try:
num = input('number\n')
yield float(num)
except ValueError:
if num == "Done":
break
print "Invalid input"
a = list(numbers())
print "Maximum is", max(a)
print "Minimum is", min(a)
You should be able to solve this by setting smallest and largest to the first number input, and doing this before your while loop. That way, for every new number you see, you can update the values of smallest and largest depending on its relative magnitude. Something like this:
largest = -1
smallest = None
try:
num1 = raw_input('number\n')
smallest = num1
largest = num1
while True:
num = raw_input('number\n')
num = float(num)
if num > largest:
largest = num
elif smallest > num:
smallest = num
except:
if num =="Done":
print "Maximum is", largest
print "Minimum is", smallest
break
print "Invalid input"

How do i print smaller value?

I am very new to python, I write below few lines of code but not able to print smallest number from the input.
largest = 0
smallest = 0
while True:
num = raw_input("Enter a number:")
if num == "done" : break
try:
num = int(num)
except:
print "invalid"
continue
if num is smallest:
smallest = num
if num > largest:
largest = num
print "Minimum", smallest, "Maximum", largest
below is my output, it does print correct largest value (whatever i input) but smallest is always zero.
Enter a number:34
Enter a number:12000
Enter a number:dds
invalid
Enter a number:done
Minimum 0 Maximum 12000=====> see minimum is "0" should be 34?
That's the expected behaviour. In fact, your second statement reads:
smallest = 0
0 is less than 34, so smallest won't be updated.
A possible solution is initializing smallest and largest from the first element read:
largest = None
smallest = None
while True:
num = raw_input("Enter a number:")
if num == "done" : break
try:
num = int(num)
except:
print "invalid"
continue
if smallest is None: # or largest is None, it's the same
smallest = num
largest = num
if num < smallest:
smallest = num
if num > largest:
largest = num
print "Minimum", smallest, "Maximum", largest
Besides, you have what appears to be a typo or a misconception about the operator is. This:
if num is smallest:
smallest = num
Should be:
if num < smallest:
smallest = num

Categories