Skip to content
Sign upLog in
← Back to Community

My apologies but I need help...again

Profile icon

If anyone could please help me with this it would mean a lot...
In the first part of your program, create a for loop that runs three times:
Inside the for loop, prompt the user for an integer
Prompt the user for another integer
Call the function compare (you are going to create this function next)
Pass the variables that you used for the integer inputs from above
Create a function called compare (remember the function definition should go at the top of the program) and use two variables in the parameters of the function:
Inside of the function, create an if / elif / else structure that compares the two values passed into the function
If one value is less than the other, output that to the user (Ex: 2 is less than 4)
Elif the other value is less than the other output something similar (Ex: 4 is less than 9)
Else, output that they are equal to each other
That is it for the first part of the program.
Next, create an empty list called names.
Create a loop that runs 6 times:
Inside of the for loop, prompt the user for a name
Append the name to the list
Outside of the for loop, prompt the user for how many people they would like to vote off the island.
Call the function eliminate and pass the variable you used from step 7 to it.
Also, this function will return a value, so store this back function call back to a new variable.
Create a function called eliminate and create a variable to use as the parameter:
Inside the function, randomly shuffle (use the shuffle() method) all the values in the list (you will need to import random at the top of the program)
Then using a for loop, loop it as many times as the value that was passed to the function:
Inside the for loop, remove one name from the list (use the pop() method)
Outside the for loop, but still inside the function, return the list of remaining people
Underneath where you left off in step 8, print the remaining people that are left: those that did not get voted off the island.

Answered by IntellectualGuy [earned 5 cycles]
View Answer
Profile icon
Profile icon

Here's your solution

from random import shuffle def eliminate(how_many): shuffle(names) for x in range(how_many): names.pop(-1) return names def compare(number,number2): if number == number2: print("\nBoth numbers are equal to each other") elif number < number2: print(f"\n{number} is less than {number2}") else: print(f"\n{number} is greater than {number2}") for x in range(3): num1 = int(input("\nEnter an integer: ")) num2 = int(input("\nEnter another integer: ")) compare(num1,num2) names = [] for x in range(6): name = str(input("\nEnter a name: ")) names.append(name) # Or if you wanted to be shorter and concise #names.append(input("\nEnter a name: ")) vote_off = int(input("\nHow many people would you like to vote of the island? Maximum is 6: ")) names = eliminate(vote_off) print("\nThe remaining people on the island are: ") for x in names: print(f"\n {x}")
Profile icon
Profile icon
Profile icon
Profile icon


use a for loop to run three times. while loops aren't good for that.

for i in range(3): print(i)

like that. You can put more code in the for loop to run more.
That should work

Profile icon

Seems like you already know how to do this pretty well, but for running the loop 3 times I would do it like this:

x = 3 while x > 0: # Your code x = x - 1