About Import and If | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

About Import and If

Just a question based out of pure curiosity, right now I am at the part of the course where I am learning about If (If x > 5 then a, for example) but how do I use the value from import for this function, what would be a line I could use?

20th Apr 2017, 12:47 PM
casleenen
3 Answers
+ 12
#you don't ... ? #well I mean, you can use functions from the module you imported. for example: import random answer = random.choice(["Red","Blue"]) print(answer) #here you import random, and use it's choice function to choose a random color, and assign that string to a variable. I hope this is the sort of thing you mean, since I don't think you can do: # a = import random #however I do think it's possible to do: importstring="import random" exec(importstring) #which interprets the string and runs it as code. however generally you're just going to want to do imports at the start of your code and not fiddle with assigning variables imports. sorry if I misunderstood, I have a headache and am tired .
20th Apr 2017, 1:12 PM
Ahri Fox
Ahri Fox - avatar
+ 3
what do you mean the value from import? x=6 if x>5: print("Yup") else: print("Nope")
20th Apr 2017, 1:05 PM
LordHill
LordHill - avatar
+ 3
def add(x,b): c=x+b return c add(5,4)
20th Apr 2017, 1:19 PM
LordHill
LordHill - avatar