#π Python tip calculator sos
133 messages Β· Page 1 of 1 (latest)
@ruby beacon
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.
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()
Wait, what's not working?
your functions are using duplicate input() statements instead of using the parameters passed into the function
Oh I see it, sorry for being here
going through the code line by line to understand what's happening would probably have lead to the same conclusion
not if someone is new to working with functions
it might be obvious for people with experience
Or if he's stupid like me
i see, so if i want to convert the value of dollars to a float, I should replace input() with "dollars" ? but the variable is from a different function so it wudnt work ?
def dollars_to_float(d):
you've defined your function with a "parameter"
that's what d is here
everyone is learning here, including me, please dont say that
so d is the value you want to be working with inside the function
ohhh! and since we have two input statments in def main, both the "percent" and "dollars" will be "d"?
can I try?
dollars = dollars_to_float(input("How much was the meal? "))
percent = percent_to_float(input("What percentage would you like to tip? "))
No, I meant, sorry if I hurt you
you're taking input, and immediately passing it into the function
the first input gets passed into dollars_to_float
I meant like that's a posobility that I'm really stupid
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)
ohhh and so i can say def percent_to_float(p)?
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
@ruby beacon I think you have a fundamental misunderstanding of how parameters or variables work
let's clear that up
No, p and d are parameters, which means when the function is called, the parameters become the value of what was passed in
ohhh that makes it clearer, thank you
!e
def greeting(name):
print("Hello", name)
greeting("peacock")
greeting("fashoomp")
:white_check_mark: Your 3.12 eval job has completed with return code 0.
001 | Hello peacock
002 | Hello fashoomp
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
oooh okie, so in this case the parameter is called "name", and it is equal to whatever value we pass in, as fashoomp put it?
yes
so instead of a second input i shud put d and p? in each function
yes exactly
functions have their own enclosed name spaces, in which the variable name is assigned whatever object is passed as the first argument
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?
sure! btw ill try it out and keep this channe open for now to see how it goes
this is the prompt im working with:
can you explain how your dollars_to_float() works, step by step?
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)
so according to my understanding, it takes the input, removes the dollar sign (i used [1:] for simplicity), and then returns the value (which was orignially a string/text) as a number (float)
to be the then multiplied to get the amount we shud tip
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()
@ruby beacon the first thing you do is try to convert the argument to a float
yes exactly, you're trying to convert, and then remove the dollar sign
you must remove it first
but how is that going to work when it still has the $ sign in front?
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
the assignment does assume that it has the $ in front
instead, you can use lstrip('$') which will be more reliable
so that's not necessary
Yeah, but it's generally good to think of edge cases
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)
still not quite
think about what each variable represents
I gotta clarify something, are we good peacock?
line 3 , 9 and 18
We've all moved on, please don't bring this up
where is the original variable defined?
def dollars_to_float(d):
modified = original[1:]
remember that d is going to be the dollar string
Okay, thank you
like "$50.00"
i see, gimme a sec, ill try to apply the things u guys taught me and get back
when in doubt, going through the code line-by-line and trying to understand what each line does is tremendously helpful, even for experts
it means you wrote something structurally that python doesn't understand
share your code
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
where is this cd tip?
show the full code
yea i cant find it either
okie
well how did you get that error then?
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()
it looks like you tried to enter a shell command in the Python interpreter
is this relevant?
i did these steps when i started the project a few days ago, my terminal doesnt say tip/$ anymore btw
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
shell is the top part right? and terminal the bottom?
I don't know where you're writing your code
m not i promise, i sent all the code there is
we can't see your screen
if you see >>> at the start of where you're typing, that's a python shell
you've entered python shell mode
bw, u see the numbers on top? if someone else has access to them, does that mean they can se emy filed?
type quit() and press enter
ok it worked
two questions: how does one enter themode
what does that mode entail/mean?
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
did you launch python from the terminal?
idk what that means (really appreciate ur guys's patience with me) but i click the horizontal triange / play button on the top right region of the ss i sent
:) ?
then perhaps your terminal remains in python mode after running a script
GUYS IT WORKS it ALL WORKS THANK YOU SO MUCH
if you ever see the >>>, you'll have to type quit() to get out of that mode
will keep that in mind, tysm guys! u shud really get paid for this lol
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
god thats smart
although I see how they can be helpful as a beginner when writing this out
also
so im currently learning python from thsi edx course
yeah I recognize this as cs50
do u guys have any other recommendations? clearly the concept of parameters and functions didnt register well thru this course
there's tons of resources online for just about every topic. Don't limit yourself to just one resource
.rp defining functions
Here are the top 5 results:
this 2nd link here is a good one
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.