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

mahesh gurbaxani
mahesh gurbaxani
1,420 Points

how do i assign a number to a variable by using "input" from the screen

suppose i wrote the following code:

def accept_number(): print("whats your number?") int(num)=input(" ")

With the above code,will the number that I enter be saved as the variable "num" when I run the application? If not, what code should I write.

random_num.py
import random
def random_num():
 print("whats your number")
 int(num)=input("> ")
random.randint(1,num)

1 Answer

Close, just a couple of small changes needed.

import random

def random_num():
  print("whats your number")
  # You need to "int()" the return from input.
  num = int(input("> "))
  # The random number needs to be returned form the function.
  return random.randint(1, num)
Kenneth Love
Kenneth Love
Treehouse Guest Teacher

The problem comes, of course, when whatever is provided to input() can't be turned into an int().