Python Forum
My first program is a bit faulty. Please Help.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
My first program is a bit faulty. Please Help.
#3
I suggest to think more thoroughly about logic of your program.

For example: how should code handle situation when user enters neither 'yes' or 'no'?

My suggestion is to refactor code:

- input validation function - keeps asking for input until either yes or no is entered by user; as this is needed only for deciding what to do next, it should return True or False
- two branch conditional - play game; quit

if validated_answer('Lets play this game,shall we? (Answer yes or no) '):
    # play game
else:
    # quit game
Regarding validation function:

- keep asking question (while True):
- make answer lowercase so that comparison is easier
- if answer is 'yes' return True; if 'no' return False (exits function)
- else print message to user (while loop continues)

Something along those lines:

def validated_answer(request): 
    while True: 
        answer = input(request).lower() 
        if answer == 'yes': 
            return True 
        elif answer == 'no': 
            return False 
        else: 
            print('Answer must be yes or no! ') 
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Messages In This Thread
RE: My first program is a bit faulty. Please Help. - by perfringo - Dec-06-2019, 12:21 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Redirection giving faulty results monstrup 5 2,676 Jul-19-2020, 07:11 AM
Last Post: Gribouillis
  Help with faulty code. Wilson1218 1 2,948 Jun-14-2017, 09:15 AM
Last Post: wavic

Forum Jump:

User Panel Messages

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