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!
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
mahesh gurbaxani
1,420 Pointshow 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.
import random
def random_num():
print("whats your number")
int(num)=input("> ")
random.randint(1,num)
1 Answer

mrben
8,068 PointsClose, 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
Treehouse Guest TeacherKenneth Love
Treehouse Guest TeacherThe problem comes, of course, when whatever is provided to
input()
can't be turned into anint()
.mahesh gurbaxani
1,420 Pointsmahesh gurbaxani
1,420 PointsThanks