Home > Back-end >  Checking variable for invalid input is ignored
Checking variable for invalid input is ignored

Time:09-27

I am working on a project and I needed to write a function to ask what type of Minecraft server software the user would like to use. I then need to change if it is one of the options and if not, have the user reenter their selection. This is currently the code I am using to do so:

config_type_check = True
while config_type_check == True:
    if configuration_type == "vanilla" or "forge" or "spigot" or "bukkit" or "paper":
        config_type_check = False
    else:
        configuration_type = input("You have entered an invalid selection. Please enter a valid server installation type.")

I am using the config_type_check for the loop variable, and it is set to false so the code can continue as normal if the user input is valid. This does not work though, it lets any input through. Any ideas why? (Sorry if I wrote this messily, it is my first post to StackOverflow!

CodePudding user response:

your problem is wrong use of "or" operator.

config_type_check = True
configuration_type = input(" server installation type: ")
while config_type_check == True:
    if configuration_type == "vanilla" or configuration_type =="forge" or configuration_type =="spigot"  or configuration_type =="bukkit" or configuration_type =="paper":
        config_type_check = False
    else:
        configuration_type = input("You have entered an invalid selection. Please enter a valid server installation type.")

also you can use "in" operator:

config_type_check = True
configuration_type = input(" server installation type: ")
while config_type_check == True:
    if configuration_type in ("vanilla","forge","spigot","bukkit","paper"):
        config_type_check = False
    else:
        configuration_type = input("You have entered an invalid selection. Please enter a valid server installation type.")

have fun :)

  • Related