Profile icon

# jn1003491

@jn1003491
shared a Post
2 years ago
How can I use a loop to find the minimum or maximum in a list?
I have a list of numbers and I want to use a loop to find the minimum or maximum in the list. I can’t figure out how to get this, so if someone could
Python
btfuss
good function for it: chk = True def checkList(lst): errorList.clear() ele = lst[0] chk = True for item in lst: if ele != item: chk = False errorList.append(item) return chk2 years ago
JBloves27
Hello, You do not have to use a for loop, it is not needed. This is because Python already has a function. It is called the min and max. Here is how you use them: names = ["Peter", "Bruce", "Steve", "Tony", "Natasha", "Clint", "Wanda", "Hope", "Danny", "Carol"] numbers = ["100", "50", "10", "1", "2", "7", "11", "17", "53", "-8", "-4", "-9", "-72", "-64", "-80"] print(min(numbers)) print(max(numbers)) I hope this helps!2 years ago
Solution you don't really need to use a for loop. you can use methods. min() and max(). It's much easier. numbers = ["100", "50", "10", "1", "2", "7", "11", "17", "53", "-8", "-4", "-9", "-72", "-64", "-80"] print(min(numbers)) print(max(numbers)) # i don't think max works. so another solution another is to sort. numbers = ["100", "50", "10", "1", "2", "7", "11", "17", "53", "-8", "-4", "-9", "-72", "-64", "-80"] d = sorted(numbers) print(d[0], d[-1]) like that. That should work2 years ago
jn1003491
shared a Post
2 years ago
I have a list of numbers, but I only want the odd ones to print. What should I do to make this happen? Also, in my list “names”, I want to print only
Python
JBloves27
So to do this, you have to do the following: numbers = ["100", "50", "10", "1", "2", "7", "11", "17", "53", "-8", "-4", "-9", "-72", "-64", "-80"] for i in numbers: if int(i) % 2 != 0: print(i) I hope this helps!2 years ago
Solution like this. use % numbers = ["100", "50", "10", "1", "2", "7", "11", "17", "53", "-8", "-4", "-9", "-72", "-64", "-80"] for i in numbers: if int(i) % 2 != 0: print (i) like that. That should work2 years ago
WWEMONSTERMONST
To print the odd numbers from your list you can check if each number is a multiple of 2, if it is then it is even else its odd: numbers = ["100", "50", "10", "1", "2", "7", "11", "17", "53", "-8", "-4", "-9", "-72", "-64", "-80"] for num in numbers: if int(num) % 2 != 0: print (num) For your second question. You can simply sort the names and print while the name is not Thor (btw there is no Thor in list) and when its Thor you can break the loop. names = ["Peter", "Bruce", "Steve", "Tony"2 years ago
jn1003491
shared a Post
2 years ago
I have a list of numbers, and I want to add them all together. How do I do that?
Python
Coder39182
temp = 0 for i in numbers: temp += int(i) print(temp) `2 years ago
SixBeeps
You could make a variable that starts at 0, then in a for-loop add whatever number you're on to whatever number you're on. Something like: sum = 0 for num in numbers: sum += num `2 years ago
jn1003491
shared a Post
2 years ago
I have a list of numbers, but I only want the positive numbers to be printed on the screen. What should I do?
Python
robowolf
Try, for num in numbers: if num >0: print(num) `2 years ago
jn1003491
shared a Post
2 years ago
I have a list of names and I’m trying to get my program to print every other name on the list. I can’t figure out how to do this, so if anyone could h
Python
Solution there is a very easy way to do this. Just use a slice method. And this works perfectly to print every other name. try this. names = ["Peter", "Bruce", "Steve", "Tony", "Natasha", "Clint", "Wanda", "Hope", "Danny", "Carol"] print(names[::2]) bingo! That works. Like that. That should work2 years ago
JBloves27
Hello, To print all you just do the following: print(names) print(numbers) However, to do certain ones, like you said every other one, you do the following: rand_num=0 for i in names: print(names[rand_num]) rand_num+=2 I hope this helps!2 years ago
Coder39182
names = ["Peter", "Bruce", "Steve", "Tony", "Natasha", "Clint", "Wanda", "Hope", "Danny", "Carol"] numbers = ["100", "50", "10", "1", "2", "7", "11", "17", "53", "-8", "-4", "-9", "-72", "-64", "-80"] print(names[::2]) print(numbers[::2]) Output: ['Peter', 'Steve', 'Natasha', 'Wanda', 'Danny'] ['100', '10', '2', '11', '53', '-4', '-72', '-80'] is a compact and easy method to do so2 years ago
jn1003491
shared a Post
2 years ago
I’ve been trying to fix this code for over two hours and I can’t find what’s wrong. I might just be over looking something but if anyone could tell me
Python