#πŸ”’ Python tip calculator sos

133 messages Β· Page 1 of 1 (latest)

ruby beacon
#

hey guys, im trying to write a code thatclaculates how much should I tip in a restaurant, but my code isnt working, and I also dont know whats wrong. i would appreciate it if you guys could just tell me which part is wrong so that I can fix it as im still learning and want to figure it out myself :)

iron crescentBOT
#

@ruby beacon

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

ruby beacon
#
def main():
    dollars = dollars_to_float(input("How much was the meal? "))
    percent = percent_to_float(input("What percentage would you like to tip? "))
    tip = dollars * percent
    print(f"Leave ${tip:.2f}")

def dollars_to_float(d):
    original = float(input())
    modified = original[1:]
    return modified

def percent_to_float(p):
    original = float(input())
    modified = original.replace("%"," ")
    return (modified/100)

main()
rancid cosmos
#

Wait, what's not working?

hearty apex
rancid cosmos
#

Oh I see it, sorry for being here

lusty wedge
#

going through the code line by line to understand what's happening would probably have lead to the same conclusion

hearty apex
#

it might be obvious for people with experience

rancid cosmos
#

Or if he's stupid like me

ruby beacon
hearty apex
#

def dollars_to_float(d):

#

you've defined your function with a "parameter"

#

that's what d is here

ruby beacon
hearty apex
#

so d is the value you want to be working with inside the function

ruby beacon
hearty apex
#

not quite

#

you have two different functions

lusty wedge
#

can I try?

hearty apex
#
dollars = dollars_to_float(input("How much was the meal? "))
percent = percent_to_float(input("What percentage would you like to tip? "))
rancid cosmos
hearty apex
#

you're taking input, and immediately passing it into the function

#

the first input gets passed into dollars_to_float

rancid cosmos
#

I meant like that's a posobility that I'm really stupid

hearty apex
#

and the 2nd input gets passed to percent_to_float

#
def dollars_to_float(d):
    original = float(input())
    modified = original[1:]
    return modified

def percent_to_float(p):
    original = float(input())
    modified = original.replace("%"," ")
    return (modified/100)
ruby beacon
hearty apex
#

if you look at your 2 functions, you have d as the parameter of your first one, and p as the parameter of the 2nd one

lusty wedge
#

@ruby beacon I think you have a fundamental misunderstanding of how parameters or variables work

#

let's clear that up

hearty apex
ruby beacon
hearty apex
#

!e

def greeting(name):
    print("Hello", name)


greeting("peacock")
greeting("fashoomp")
iron crescentBOT
hearty apex
#

see how name is "peacock" on the first call, and then "fashoomp" on the 2nd?

#

we're using the parameter to represent whatever gets passed in

ruby beacon
hearty apex
#

yes

ruby beacon
hearty apex
#

yes exactly

lusty wedge
#

functions have their own enclosed name spaces, in which the variable name is assigned whatever object is passed as the first argument

hearty apex
#

you're already taking input from elsewhere and passing it in, there's no need to ask for input again within the function

#
def dollars_to_float(d):
    original = float(input())
    modified = original[1:]
    return modified
#

also can you explain what you're trying to do with this function?

ruby beacon
lusty wedge
#

can you explain how your dollars_to_float() works, step by step?

hearty apex
#

try rewriting it first with the parameter instead of input

#

since it might ultimately change things

#

