0

The program asks:

would you like to repeat the execution? (Y/N)

If user enters "Y" or "N", the program starts again or exit, respectively.

If user enters something different from "Y" or "N", the program should print "I can't understand you, bye!"

I defined a function with the operations that I'm interested in. I call this function from a while loop. If user enters "Y" it calls the function, if user enters "N" it prints a message and exit, if user enters another character it prints the error message.

It works OK when I enter "Y" or "N", however it calls the function if I enter any other character such as "Z".

Below is my code. Thank you!

import random
import time

def Intro():
    
    print("some text here") 
    print("select a cavern (1 ó 2)")

def juego():
    dragon = random.randint(1,2)
    cueva = int(input())

    print("bla bla")
    time.sleep(2)
    print("bla bla bla")
    time.sleep(2)
    print("bla")
    time.sleep(2)
    
    if cueva == dragon:
        print("you win")
        print("start again? (Y/N)")
        print()
    else:
        print("you lose")
        print("start again? (Y/N)")
        print()
    
Intro()
juego()

seguir = input()
print()

while seguir == "Y" or "y":
    Intro()
    juego()
    seguir = input()
    print()
    if seguir == "N" or "n":
        print("Thanks for playing")
        break
    if seguir != "S" and seguir != "s" and seguir != "N" and seguir != "n":
        print("I can't understand you, bye!")
        break
Matt Oestreich
  • 8,219
  • 3
  • 16
  • 41
  • 5
    `seguir == "Y" or seguir == "y"` – Loocid Jan 05 '22 at 03:43
  • as the comment from @Loocid shows, watch for same logical error in `if seguir == "N" or "n":` – Jorge Gx Jan 05 '22 at 03:48
  • 2
    **Near-duplicate: [How to test multiple variables for equality against a single value?](https://stackoverflow.com/questions/15112125/how-to-test-multiple-variables-for-equality-against-a-single-value)**. You should this either with `seguir == 'Y' or seguir == 'y'` or `seguir in ['Y','y']` – smci Jan 05 '22 at 03:58

2 Answers2

0

Your while loop is only working when the user initially inputs a Y. If the user does not input the Y before the while loop, the check evalueates to false so the while loop never gets run. Dont get input before the while loop, set seguir to 'Y' at the begining so you can actually enter it.

ZXYNINE
  • 712
  • 4
  • 5
  • That's as designed. Before entering the loop, the input is from the question "start again? (Y/N)" so if it's not Y, the loop shouldn't be entered. – Nick Matteo Jan 05 '22 at 03:54
0

You should try like:

if seguir in ["N", "n"]: 
    print("Thanks for playing") 
    break

if seguir.lower() == "n": 
    print("Thanks for playing") 
    break

want to help~

ref: How to test multiple variables for equality against a single value?

EEEEH
  • 759
  • 1
  • 7
  • 28
  • Please cite [How to test multiple variables for equality against a single value?](https://stackoverflow.com/questions/15112125/how-to-test-multiple-variables-for-equality-against-a-single-value). It's 9 years old and has 765 upvotes. This issue has been asked on SO squillions of times. – smci Jan 05 '22 at 04:02
  • @smci Thanks for your kind reminder. It's my first instinct :) – EEEEH Jan 05 '22 at 04:06