Skip to content
Sign UpLog In
Profile icon

jn1003491

@jn1003491
Repls
Community
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
Lists
Python
btfuss
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
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
RYANTADIPARTHI
RYANTADIPARTHI
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
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
Lists
Python
JBloves27
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
RYANTADIPARTHI
RYANTADIPARTHI
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
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
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?
Lists
Python
Coder39182
Coder39182
temp = 0 for i in numbers: temp += int(i) print(temp) `2 years ago
SixBeeps
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
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?
Lists
Python
robowolf
robowolf
Try, for num in numbers: if num >0: print(num) `2 years ago
jn1003491
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
Lists
Python
RYANTADIPARTHI
RYANTADIPARTHI
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
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
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
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
More Loops
Python
RYANTADIPARTHI
RYANTADIPARTHI
Solution you cannot use print in input. A input prints itself. So your code should look like this. And you need to assign it a value to use it. and for the else statement, you should unindent it, since it's not in the look anymore. guesses = 1 d = input ("Try to guess my favorite color. ") while d != "aqua": print ("That is not my favorite color.") d = input ("Try to guess my favorite color. ") guesses = guesses + 1 print ("That's my favorite color!") print ("It took you " + str(guesse2 years ago
Coder100
Coder100
that's not how you take user input. input is a function that returns the user input lol guesses = 1 color = input ("Try to guess my favorite color. ") while color != "aqua": print ("That is not my favorite color.") color = input ("Try to guess my favorite color. ") guesses = guesses + 1 print ("That's my favorite color!") print ("It took you " + str(guesses) + " guesses.") do you need to take a python tutorial?2 years ago
JBloves27
JBloves27
Well, an input cannot be named input. You can also NOT print an input. So what you have to do is name the input. Like this: a = input() Then fix the while statement to whatever you named your input. Like this: while a != “aqua”: That was an example. I hope this helps!2 years ago