#converting currency

71 messages · Page 1 of 1 (latest)

plucky stream
#

need help converting currency

clever cave
#

It's a bit hard to read your code from that small screenshot

#

Do you mind copying and pasting it with a code block?

#

!format

short summitBOT
#
Code Formatting

When sharing code with the community, please use the correct formatting for ease of readability.

Example

```py
YOUR CODE HERE
```

Those are back ticks not single quotes, typically the key above TAB

clever cave
#

Also, what seems to be the issue?

#

You need to explain what you need help with, not just state that you need help

plucky stream
#
def usdcny(usd):
    rate = 6.75   
    return "{:.2f} Chinese Yuan".format(usd*rate)
#

also I'm still confused on how to use def function

short summitBOT
# clever cave !exec

@clever cave

✅ Exec - Success (Super User 🦸)
*No output or exceptions*
clever cave
#

There doesn't seem to be any error?

clever cave
#

And is that all the code you have?

inner plover
#

Code seems to work although I wouldn't store currency values as float (also rounding during conversions might be a bit tricky to get right)

plucky stream
plucky stream
clever cave
#

I'm not sure where you're getting it

plucky stream
#

and not float ?

#

@clever cave

clever cave
#

Why'd you ping me?

#

!exec ```py
def usdcny(usd):
rate = 6.75
return "{:.2f} Chinese Yuan".format(usd*rate)

print(usdcny(1))

short summitBOT
clever cave
#

Looks like the function works

#

Where is the error you're getting

#

You said that the code says an error but you never elaborated

#

Why can't you run it?

plucky stream
plucky stream
#

why it's 1

clever cave
#

Because I tested it with 1 usd

plucky stream
#

also, how do I use def function... I still couldn't catch with it

lavish ember
#

def is basically a keyword used for defining functions.

In bookish terms, function is a piece of code that can be re-used for different values.

A function may or may not contain parameters. There were essentially three types of parameters. Positional (determined by the position they are defined in), Keyword only, default position or keyword only parameters
after python 3.8, a new type of parameter was introduced, positional only parameters

Any default parameter falls after non-default parameters

what are default parameters you may ask, any parameter you don't assign value to while calling a function will have a default value passed onto it. These values are created when you run the program.

Let's look at an example function for understanding parameters and their types

1. def function():
2.    print("This is a function")
3.
4. function()

the numbers on the left are for reference for the line number

python executes line by line
when python sees the def keyword in our first line it recognizes a user defined function, it directly jumps onto the line that has the next line of code, it does not go inside the function. i.e it goes directly to line 4 and sees function()
the parentheses in front of function is known as calling a function. Calling a function essentially makes python go back to line 1, then go inside the function. It then reads the print("This is a function") line and then executes it for you to see This is a function on the console.

Since python is a dynamically typed language, you don't need to mention the type of data a function will return. By default, python function returns None which is null equivalent for most languages

#

why does it matter to know that python reads code line by line you might ask because
if you make an error in a function that's not a syntax error, python would be fine with it until you call the function

here is an example showcasing this

def function():
    print(x)

print("Hello World")
function()

you would first get Hello World on console then a NameError saying x is not defined

if you tried this in a compiled language, the compiler will point this out before running the program

#

Let's talk about parameters, again, as python is dynamically typed, parameters need not have a fix data type which provides a lot of flexibility.

But this comes at the cost of time for execution.
It's very logical
which would be easier if I gave you something

  1. I tell you what symbols in it are going to be and are fixed and only the value will change not the type
  2. I don't tell you what symbols in it are going to be and you will have to figure out as you go + no fixed type or value

anyway, let's see a function with parameter inside it. Parameters are variables that can only be used inside its function

def function(parameter):
    print(parameter)

function("Hello World")
function(1)
function(["hey", "this", "is", "a", "list"])
#

In this example
you can see, parameter is first "Hello World" which is a string
then 1 which is an integer
then ["hey", "this", "is", "a", "list"] which is a list

and it prints accordingly

you can have multiple parameters

this parameter in our example is an example of a local variable

#

In python, there are mainly 4 types of variable scopes, and python looks for them in LEGB order

L - Local
E - Enclosed
G - Global
B - Built-in
#

Built-in examples include: id, int, list, hash
Global variables are any variables that are accessible by any part of your code.
Enclosed variables or Non-local variables are the variables that are neither local nor global
Local variables are variables that are only accessible only by a part of a code

#

it's a big topic to write about, let me know if you have any specific doubts about functions or anything related to python. I would be glad to explain!

plucky stream
#

also I have a question regarding when to use true and false boolean

lavish ember
#

That would depend on what you want to achieve.

Think about this scenario
I have a loop for a game, this loop will end when the game is over
how can you achieve this

the very natural solution would be to use a boolean value

game_is_over = False
while not game_is_over:
    ... # rest of the code
#

and in your loop, when something happens which ends the game, you don't have to break manually, you can just set game_is_over to True

#

another scenario is where your function checks for something and returns a boolean value which your program uses to handle its if else statement

def is_prime(n):
    for i in range(2, n):
        if n % i == 0:
          return False
    return True

till = 100
for x in range(2, till):
    if is_prime(x):
        print(x, "is a prime")
#

there could be other scenarios, but ultimately it depends on what you want to do

plucky stream
#

thank you so much !

#

@lavish ember

lavish ember
#

You're welcome

plucky stream
#

@clever cave what do I need to input 😭

clever cave
#

Idk lol

#

Aren't you the one who wrote the code to ask for input?

#

What did you want the user to input?

plucky stream
#

OH

plucky stream
clever cave
#

lol

clever cave
#

To prevent these kinda questions haha

plucky stream
#

I don't know where I did wrong

pastel quarry
#

;-;

plucky stream
#

I wouldn't ask such questions if I rlly know where I did wrong

#

😭😭😭

clever cave
#

input is a function to takes in an input from the user

#

That's why you got that lil popup

pastel quarry
#

This guy is right