# Help with if/elif and / or repeating messages

Hey so I’ve just started python like 2 hours ago and want to make a little thing that tells me what size a carp is in a simple word. “Monster, Small, Medium, Average” etc. Im having trouble with the elif/if statements. I will give an example here. If I set the weight value to anything above 23 it will put “Good job! Anixtzer You caught a average size carp weighing 24 lbs!
Good job! Anixtzer You caught a medium carp weighing 24 lbs!”

in terminal instead of just “Good job! Anixtzer You caught a medium carp weighing 24 lbs!”
Since i made the weight if it is greater or equal to 24 to send “Good job! Anixtzer You caught a medium carp weighing 24 lbs!” But it sends both that one and the previous one which is saying i have a carp average sized carp weighing 24lbs which i have the weight for the average carp message set to >= 6

``````
def carp_weight_calculator(name, weight):
if weight <= 5:
print("You caught a small carp!")
if weight >= 6:
print("Good job!", name, "You caught a average size carp weighing", weight, "lbs" "!")

if weight >= 24:
print("Good job!" , name, "You caught a medium  carp weighing", weight, "lbs" "!")

if weight >= 50:
print("Good job!", name, "you caught a monster carp weighing", weight,"lbs" "!")

carp_weight_calculator("Anixtzer",24)

``````

^ This is my code for the entire thing.
The line of code at the bottom is name and weight.

