+ 2

What's wrong with my code?

When you run the code it doesn't do the math. My code is in the comment section

28th May 2024, 5:51 PM
Clyde Jhan Paglinawan
Clyde Jhan Paglinawan - avatar
6 Answers
+ 4
Now cover all your code and look at the first 4 lines only. You have got a function there. But what is male and female? They are not meaningful names inside this function. This is called local scope. To make it work, male and female should be arguments: def breed(male, female): And in the last line where you invoke the function, you need to pass the actual values that were provided as input. print(breed(male, female)) There are also other mistakes, but it would be easier if you gave a link to your code, so people can fix it more easily.
28th May 2024, 6:37 PM
Tibor Santa
Tibor Santa - avatar
+ 2
You may share your code? without knowing what you tried we cannot help you
29th May 2024, 12:43 AM
john ds
john ds - avatar
+ 1
Can you share your code, so that we can help you better.
29th May 2024, 9:14 AM
Alhaaz
Alhaaz - avatar
+ 1
Clyde Jhan Paglinawan your function does not accept parameters. Using globals is not advisable. I have it working, but I think you might have a logic error. would 1 female and 2 males produce the same result as 2 females and 1 male? def breed(m, f): parents = m + f mate = parents * 2 return mate for_cats = print('Input the number of your cats: ') cats = int(input()) input_c = (f"(for_cats){cats}") if cats > 0: print("How many are males?: ") male = int(input()) print("How many are females?: ") female = int(input()) if (male >= 1) and (female >= 1): print(breed(male,female))
29th May 2024, 3:12 PM
Bob_Li
Bob_Li - avatar
0
def breed(): parents = male + female mate = parents * 2 return mate for_cats = print('Input the number of your cats: ') cats = int(input()) input_c = (f"(for_cats){cats}") if cats > 0: print(f"How many are males?: ") male = int(input()) print(f"How many are females?: ") female = int(input()) if (male > 1) and (female > 1): print(breed)
28th May 2024, 5:53 PM
Clyde Jhan Paglinawan
Clyde Jhan Paglinawan - avatar
0
Clyde Jhan Paglinawan Also, the total is not crosschecked.
29th May 2024, 4:26 PM
Bob_Li
Bob_Li - avatar