Coder Cody
Coder Cody

Reputation: 120

How to Check for no input in Python

# Imports
import re

# Variables
contestants = []


def run(string):
    incorrectSymbols = re.compile('[@_!#$%^&*()<>?/\|}{~:]')
    if (incorrectSymbols.search(string) == None):
        return "yes"
    else:
        return "no"


def bootUp():
    print(""">>> Hello judges!
>>> Today you will be viewing 6 contestants!
>>> To continue please enter all contestants names!""")
    contestantNames()


def contestantNames():
    for i in range(0, 6):
        while True:
            x = input(">>> Contestant number " + str(i) + " name: ")
            if run(x) == "yes":
                break
            elif run(x) == "no":
                print(">>> No symbols allowed!")
            elif x=="":            # Here is what im confused about.
                print(">>> You can't leave it blank!")
            else:
                print(">>> Error in run function.")
    contestants.append(x)
    print(contestants)


bootUp()

After reading up on this problem I found multiple accounts saying to do this simply use (variable)="" . However, this does not work for me, any other ideas?

My code is an attempt for a dance project where contestants all take turns and each judge scores each contestant. A tournament to be put simply. This part of the code would be the beginning where I obtain the contestant's names and i need to make sure they have no symbols or left blank.

Upvotes: 2

Views: 79

Answers (1)

Poojan
Poojan

Reputation: 3519

  • Somehow it looks like your run is not working as you expected. Its returning 'yes' and 'no' not True and False. Ordering of your if else is wrong you need to fix that.
  • just move up if x=='' condition and it will work.
# Imports
import re

# Variables
contestants = []


def run(string):
    incorrectSymbols = re.compile('[@_!#$%^&*()<>?/\|}{~:]')
    if (incorrectSymbols.search(string) == None):
        return "yes"
    else:
        return "no"


def bootUp():
    print(""">>> Hello judges!
>>> Today you will be viewing 6 contestants!
>>> To continue please enter all contestants names!""")
    contestantNames()


def contestantNames():
    for i in range(0, 6):
        while True:
            x = input(">>> Contestant number " + str(i) + " name: ")
            if x == "":            # Here is what im confused about.
                print(">>> You can't leave it blank!")
            elif run(x) == "yes":
                break
            elif run(x) == "no":
                print(">>> No symbols allowed!")
            else:
                print(">>> Error in run function.")
    contestants.append(x)
    print(contestants)


bootUp()
  • Another solution is to change run function like this.
def run(string):
    if string == "":
        return
    incorrectSymbols = re.compile('[@_!#$%^&*()<>?/\|}{~:]')
    if (incorrectSymbols.search(string) == None):
        return "yes"
    else:
        return "no"

This will work with your default if else ordering.

Upvotes: 4

Related Questions