Jump to content

I am a bit stuck in python

CookiezFort

I downloaded the wrong version of python from what codecademy uses (i use 3.1 on my pc)

 

I tried to make a simple calculator and did division first, this is my code atm

 

action = input("division, multiplication, addition or subtraction? ")
n1 = input("what is your first number? ")
n2 = input (" what is your second number? ")

def division():
    if action == "division":
        return n1 / n2

print (division)

 

however when i run the code, i input the numbers then this happens

 

division, multiplication, addition or subtraction? division
what is your first number? 6
 what is your second number? 3
<function division at 0x03466FA8>
        

        
 

My PC:

Spoiler

MOBO: MSI B450 Tomahawk Max, CPU: AMD Ryzen 5 3600, Cooler: BeQuiet! Dark Rock 3, GPU: Gigabyte GTX 1050ti D5 4G, Ram: 16GB (2x8) HyperX Fury DDR4, Case: NZXT S340, Psu: Be Quiet! Pure Power 11 600W , HDD's: WD 1TB Caviar Blue, WD 256GB Scorpio Blue, WD 2TB Caviar Blue  SSD: Sandisk SSD PLUS 240Gb

Link to comment
Share on other sites

Link to post
Share on other sites

Your division function won't be able to access the variables defined outside it. What you need to do is check if the input == division inside your main function back where the inputs are, and if it is then call the division function and pass the relevant variables to it. Otherwise everything under "def division()" won't see the variables n1, n2, or action. You also need the "float" in front of your input to make sure the input is taken as a number, otherwise your input of 6 will be seen as the character "6" instead of a number with a value of 6.
 

def main():
    action = input("Division, multiplication, addition, or subtraction? ")
    n1 = float(input("What is your first number? "))
    n2 = float(input("What is your second number? "))
    if action == "division":
        print(division(n1, n2))
 
def division(num1, num2):
    return num1/num2
 
main()
 

Output:

Division, multiplication, addition, or subtraction? division
What is your first number? 6
What is your second number? 3
2.0

Link to comment
Share on other sites

Link to post
Share on other sites

So, i should put everything in one function with sub functions so they can access each other, and make numbers floats

 

Thanks

My PC:

Spoiler

MOBO: MSI B450 Tomahawk Max, CPU: AMD Ryzen 5 3600, Cooler: BeQuiet! Dark Rock 3, GPU: Gigabyte GTX 1050ti D5 4G, Ram: 16GB (2x8) HyperX Fury DDR4, Case: NZXT S340, Psu: Be Quiet! Pure Power 11 600W , HDD's: WD 1TB Caviar Blue, WD 256GB Scorpio Blue, WD 2TB Caviar Blue  SSD: Sandisk SSD PLUS 240Gb

Link to comment
Share on other sites

Link to post
Share on other sites

So, i should put everything in one function with sub functions so they can access each other, and make numbers floats

 

Thanks

No, you need to pass parameter arguments to your function(s) so that they can get the variables that they need. It is very important in program design that you carefully control access to resources, and that includes not using global variables unless absolutely necessary. Why? Because it is very easy to introduce bugs and side effects if a variable can be modified from anywhere in your program without warning.

Link to comment
Share on other sites

Link to post
Share on other sites

While most of what they said is true, the reason you were getting that as output was because in the line

print(division) 

division is a pointer to a function, to actually call the function you need ()

print(division())

1474412270.2748842

Link to comment
Share on other sites

Link to post
Share on other sites

No, you need to pass parameter arguments to your function(s) so that they can get the variables that they need. It is very important in program design that you carefully control access to resources, and that includes not using global variables unless absolutely necessary. Why? Because it is very easy to introduce bugs and side effects if a variable can be modified from anywhere in your program without warning.

 

 

While most of what they said is true, the reason you were getting that as output was because in the line

print(division) 

division is a pointer to a function, to actually call the function you need ()

print(division())

 

For example, on the code Glenwing said, i dont get how the n1 becomes num1 anyone mind explaining?

My PC:

Spoiler

MOBO: MSI B450 Tomahawk Max, CPU: AMD Ryzen 5 3600, Cooler: BeQuiet! Dark Rock 3, GPU: Gigabyte GTX 1050ti D5 4G, Ram: 16GB (2x8) HyperX Fury DDR4, Case: NZXT S340, Psu: Be Quiet! Pure Power 11 600W , HDD's: WD 1TB Caviar Blue, WD 256GB Scorpio Blue, WD 2TB Caviar Blue  SSD: Sandisk SSD PLUS 240Gb

Link to comment
Share on other sites

Link to post
Share on other sites

For example, on the code Glenwing said, i dont get how the n1 becomes num1 anyone mind explaining?

 

The value of n1 is passed to the function when the function is called, i.e. it is inside the parenthesis after division. Inside the function, this parameter is identified as num1 but it is the same value.

 

If you're using codeacademy you should probably actually do the tutorials so that you understand basic things like this.

Link to comment
Share on other sites

Link to post
Share on other sites

The value of n1 is passed to the function when the function is called, i.e. it is inside the parenthesis after division. Inside the function, this parameter is identified as num1 but it is the same value.

 

If you're using codeacademy you should probably actually do the tutorials so that you understand basic things like this.

I dont remember going on using 2 different names like that in codecademy :/ or i just dont remember.

 

So for example

 

def practice(p1, p2): on that the p1 and p2 will be used for only the def, if u want to have the numbers from a var such as p3 and p4 you can say print (practice(p3, p4)) where p3 == p1 and p4 == p2 in the def

My PC:

Spoiler

MOBO: MSI B450 Tomahawk Max, CPU: AMD Ryzen 5 3600, Cooler: BeQuiet! Dark Rock 3, GPU: Gigabyte GTX 1050ti D5 4G, Ram: 16GB (2x8) HyperX Fury DDR4, Case: NZXT S340, Psu: Be Quiet! Pure Power 11 600W , HDD's: WD 1TB Caviar Blue, WD 256GB Scorpio Blue, WD 2TB Caviar Blue  SSD: Sandisk SSD PLUS 240Gb

Link to comment
Share on other sites

Link to post
Share on other sites

I dont remember going on using 2 different names like that in codecademy :/ or i just dont remember.

 

So for example

 

def practice(p1, p2): on that the p1 and p2 will be used for only the def, if u want to have the numbers from a var such as p3 and p4 you can say print (practice(p3, p4)) where p3 == p1 and p4 == p2 in the def

 

Pretty much.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×