Weight in this screenshot is set to 5
( Wont include this screenshot since this is correct and it wont let me post any more links because im a new user, But you get the gist, This line of code is correct and gives me the correct message “You caught a small carp!” if weight <= 5:
print(“You caught a small carp!”)
( Correct.)

Weight in this screenshot is set to 6 ( i want this message to only post in terminal if its in between 6-23 )
(https://imgur.com/a/KR6Pzl6)

weight in this screenshot is set to 24 ( i want this message to only show if weight is set to 24-49 so that if i set the weight to 50 it doesn’t show this but only shows the message associated with weight 50 or above )
https://imgur.com/a/DO1hkmJ

weight in this screenshot is set to 50

( again i dont want the 2 previous messaging saying i got a medium and average sized carp,
i just want it to say i got a monster. )

Sorry for the links and photos but wouldn’t let me post 3 photos or 3 links so i did 2:1. ( ALL THESE SCREENSHOTS ARE FROM THE PYTHON BUILT IN TERMINAL BUT I DIDNT INCLUDE FULL TERMINAL BECAUSE OF MY NAME INCLUDED IN THEM. )

If `weight` is 24, `weight >= 6` is true, so it prints the message, but, later, `weight >= 24` is also true, so it prints that message too.

If `weight` is 50, `weight >= 50` will be true, so it’ll print that message too.

And if `weight` is, say, 5.5, it won’t print any message because none of the conditions are true.

With `if``elif``elif`, if a condition is true, it’ll skip all of the later tests. You can add a final `else` for when none of the preceding conditions were true.

Try this:

``````if weight <= 5:
print("You caught a small carp!")
elif weight < 24:
print("Good job!", name, "You caught a average size carp weighing", weight, "lbs!")
elif weight < 50:
print("Good job!", name, "You caught a medium  carp weighing", weight, "lbs!")
else:
print("Good job!", name, "you caught a monster carp weighing", weight, "lbs!")
``````

It’s still better to copy/paste text - we can copy/paste it ourselves, and it’s better for the visually impaired.

You can also hand scrub your name from the transcript, or temporarily change your prompt (if that’s where the name appears). On UNIXlike systems you can change your prompt by setting `\$PS1`, eg:

``````PS1='\$ '
``````

The limits on images and links apply to new users - they get relaxed over time and with activity.

I appreciate all the help! Thank you Matthew, I tweaked the code to add another saying if the weight goes over a certain amount and it works great! And thanks to Cameron for letting me know i can scrub the personal name and stuff out

@MRAB Would you be able to help me with 1 more thing?

If so I would love for you to help me with this.
So i want to set weight by the code asking a question in terminal ex: “What is your carp weight?”
i also want to name by having the code as me in terminal what my name is. Ex “What is your name?”

I know the code for this ( I think?? ) I just dont know where to put it / how to use it. To ask the question about weight / name it’s `name = input(“what is your name?”

Same for weight.
weight = input(“How much does your fish weigh?”) `

I can get the questions to ask good but when i give an answer to both the questions it just doesn’t give me a response, Just blank Here is what it says in the terminal

``````C:\Users\>C:/Users//AppData/Local/Programs/Python/Python312/python.exe "c:/Users//Desktop/def carp_weight_calculator(name, weight).py"
what is ur name: Anixtzer
how much does ur fish weigh: 30

C:\Users\>
``````

( i deleted my personal name out of the transcript, Also it’s not meant to look like code but i formatted it like that so its more visible. )

As you can see after i give it an input for " How much does your fish weigh " it just doesn’t do anything else it’s supposed to say “Congrats, Anixtzer You caught a normal size carp weighing 30lb!”

So on so fourth, I probably just didn’t arrange it properly but i don’t know. Like i said i started coding python yesterday so it’s going to take some time to fully understand. Here is the code for the whole thing. `name = input("what is ur name: ")
weight = input ("how much does ur fish weigh: ")

def carp_weight_calculator(name, weight):
if weight <= 5:
print(“You caught a small carp!”)
elif weight < 24:
print(“Good job!”, name, “You caught an Average size carp weighing”, weight, “lbs” “!”)

``````elif weight < 35:
print("Congrats", name, "You caught a Normal size carp weighing", weight, "lbs!")

elif weight < 50:
print("Good job!" , name, "You caught a Medium  carp weighing", weight, "lbs" "!")

elif weight < 70:
print("Wow!!", name, "You caught a Large carp weighing", weight, "lbs" "!")

elif weight < 100:
print("Good job!", name, "you caught a Monster carp weighing", weight,"lbs" "!")
else:
print("Liar!!")

carp_weight_calculator(name,weight)`
``````

In order to preserve formatting, please select any code or traceback that you post and then click the `</>` button.

On Windows, it’s recommended that you use the Python Launcher `py`:

``````py "c:/Users/Desktop/def carp_weight_calculator(name, weight).py"
``````

“def carp_weight_calculator(name, weight).py” is a strange filename, by the way. I’d recommend something simpler like “carp_weight_calculator.py”.

Check the indentation in your code. Is the call to `carp_weight_calculator` indented more than the `def` of the function? If it is, then it’s part of the function.

Another point to make is that `input` returns a string, so you won’t be able to ask whether it’s less than a number. Strings are strings, and numbers are numbers.

You can convert a string representation of an integer (whole number, no fractional part) to an integer with `int`. If it does/could have a fractional part, then you can convert it to a floating-point number with `float`.

Oh, Sorry I didn’t realize that the full code didn’t post, Here is the full code, If you will go over and see what i’ve done wrong I would greatly appreciate that. ( ive edited it since to try to fix it and now after i finish all the questions it says the like associated with " elif weight < 24: " instead of just being blank, I think it does this because i set weight = 23, again i want the answer to the question(s) to set the name and weight.
I’ve fixed the file name as well.

``````name = input("what is ur name: ")
weight = input ("how much does ur fish weigh: ")

def carp_weight_calculator(name, weight):
if weight <= 5:
print("You caught a small carp!")
elif weight < 24:
print("Good job!", name, "You caught an Average size carp weighing", weight, "lbs" "!")

elif weight < 35:
print("Congrats", name, "You caught a Normal size carp weighing", weight, "lbs!")
elif weight < 50:
print("Good job!" , name, "You caught a Medium  carp weighing", weight, "lbs" "!")

elif weight < 70:
print("Wow!!", name, "You caught a Large carp weighing", weight, "lbs" "!")

elif weight < 100:
print("Good job!", name, "you caught a Monster carp weighing", weight,"lbs" "!")
else:
print("Liar!!")
carp_weight_calculator("Anix",23)
``````

Hopefully this is an easy fix, If not that’s fine you don’t have to try to fix this for me, I will learn as I progress. I see you said something about int/float i just don’t how to to incorporate that into my code correctly, I may be able to find a Youtube video on this.

Just pass `name` and `int(weight)` when you call `carp_weight_calculator`.

Thank you so much all i had to do was add int to `carp_weight_calculator("Anix, 23)`

Revised: `carp_weight_calculator("Anix",int(weight))`