Python Forum
I have no clue what I am doing wrong here
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I have no clue what I am doing wrong here
#1
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
Yoriz write Jun-19-2022, 06:56 AM:
Please post all code, output and errors (In their entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
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.
Reply
#3
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.
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply
#4
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!!!
Reply
#5
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Am I wrong or is Udemy wrong? String Slicing! Mavoz 3 2,575 Nov-05-2022, 11:33 AM
Last Post: Mavoz
  Can I get a clue about testing please? Mustey 8 3,677 Apr-12-2021, 04:45 PM
Last Post: Larz60+
  I haven't a clue how to implement this logic 357mag 3 2,132 Apr-02-2020, 04:35 PM
Last Post: 357mag
  python gives wrong string length and wrong character thienson30 2 3,025 Oct-15-2019, 08:54 PM
Last Post: Gribouillis
  Getting syntax error, no clue why KaynRyu 4 3,138 Mar-26-2019, 02:43 PM
Last Post: KaynRyu
  I have no clue whats wrong... Jack_03 1 2,837 Sep-28-2017, 05:36 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020