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

Corey Chattin
Corey Chattin
777 Points

random.randint issues

The challenge wants me to create a function that takes and int argument then returns a random number between 1 and the number given. I suppose by using the random.randint(a, b) method since it had me import random.

So have I have this:

import random
def random_num(int):
  print("Type any number above 1")
  int = input("> ")

random_num(int)
random.randint(1, int)

I have no idea how to get the input from the user into the place where I have [int], also I suppose I'll need to print the results after this as well.

Any help would be greatly appreciated and please let me know if I am using the terms "method" and function correctly! :)

5 Answers

Hi Corey,

That's a pretty good attempt. For this challenge, we don't need to get any input from the user, and we don't need to call the function ourselves.

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

You're writing random.randint correctly, it just needs to be inside the function - and the value this generates needs to be returned.

Functions and methods are identical, except that methods live inside a class.

# example method
class Dog():
  def speak(self):
    return 'bark'

doggy = Dog()
doggy.speak()  # this is calling a method on the doggy object, which is an instance of the class Dog

# example function
def dogSpeak():
  return 'bark'

dogSpeak()  # this is a function, called by itself

Hope this helps,

Cheers

Corey Chattin
Corey Chattin
777 Points

Thank you Robert, you are the greatest! Super fast in your replies and super helpful too! Thank you so much, KUDOS to you sir!

I will tool around again and digest the information... I really want to fully understand. My thought process can be slow at first because I really want to know things in a very detailed way.

Regards,

Corey

Corey Chattin
Corey Chattin
777 Points

My ignorance of python syntax continues: (this is what happens with you return in the middle of a coarse a month later)

So here is the mess I am currently in:

import random
dev random_num(int):
  random.randint(1, 5)

That's a lot closer. There's a small typo to fix, change dev to def.

Basically, the purpose of this function is to return a random value from 1 to whatever gets passed in when the function is called - you chose to call it int, but you could name this anything and can be used as a variable local to the function.

Here is an example that should help you piece together how to solve the challenge.

def foo(n):
  return random.randint(1, n)
Corey Chattin
Corey Chattin
777 Points

I was tooling around a solved it with this:

import random
def random_num(int):
  return random.randint(1, int)

Your last post does help me grasp things a bit better. I have some many questions about even little things like what I'm working on here.

Thanks again!

Raymond Torres
Raymond Torres
2,222 Points

I typed what corey had word for word and still got it wrong.