Python Forum

Full Version: I have no clue what I am doing wrong here
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Code Version 1:
message="Thank You for Choosing Robert's Fiberoptics! The One-Stop-Shop for all your fiberoptics needs!"
print(message)

amount_of_cable=input("Please enter the desired amount of fiberoptic cable needed for your project: ")
float(amount_of_cable)#I think I'm doing something wrong here
if amount_of_cable<100:
    def multiply(amount_of_cable,b):
        result=int(amount_of_cable)*0.87
    return result

    print(multiply(amount_of_cable,0.87))
When Ran in Console:
Error:
Thank You for Choosing Robert's Fiberoptics! The One-Stop-Shop for all your fiberoptics needs! Please enter the desired amount of fiberoptic cable needed for your project: 50 Traceback (most recent call last): File "main.py", line 6, in <module> if amount_of_cable<100: TypeError: '<' not supported between instances of 'str' and 'int'
Code Version 2 (Attempted Solution):
message="Thank You for Choosing Robert's Fiberoptics! The One-Stop-Shop for all your fiberoptics needs!"
print(message)

amount_of_cable=int(input("Please enter the desired amount of fiberoptic cable needed for your project: "))  #I think I'm doing 5. something wrong here
if amount_of_cable<100:
    def multiply(amount_of_cable,b):
        result=int(amount_of_cable)*0.87
        return result
    print(multiply(amount_of_cable,0.87))

print(amount_of_cable,"Is this correct? Please say 'Yes, or No'")
question=input("Please say Yes, or No: ") #Test this in Replit!!!
When Ran in Console:
Output:
Thank You for Choosing Robert's Fiberoptics! The One-Stop-Shop for all your fiberoptics needs! Please enter the desired amount of fiberoptic cable needed for your project: 50 50 Is this correct? Please say 'Yes, or No' Please say Yes, or No: Yes
No error message regarding the if statement, but it looks like it just skipped over it.
What am I doing wrong?


All I'm trying to do is get the number that is set to the amount_of_cable variable to be recognized as less than 100, and then multiply that same number by 0.87
Please format your code properly - put it within "[python]" tags, as that will add line numbering for you, preserve indentation and add syntax highlighting. As it is, it's really hard to read.
First off, as already posted: please use the code tags
  • You don't need to define message. You can simply print("Thank you...")
  • Move the custom function out of the if branch: it should the first thing in your code block

edit: in fact, as is, I can't see that you even need your custom function, but post back when you've made the changes and we'll take it from there.
Try it like this:
def multiply(amount_of_cable,b):
	result=int(amount_of_cable)*0.87
	return result

message="Thank You for Choosing Robert's Fiberoptics! The One-Stop-Shop for all your fiberoptics needs!"
print(message)

amount_of_cable=int(input("Please enter the desired amount of fiberoptic cable needed for your project: ")) #I think I'm doing 5. something wrong here

if amount_of_cable<100:
	print(multiply(amount_of_cable,0.87))

print(amount_of_cable,"Is this correct? Please say 'Yes, or No'")
question=input("Please say Yes, or No: ") #Test this in Replit!!!
While this is legal Python code:
amount_of_cable = 100

if amount_of_cable < 100:
    def multiply(amount_of_cable,b):
        result=int(amount_of_cable)*0.87
        return result
It is very unusual code, and it does not do what you think it does.

This defines a function:
def multiply(amount_of_cable):
    return amount_of_cable * 0.87
Now that the function is defined you can use it in your program. Like this:
if amount_of_cable < 100:
    price = multiply(amount_of_cable):
In your program the if statement controlled if the function was defined, not if it was used. If amount of cable >= 100 the function is not defined and it was not called. If the amount of cable < 100 the function was defined, but not used. Neither result are what you want.