-1
import sys    
def menu():

    print("Welcome to Celebrity Dogs")

    print("Write A to Start Game or B to Quit.")

    answer = input()
    if answer == ("A"): 
        print("Let's play!")
    elif answer == ("B"):  
        print("Bye then!")
        sys.exit()
    else:
        print("invalid answer, select A or B")
        menu()


def numberofcards():

    number=int(input("Enter an even number that is less than 30 and more than 4."))
    if number < 30 and number > 4 and number % 2 == 0:
        print("Ok, Here are the cards.")
    else:
        print("Invalid")
        numberofcards()

Hi. My code works perfectly fine, but when I input B to quit, it still asks me to Enter an even number that is less than 30 and more than 4. why? Thanks in advance x p.s. the def ... and the print is part of the code xx

rjv
  • 6,058
  • 5
  • 27
  • 49
  • 1
    Please change your question title in a way that points out the specific problem(s) you're faced with. Don't use descriptions in q-title. – Mazdak Oct 02 '18 at 18:49
  • Don't use recursion for this kind of iteration; just use a `while` loop. – chepner Oct 04 '18 at 19:14

2 Answers2

0

I would modify your code slightly to call the functions at the right places:

def numberofcards():

    number=int(input("Enter an even number that is less than 30 and more than 4."))
    if number < 30 and number > 4 and number % 2 == 0:
        print("Ok, Here are the cards.")
    else:
        print("Invalid")
        numberofcards()


def menu():

    print("Welcome to Celebrity Dogs")
    print("Write A to Start Game or B to Quit.")

    answer = input()
    if answer == ("A"): 
        print("Let's play!")
        numberofcards()
    elif answer == ("B"):  
        print("Bye then!")
    else:
        print("invalid answer, select A or B")


menu()
SmitM
  • 1,366
  • 1
  • 8
  • 14
  • Thankyou soooo much. You're the best! xx – Salma Yusuf Oct 02 '18 at 19:37
  • Accept it is an answer if it satisfactorily answered your question. Thanks! – SmitM Oct 02 '18 at 19:49
  • While this code may solve the question, [including an explanation](//meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please edit your answer to add explanation, and give an indication of what limitations and assumptions apply. – Makyen Mar 17 '19 at 16:04
0
def menu():

print("Welcome to Celebrity Dogs")

print("Write A to Start Game or B to Quit.")

answer = input()
if answer == ("A"): 
    print("Let's play!")
elif answer == ("B"):  
    print("Bye then!")
    quit()
else:
    print("invalid answer, select A or B")
    menu()

replace sys.exit() with quit()

Endy
  • 1
  • 3
  • That will not solve the problem. Besides, it's better to use `quit()` in the interactive interpreter only. See https://stackoverflow.com/questions/19747371/python-exit-commands-why-so-many-and-when-should-each-be-used – Valentino Mar 12 '19 at 14:01