简体   繁体   中英

Changing loop conditions to repurpose a number guessing game

I am a new programmer with experience with Visual Basic for Applications and have recently changed to python.

I'm working on a number guessing game and so far progress has been great. The user enters 4 digits into the program. The program also generates a 4 digit number and the program returns Ys or Ns to show whether any digits are correct or not. EG 1357 as a user guess and 1358 as the programs number shows YYYN as output.

I'm trying to rework the program to make it simpler for users by showing H or L to suggest that they need to guess higher or lower IF the digit guessed is incorrect. If it's correct, then a Y should be shown as per normal. I am aware that it's a condition in the loop I need to change or another loop I need to add but I am struggling to see where to add this and how to write it. Does anybody have a solution for this?

Here is part of my code for the section of the program which returns the result for the guesses.

lives = 10

while lives > 0:
    number = input("Enter your guess: ")

    letter = ''
    for i in range(len(numToGuess)):
        letter += 'Y' if int(number[i]) == numToGuess[i] else 'N'


    if letter == 'Y' * len(numToGuess):
        print("Good job!")
        break

    print(letter)

    lives -= 1
    else:
        print("Game over! You used up all of your tries.")

Does anybody have a solution for this?

I prefer to use lists for this. Meaning, I'd convert both the correct answer and the user guess into separate digits saves in two lists and then compare them.

Let's say the correct answer is '1234':

lives = 10

correct_answer = 1234

correct_answer = [int(char) for char in str(correct_answer)]


while lives > 0:
    letter = ''

    number = input("Enter your guess: ")
    number = [int(char) for char in str(number)]

    if number == correct_answer:
        print("Correct!")
        break

    for i in range(len(correct_answer)):
        if correct_answer[i] == number[i]:
            letter += 'Y'
        elif correct_answer[i] > number[i]:
                letter += 'H'
        else:
            letter += 'L'
    print("Your guess is wrong: ", letter)

    lives -= 1


print("Game over!")

Now for example:

Enter your guess: 1111
Your guess is wrong:  YHHH
Enter your guess: 1235
Your guess is wrong:  YYYL
Enter your guess: 1234
Correct!
Game over!
>>> 

You can use zip function for compare the letters :

>>> a='1357'
>>> b='1358'
>>> l=[]
>>> for i,j in zip(a,b):
...   if i==j :l.append('Y')
...   else :l.append('N')
... 
>>> ''.join(l)
'YYYN'

And for check the answer you can use a generator expression within all :

>>> all(i=='Y' for i in l)
False

You don't need to change your loop condition. Just change the logic of your if expression.

letter = ''
for i in range(len(numToGuess)):
    if int(number[i]) == numToGuess[i]:
        letter += 'Y'
    elif int(number[i]) > numToGuess[i]:
        letter += 'H'
    else:
        letter += 'L'

Or, in one line:

letter = ''
for i in range(len(numToGuess)):
    letter += 'Y' if int(number[i]) == numToGuess[i] else ('H' if int(number[i]) > numToGuess[i] else 'L')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM