Exercise 19 - Study Drills

The third study drill states you should write a function and run it 10 different ways.

  1. Give the function numbers directly: my_function(20, 30)

  2. Use variables in the script: my_function(amount1, amount2)

  3. By doing math: my_function(10 + 20, 5 + 6)

  4. Combining variables and math: my_function(amount1 + 5, amount2 + 10)

Has anyone else come up with additional ways?

Thanks.

Stop cheating nail! Just kidding. Have you tried using functions to calculate these values? You know you can put a function and where you have those values.

I have wrote my own function and try different way to run it. Here is the list that I came up with:

def marbles(red, blue):
print(f"I have {red} red marbles and {blue} blue marbles.\n") #the line is indented, I don’t know why the website doesn’t show it

marbles(6,10)

yellow = 17
green = 18

marbles(yellow, green)

marbles(8 - 2, 10 * 2)

marbles(yellow - 8, green + 2)

purple = 4
orange = 7

marbles(f"{purple}",f"{orange}")

marbles(input(),input())

black = green - purple
white = orange + yellow

marbles(black, white)

rainbow = “{}”

marbles(rainbow.format(12), rainbow.format(6))

indigo = f"{yellow}"
cyan = f"{green}"

marbles(indigo, cyan)

marbles(orange + green, yellow - purple)

marbles(int(indigo + 4), int(cyan - 2))

marbles(int(f"{orange}" + f"{green}"), int(f"{yellow}" - f"{purple}"))

for the second last line, I got the error “TypeError: must be str, not int”. I try to put int() in it because I want number, but the code just doesn’t seem to let me.
for the last line, python seem to recognize everything as string because I got “TypeError: unsupported operand type(s) for -: ‘str’ and ‘str’”. I thought that f"{} should have return the value of the variables and not the name of the variables themselves.

If someone can show me how to fix the last two lines it would help me a lot. Thank you.

No f"{yellow}" gives you a string representation of the value of yellow, as if you did str(yellow). So indigo is the string "17", not the int 17, and you can’t add ints and strings. Same in the last line: you can add (concatenate) two strings, but you can’t subtract one string from another.

If you want to use the value of a variable, you just type its name. Where did you get that idea with the f-string?