Skip to content
← Back to Community
Functions in python
Profile icon
mhs_mhs123

Hey everyone, I'm a new python dev and i'm trying to familiarize myself with functions. for my first functions project, I tried making a miles to km converter, but I'm running into an error. can someone tell me what I did wrong and how to fix it. Thanks.

https://repl.it/@mhs_mhs123/milestokmconverter

Voters
Profile icon
ObasiWinn
Profile icon
pergarhribljanf
Profile icon
ColeFrebrowski
Profile icon
NhuNguyen8
Profile icon
AnunyaRuengsrin
Profile icon
mhs_mhs123
Comments
hotnewtop
Profile icon
hyperupcall

Hi!
It looks like you forgot to call the function. Underneath def convert(miles) (with no indentation), you might want to call your convert function. Kind of like this

convert(20)
Profile icon
HenryShearer

I agree (mark @eankeen as the right answer). You've defined what you want the function to do, but you haven't actually told the computer to carry out that function. It's like getting a recipe but not using it.

This works for me:
def convert(miles):
print('in km:')
km = miles*1.609
print(km)

convert(20)

Profile icon
mhs_mhs123

Thanks for your input as well @HenryShearer

Profile icon
mhs_mhs123

@HenryShearer I see that you code in python as well

Do you want to collaborate on a choose your own adventure game with me?

Profile icon
HenryShearer

@mhs_mhs123 I have one I'm working on right now, so I'd prefer not to start a new collab, but I'm down for helping each other! =D

Profile icon
mhs_mhs123

@HenryShearer ok cool, let's help each other. This was one of my first projects ever in programming, so it may seem a bit simple, but there are a few bugs in it which are quite obvious. I'll link it here:

https://repl.it/@mhs_mhs123/textbasedgametrial2

Profile icon
HenryShearer

@mhs_mhs123 Ok so in the waterfall portion, change the "and" to an "or", because you can only change one of them each game, and when it checks for an undefined variable it breaks,

Make this:

if pick_waterfall and pick_waterfall_2 == 'yes':
print('you step into the waterfall. You notice a shiny purple stone at your feet.')
pick_stone = input('Do you want to pick up the stone?')

This:

if pick_waterfall or pick_waterfall_2 == 'yes':
print('you step into the waterfall. You notice a shiny purple stone at your feet.')
pick_stone = input('Do you want to pick up the stone?')

Profile icon
mhs_mhs123

@HenryShearer I'm actually restarting the whole game, but I have a copy of the one that you're referring to in sublime text. I'll let you know when i finish the one I'm working on right now. Thanks for the advice. Link me to your game as well.

Profile icon
HenryShearer

@mhs_mhs123 Okay!

Profile icon
HenryShearer

@mhs_mhs123 I don't actually have much function right now, because I'm building the background functions, but here it is: https://repl.it/@HenryShearer/Adventure-Game

Profile icon
HenryShearer

@mhs_mhs123 Try commands such as "look at key" or "help". Those are the ones I have semi-functioning right now.

Profile icon
mhs_mhs123

@HenryShearer I'm getting an error on your project rn

Profile icon
mhs_mhs123

@HenryShearer I'm getting similar bugs in this version of the game and the previous version of my game, you're much better than me in python, so can you help me fix this?

https://repl.it/@mhs_mhs123/textbasedgametrial2

Profile icon
HenryShearer

@mhs_mhs123 OK, it seems like that "and" operator is messing you up again. Try changing the go_waterfall and go_tunnel event into two separate events. What's happening is you are trying to call a variable that has no definition yet.
Another thing you could try is defining all the variables as "no" at the beginning of the program, so that they default to that if they haven't been changed.

Profile icon
Kuan-WunWun

add
convert(int(input("enter miles")))

Profile icon
pattmax

Change the print to a return and then you can do

print(convert(5))
Profile icon
Masy

Yeah @eankeen is correct you forgot to call the function at the end