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.
#1
Could you please help me a little?
Game is not written by me obviously.
Mine is an attempt to introduce a question with two outcomes.
If "yes" we play, if "no" end of the game. So far "yes" worked. Option with "no" does not work.
Where do I insert it?
Thank you very much in advance.

I do not get this logic yet.
Tried elif and else in different places but it did not work.



play_more = input('Lets play this game,shall we? (Answer yes or no) ')
if play_more == 'yes':
    print('Great! Lets guess a number!')
    import random
n = random.randint(1, 99)
guess = int(input("Enter an integer from 1 to 99: "))
while n != "guess":
    print
    if guess < n:
        print ("guess is low")
        guess = int(input("Enter an integer from 1 to 99: "))
    elif guess > n:
        print ("guess is high")
        guess = int(input("Enter an integer from 1 to 99: "))
    else:
        print ("you guessed it!")
        break

print('')

print ('Game is over')

#The question is: how and where do I insert option for
#else print something and end program in case of answer is "no"?
Reply
#2
Two tips:
Put all of your import statements at the top of the program

Do one thing at a time and test it, then add one thing to it and test it.
Start by making a loop that asks if you want to play and prints "playing" until you reply no. Then test it. Then add other features, one at a time.

It looks like you wrote this whole thing at once and have no idea why it's not working the way that you want.
Reply
#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
#4
(Dec-05-2019, 09:51 PM)Manjushri Wrote: while n != "guess":
This okay,but can not have quotes around "guess" then is just a string and always True.
Should put this line inside the loop,then set a guess variable to 0 before the loop:
guess = int(input("Enter an integer from 1 to 99: "))
Okay easier to see if i put this together i leave out yes/no to play the game.
See that there is no break out.
!=(not equal) will break out the loop when equal.
import random

n = random.randint(1, 99)
tries, guess = 0, 0
while n != guess:
    guess = int(input("Enter an integer from 1 to 99: "))
    if guess > n:
        print("guess lower")
    elif guess < n:
        print("guess higher")
    tries += 1

print(f'You guessed it! The number was {n} in {guess} tries')
Test.
Output:
E:\div_code λ python guess_n.py Enter an integer from 1 to 99: 50 guess lower Enter an integer from 1 to 99: 25 guess higher Enter an integer from 1 to 99: 35 guess higher Enter an integer from 1 to 99: 40 guess higher Enter an integer from 1 to 99: 45 guess higher Enter an integer from 1 to 99: 47 guess lower Enter an integer from 1 to 99: 46 You guessed it! The number was 46 in 7 tries
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Redirection giving faulty results monstrup 5 2,492 Jul-19-2020, 07:11 AM
Last Post: Gribouillis
  Help with faulty code. Wilson1218 1 2,874 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