Ask coding questions

← Back to all posts
Someone pls help
BrendanD10 (4)

I don't know how to do this I have went back and watched videos and everything they didn't help.
Then create the following:
A loop that will output every other name in the names list.
A loop that will output only the positive numbers in the numbers list.
A loop that will output the sum of all the values in the numbers list.
A loop that will output only the numbers that are odd.
A loop that will output only the names that come before "Thor" in the alphabet from the names list.
A loop that will find the maximum or minimum value in the numbers list. This algorithm requires an additional variable that is assigned to the first element in the list. Then, in a loop compare each element to the variable. If the element is > (for max) or < (for min), assign the variable to the element. After the loop, print the variable.

Answered by IntellectualGuy (648) [earned 5 cycles]
View Answer
Comments
hotnewtop
IntellectualGuy (648)

1.

names_run = 0
for name in names:
   if names_run % 2 == 0:
       print(name)
   names_run += 1

2.

#For this you would use a nested for loop and if statement
#First you have your for loop to look through each number in your list
for integer in numbers:
   #Then you have an if else statement to see if it is more than 0, or positive
   if integer >= 0:
      print(f"The number {integer} is positive")

3.

#For this you would have just a for loop and a variable for the total
sum = 0
for integer in numbers:
   sum += integer
print(f"The sum of all your numbers is {sum}")

4.

#For this you would also have a nested loop
#First you have your for loop
for integer in numbers:
    #Then you have your if statement
    if integer % 2 == 1:
        print(f"The number {integer} is odd") 

5.

#For this you would have a nested loop
#First you have your for loop
for name in names:
   #Then you have an if statement to check if the name is Thor then it will stop the code
   if name == "Thor":
      #A break to stop the code if the name is Thor
      break
   #An else statement to print out the name if it is not Thor and comes before thor
   else:
      print(f"The name is {name}")

I don't understand exactly how to make the 6th loop,but I will try to do it, and will send it as soon as possible
Edit: I got it and here's how you would do it

6.

#If we chose minimum
number_chosen = numbers[0]    
for number in numbers:
	if number < number_chosen:
		number_chosen = number
print(number_chosen)
#If we chose maximum
number_chosen = numbers[0]
for number in numbers:
	if number > number_chosen:
		number_chosen = number
print(number_chosen)

If there are any errors when running this code please tell me and I will try to help, but anyways hope this helped you accomplish whatever you needed to.

BrendanD10 (4)

@IntellectualGuy Dude you are such a hero. I just hope you know you saved me and my class grade bc I am simply retarded XD TYSM!

SauravBastola (0)

@IntellectualGuy the questions asked for output all variables that come before the word "Thor" in the alphabet, so the word wanda wasn't supposed to be there. Could you show me how its done because my brain hurts.

InvisibleOne (2674)

Use a for loop

for name in names:
  print(name)
for number in numbers:
  if number > 0:
    print(number)
all = 0
for number in numbers:
  all += number
print(all)
for number in numbers:
  if number % 2 == 0:
    pass
  else:
    print(number)
#thor is not in your list of names
Kai_Justice (221)

What do you need help with exactly? I don't see any attempts in the attached repl.

BrendanD10 (4)

@Kai_Justice All my attempts I deleted bc they didn't work d: