0

I have created code that will do a condition validation statement. I have a menu that prompts the user the enter a number between 1-3 and if the user presses any number besides those 3 numbers, it would tell the user an error message and loop the menu statement again until the condition is met. This is my code

from flask import Flask, request, jsonify, make_response, abort
import mysql.connector
from mysql.connector import Error
import myfunctions
import datetime
from myfunctions import create_connection, execute_query, execute_read_query, connection
from friend_insert import friend_insert
from movie_insert import movie_insert
from friend_delete import friend_delete
from random_movie import random_movie

while True:
    print("Press 1 To Add Friend Information")
    print("Press 2 To Add Up To 10 Movies Per Friend")
    print('Press 3 To Select Who Participates And Generate A Random Movie')
    choice = int(input())


    if choice == 1:
        friend_insert()
    if choice == 2:
        movie_insert()
    if choice == 3:
        friend_delete()
        random_movie()
        break
    
if choice != 1 and 2 and 3:
    print("Please Try Again")

The loop works. however, it is not showing the error message "Please Try Again." Why is that?

Machavity
  • 30,841
  • 27
  • 92
  • 100
kielp2
  • 11
  • 3
  • 1
    It's not showing because it's outside the loop. – Have a nice day Apr 08 '21 at 18:08
  • when you fix that, you will notice that it will print the message even when choice = 2 or 3. – Nicholas Hunter Apr 08 '21 at 18:09
  • @NicholasHunter That's because the last if-statement is wrongly formatted. See my answer for a simplification. – M-Chen-3 Apr 08 '21 at 18:11
  • Related: [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – wwii Apr 08 '21 at 18:26
  • Welcome to SO. Please read [mre]. then look at your example - is there anything in your example that is not relevant to your question? or needed by us to reproduce the problem? – wwii Apr 08 '21 at 18:28

1 Answers1

2

The reason it doesn't work is that the last if-statement isn't in your while loop! Also, I'd suggest changing your if-statement format a little. Below is your corrected while loop:

while True:
    print("Press 1 To Add Friend Information")
    print("Press 2 To Add Up To 10 Movies Per Friend")
    print('Press 3 To Select Who Participates And Generate A Random Movie')
    choice = int(input())


    if choice == 1:
        friend_insert()
    elif choice == 2:
        movie_insert()
    elif choice == 3:
        friend_delete()
        random_movie()
        break
    else:
        print("Please Try Again")

This gets rid of the wrongly formatted if choice != 1 and 2 and 3. If you did want to do something like that, you'd have to do if (choice != 1 and choice != 2) and choice != 3; an else-statement is far more elegant, however.

M-Chen-3
  • 2,036
  • 5
  • 13
  • 34
  • Okay It works! Now I have another problem. I created a flask app that would run after that menu is executed but after the app runs, it would loop back to the menu – kielp2 Apr 08 '21 at 18:41
  • @kielp2 That is outside the scope of this question. I would suggest asking another one. Since my answer helped you, please consider accepting it! – M-Chen-3 Apr 08 '21 at 21:04