+ 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"
7 Réponses
+ 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...'))
+ 5
hi Seb, that's really smart code. 👍
+ 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
0
What is the easiest programing language to start with ?
0
Of the three ,which is easier to learn first;
Java
Python
C
0
sande morris Python is typically the beginner friendliest programming language of those 3.