Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Python Basics (Retired) Pick a Number! Any Number! Imports

again with poor questions~~~~ I am so mad!!!!!!!!

so how come my code at home works perfectly every time but i get random errors on here??

import random
passed_in = input(" ")
def random_num():
    randominter = random.randint (1, passed_in)
    return randominter
print random_num()
 # i have tried it with randint too and it works at home and not here!!) 

i enter the range i want and gives a random number (different number each time) between 1 and whatever range i tell it. if the name of variables are not good or they want a certain way of writing the code! why is it not indicated in the question?????

[MOD: added ```python formatting -cf]

6 Answers

Greg Kaleka
Greg Kaleka
39,021 Points

Hi Bruno,

Your code isn't doing what the question is asking for. You need to create a function that takes a parameter, and then spits out a random number between 1 and that parameter. It also doesn't say anything about calling the function yourself or printing the result. Here's my solution:

import random
def random_num(passed_in):
    randominter = random.randint (1, passed_in)
    return randominter
Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,423 Points

For the code challenges like the Import challenge, the input used for testing is provided by the grader. After you create the code for the random_num() function, when you select "Check Work", the grader will exercise your code and check the results against expected outcomes. The complete solution as Greg Kaleka has suggested, can be:

import random

def random_num(arg):
    return random.randint(1,arg)
Chris Freeman
Chris Freeman
Treehouse Moderator 68,423 Points

The other issue is the input statement is blocking the grader from executing. No input statement needed.

well this is working just fine at home:

import random passed_in = input(" ") def random_num(): randominter = random.randint (1, passed_in) return randominter print random_num()

the question doesn't clarify that. your method works too but no argument is passed in. where does variable passed_in comes from? from user "input()" then when you do that it gives a different error for that.

Greg Kaleka
Greg Kaleka
39,021 Points

passed_in is a parameter of the function. There's no need to use input().

Imagine, for example, you were coding a game, and at the end of each level, the player got a random points bonus between 1 and 1000.You would simply call the function in your code like this:

random_num(1000)

On the next level, they might get a bonus between 1 and 2000. You would call the function in your code like this:

random_num(2000)

Make sense?

oh i am so sorry, i think i didn't explain that this is a quiz on treehouse python track. i have no idea what number they pass or how they test the code. i am well aware of function methods and passing argument. for example : when you call the function random_num(100) it should work!

for example, if i changed the code according to what you said it would look like this:

import random

passed_in = int(raw_input(" "))

def random_num(passed_in): randominter = random.randint (1, passed_in) return randominter print random_num(100)

which works but not in treehouse! which is the problem!

Greg Kaleka
Greg Kaleka
39,021 Points

I guess the challenge was a bit ambiguous as to how they wanted you to pass the number in. If you're going to use raw_input, though, you should absolutely use it inside your function. Otherwise, your code works, but there could be variable scope issues down the line. Here's how I'd do it, if you want to use raw_input:

import random

def random_num():
    passed_in = int(raw_input(" "))
    randominter = random.randint (1, passed_in)
    return randominter

print random_num()

Note this would not pass the challenge. When you're taking the challenge, writing code that does something correctly is not the same as writing code that does what the challenge is asking for correctly. I agree sometimes the challenge wording is ambiguous, but it is what it is.

oh i am so sorry, i think i didn't explain that this is a quiz on treehouse python track. i have no idea what number they pass or how they test the code. i am well aware of function methods and passing argument. for example : when you call the function random_num(100) it should work!

for example, if i changed the code according to what you said it would look like this:

import random

passed_in = int(raw_input(" "))

def random_num(passed_in): randominter = random.randint (1, passed_in) return randominter print random_num(100)

which works but not in treehouse! which is the problem!

seems like you know your python well or perhaps very good! there are 100 ways to code for something. just you and me right now did like 5 different ways. but again this new method doesn't work in treehouse but works on my Pycharm just fine. one missing thing from the challenge is how passed_in is ...well, passed in??

Greg Kaleka
Greg Kaleka
39,021 Points

OK, so here's the original text of the challenge:

Now make a function named random_num that takes an int argument. Return a randint between 1 and the passed-in number.

For starters, they tell you to make a function that takes an int argument.

In programmer speak, when you call a function that takes arguments, you are calling the function and "passing it" something. So the phrase "the passed-in number" is referring to that int argument. It's not any special variable (as chris freeman illustrates below, you're welcome to call the variable anything you like).