How do I apply a condition to change the values of a list of variables? - Python

Viewed 52

I'm trying to allow for the values of these pre-defined variables to change based on the input of a user.

I've recently come across the map() function, and I tried using that, though I'm not sure if I used that properly.

In this code, I attempted to decrease the percentages of a list of numbers, and then output those modified numbers, based on the user input.

a = 1
b = 2
c = 3 

list = [a, b, c]

i = 0

def percentage(n):
    while i < 7: 
        n= n*20/100 
i+=1

sample = input("Enter a string: ")

if sample == "Fox":
    map(percentage, list)


cat1 = f"The fox ran at a speed of {a}."
cat2 = f"The shark swam at a speed of {b}."
cat3 = f"The bird flew at a speed of {c}."

print(cat1, cat2, cat3)
print(list)

This is the output:

Enter a string: Fox 
The fox ran at a speed of 1. The shark swam at a speed of 2. The bird flew at a speed 
of 3. 
[1, 2, 3]

What I did didn't change the values. Have I done anything wrong? If not, how can I go about achieving what I want to achieve here?

4 Answers

A few mistakes, fixed below with comments to help you understand what's wrong.

a = 1
b = 2
c = 3 

# Do not use buildins as variable names. Renamed to mylist
mylist = [a, b, c]



def percentage(n):
    i = 0  # i shouldnt be a global variable, so move it here.
    while i < 7: 
        n= n*20/100 
        i+=1 # keep iterating in loop, or you will loop forever
    return n # function needs to return value, without it you would get [None,None,None]

sample = input("Enter a string: ")

if sample == "Fox":
    # Map does not change list "in place", it returns map.
    # You need to transform it to list and assign it to "mylist"
    mylist = list(map(percentage, mylist)) 


cat1 = f"The fox ran at a speed of {a}."
cat2 = f"The shark swam at a speed of {b}."
cat3 = f"The bird flew at a speed of {c}."

print(cat1, cat2, cat3)
print(mylist)

Output

Enter a string: Fox
The fox ran at a speed of 1. The shark swam at a speed of 2. The bird flew at a speed of 3.
[1.28e-05, 2.56e-05, 3.84e-05]
                             

You should rename your list to anything but list as it's a built-in type and it's bad practice to name your variables after built-in types (e.g. int, float, str, list, etc.)

E.g., rename your list speeds_lst instead and reference speeds_lst in your code.

the map function does not modify to input list instead it returns a map object to get the desired result you need to do this:

new_list=list(map(percentage, old_list))
  1. Fix percentage function, it does not return any value, so map is not using it, so make it look like this:
def percentage(n):
    i = 0
    while i < 7:
        n = n * 20 / 100
        i += 1
    return n
  1. map will return a result, you have to assign it to your, and as @Adam suggested, you ought to rename your list, so your condition will look like
if sample == "Fox":
    speeds_lst = list(map(percentage, speeds_lst))
  1. fix the printing to use speeds_lst[] instead of a, b or c
cat1 = f"The fox ran at a speed of {speeds_lst[0]}."
cat2 = f"The shark swam at a speed of {speeds_lst[1]}."
cat3 = f"The bird flew at a speed of {speeds_lst[2]}."

The final snippet

a = 1
b = 2
c = 3

speeds_lst = [a, b, c]

def percentage(n):
    i = 0
    while i < 7:
        n = n * 20 / 100
        i += 1
    return n

sample = input("Enter a string: ")

if sample == "Fox":
    speeds_lst = list(map(percentage, speeds_lst))

cat1 = f"The fox ran at a speed of {speeds_lst[0]}."
cat2 = f"The shark swam at a speed of {speeds_lst[1]}."
cat3 = f"The bird flew at a speed of {speeds_lst[2]}."

print(cat1, cat2, cat3)
print(speeds_lst)
Related