(it's not so simple as just replacing input() with the parameter)

ruby beacon
#

anyways i got an error that said i cant convert float to string

#

def main():
    dollars = dollars_to_float(input("How much was the meal? "))
    percent = percent_to_float(input("What percentage would you like to tip? "))
    tip = dollars * percent
    print(f"Leave ${tip:.2f}")

def dollars_to_float(d):
    original = float(d)
    modified = original[1:]
    return modified

def percent_to_float(p):
    original = float(p)
    modified = original.replace("%"," ")
    return (modified/100)

main()
lusty wedge
#

@ruby beacon the first thing you do is try to convert the argument to a float

hearty apex
#

yes exactly, you're trying to convert, and then remove the dollar sign

#

you must remove it first

lusty wedge
#

but how is that going to work when it still has the $ sign in front?

hearty apex
#

also while [1:] does work to remove the dollar sign, it does leave you with an edge case

#

which is the potential for a number being provided that doesn't have $ in front

lusty wedge
#

the assignment does assume that it has the $ in front

hearty apex
#

instead, you can use lstrip('$') which will be more reliable

lusty wedge
#

so that's not necessary

hearty apex
ruby beacon
#

oooh that makes sense, cuz it wud get confused because of the signs

#

okay this is what i did

#
def dollars_to_float(d):
    modified = original[1:]
    original = float(d)
    return modified

def percent_to_float(p):
    modified = original.replace("%"," ")
    original = float(p)
    return (modified/100)
hearty apex
#

think about what each variable represents

rancid cosmos
#

I gotta clarify something, are we good peacock?

ruby beacon
#

line 3 , 9 and 18

hearty apex
lusty wedge
#

where is the original variable defined?

hearty apex
#
def dollars_to_float(d):
    modified = original[1:]
#

remember that d is going to be the dollar string

rancid cosmos
hearty apex
#

like "$50.00"

ruby beacon
#

i see, gimme a sec, ill try to apply the things u guys taught me and get back

lusty wedge
#

when in doubt, going through the code line-by-line and trying to understand what each line does is tremendously helpful, even for experts

ruby beacon
#

guys what does this mean

hearty apex
#

it means you wrote something structurally that python doesn't understand

#

share your code

ruby beacon
#
def dollars_to_float(d):
    modified = d[1:]
    x= float(modified)
    return x

def percent_to_float(p):
    modified= p.replace("%"," ")
    y= float (modified)
    return y/100

#

my code now btw

lusty wedge
#

where is this cd tip?

hearty apex
#

show the full code

ruby beacon
ruby beacon
lusty wedge
#

well how did you get that error then?

ruby beacon
#

def main():
    dollars = dollars_to_float(input("How much was the meal? "))
    percent = percent_to_float(input("What percentage would you like to tip? "))
    tip = dollars * percent
    print(f"Leave ${tip:.2f}")

def dollars_to_float(d):
    modified = d[1:]
    x= float(modified)
    return x

def percent_to_float(p):
    modified= p.replace("%"," ")
    y= float (modified)
    return y/100

main()
lusty wedge
#

it looks like you tried to enter a shell command in the Python interpreter

ruby beacon
#

is this relevant?

#

i did these steps when i started the project a few days ago, my terminal doesnt say tip/$ anymore btw

hearty apex
#

well if you're trying to type cd tip into a python shell, that's not going to work

#

cd is a terminal command

#

not python

ruby beacon
hearty apex
#

I don't know where you're writing your code

ruby beacon
lusty wedge
#

we can't see your screen

hearty apex
#

if you see >>> at the start of where you're typing, that's a python shell

hearty apex
#

you've entered python shell mode

ruby beacon
#

bw, u see the numbers on top? if someone else has access to them, does that mean they can se emy filed?

hearty apex
#

type quit() and press enter

ruby beacon
hearty apex
#

python shell is an interactive python session

#

where you can write a line of python code and get a result immediately

#

I don't use VSC so I'm not sure how you might have entered that mode

lusty wedge
#

did you launch python from the terminal?

ruby beacon
hearty apex
#

then perhaps your terminal remains in python mode after running a script

ruby beacon
#

GUYS IT WORKS it ALL WORKS THANK YOU SO MUCH

hearty apex
#

if you ever see the >>>, you'll have to type quit() to get out of that mode

ruby beacon
hearty apex
#

Also just to show how you can simplify functions

#
def dollars_to_float(d):
    return float(d[1:])
#

you don't need all of those other variables

ruby beacon
hearty apex
#

although I see how they can be helpful as a beginner when writing this out

ruby beacon
#

also
so im currently learning python from thsi edx course

hearty apex
#

yeah I recognize this as cs50

ruby beacon
#

do u guys have any other recommendations? clearly the concept of parameters and functions didnt register well thru this course

hearty apex
#

.rp defining functions

quartz oxideBOT
#

Here are the top 5 results:

Using Python Optional Arguments When Defining Functions
MATLAB vs Python: Why and How to Make the Switch
What Are Python Asterisk and Slash Special Parameters For?
Python's reduce(): From Functional to Pythonic Style
hearty apex
#

this 2nd link here is a good one

ruby beacon
#

really appreciate this community

#

!close

iron crescentBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.