Python Forum
Problem with very easy code.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem with very easy code.
#1
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.
buran write Dec-10-2020, 12:49 PM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Reply
#2
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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Code problem - probably easy fix? colin_dent 5 891 Jun-30-2023, 01:55 PM
Last Post: deanhystad
  easy name problem Steinsack 1 1,751 Jun-16-2021, 02:03 PM
Last Post: snippsat
  What was my mistake in this Python code (easy)? voltman 4 3,452 Nov-19-2019, 09:58 PM
Last Post: snippsat
  How to start with this easy problem? Fran 8 4,170 Sep-11-2018, 09:04 AM
Last Post: Fran
  Making a Easy Password/code system nmsturcke 4 3,854 Jul-09-2018, 02:50 AM
Last Post: ichabod801
  probably a easy problem for you krheigh 4 4,677 May-12-2017, 06:45 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020