← Back to Community

# PassingValues

Profile icon
DrillingSphere

#1
names = []

value1 = 0
value2= 0
def compare() :
if value1 > value2 :
print("Value1 is greater than Value2.")
elif value2 > value1 :
print("Value2 is greater than Value1.")
elif value1 == value2 :
print("Value 1 and Value 2 are equal.")
#2
#Add the beginning comments at the top of the program.
#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
index = 0
while index <= 3 :
value1 = int(input("\nPick a number."))
value2 = int(input("Pick another number."))
compare()
index = 1 + int(index)
#3
#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
#4
#That is it for the first part of the program.

#All things above were for the first program with the compare function.

#All things below are for the name list with the eliminate function.

#5
#Next, create an empty list called names.

#6
#Create a loop that runs 6 times:
import random
def eliminate():
while len(names) > 1:
print("\nThe original list : " + str(names))
names.pop(random.randrange(len(names)))
print("List after removal of random number : " + str(names))
print("/nThe last remaining member is" + str(names))
#Inside of the for loop, prompt the user for a name
#Append the name to the list
index = 0
names = []
while index < 6 :
index = 1 + int(index)
eliminate()

#7
#Outside of the for loop, prompt the user for how many people they would like to vote off the island.
#8
#Call the function eliminate and pass the variable you used from step 7 to it.
#9
#Also, this function will return a value, so store this back function call back to a new variable.
#10
#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
#11
#Underneath where you left off in step 8, print the remaining people that are left: those that did not get voted off the island

Voters
Profile icon
SanthoshNalla
Profile icon
DrillingSphere