+ 2

How to make this code run my way?

There are two strings a and b that mean smart and dumb Code: a = "smart" b = "dumb" print("Lions" + " are " + input()) When I enter a or b in the pop box it doesn't show smart or dumb. For ex: It only shows "Lions are a" or "Lions are b"

23rd Jun 2019, 5:08 PM
Vin Raghav
7 odpowiedzi
+ 8
It will take your input as a string and have no idea that if you enter "a", you're referring to the variable a. So it'll just print your input as a string. Theoretically, you could use the eval() function to evaluate your input: print("Lions are " + eval(input())) However, this is very dangerous and shouldn't be done. The safest way is to take input as a string and check what was entered: user_input = input() if user_input == 'a': (...) elif user_input == 'b' (...) You could also use a dictionary: d = { 'a': 'smart', 'b': 'dumb', } print("Lions are " + d.get(input(), 'meh...'))
23rd Jun 2019, 5:21 PM
Anna
Anna - avatar
+ 5
hi Seb, that's really smart code. 👍
23rd Jun 2019, 7:48 PM
Lothar
Lothar - avatar
+ 2
Using eval works, some developers don't recommend using eval within input statements, but here is another method: #Defining function: def a_or_b(a, b, put): if put == "a": return a elif put == "b": return b else: return "not smart, but not dumb" print("Lions" + " are " + a_or_b(a, b, input())) #if input == "a": Lions are smart #elif input == "b": Lions are dumb #else: Lions are not smart, but not dumb
23rd Jun 2019, 5:47 PM
Seb TheS
Seb TheS - avatar
+ 2
Lothar I know Python good enough for that to be nothing. But thanks.
23rd Jun 2019, 7:49 PM
Seb TheS
Seb TheS - avatar
0
What is the easiest programing language to start with ?
10th Aug 2019, 10:20 PM
sande morris
0
Of the three ,which is easier to learn first; Java Python C
10th Aug 2019, 10:22 PM
sande morris
0
sande morris Python is typically the beginner friendliest programming language of those 3.
11th Aug 2019, 7:26 AM
Seb TheS
Seb TheS - avatar