Hi! So today I started coding with Python, and I wanted to create this easy code, where you need to answer with: yes or no. It's not working, everytime, when I type "no" it always shows me "good answer!". How do I fix that?
fruits = input("do you like fruits?: ")
yes = input
no = input
if yes:
print("good answer!")
else:
print("bad answer!")
I don't really now what I am doing.
there are multiple issues with your code
- indentation is wrong, but probably it's just because of posting here (please, use BBcode tags
yes = input
and no = input
do NOT work as you expect.
you bind name yes
and also name no
to a built-in function. It looks like you want to bind specific values (strings) to these names
yes = 'yes'
no = 'no'
Please, note that this is NOT required.
- you want to check if
fruits
- i.e. what user provide as input is 'yes'
.
- it's good to use meaningful names.
fruits
is so so, e.g. answer
is better :-)
answer = input("do you like fruits?: ")
if answer == 'yes':
print("good answer!")
else:
print("bad answer!")
Note that any answer different from
'yes'
will produce
bad answer
, incl.
Yes
,
YES
,
y
, etc. You can handle this if you want