0

I'm trying to do so that when I insert my name it checks if it has numbers in it (success) and if the name is to long. And once I type the name again it checks it all again. it works with checking the numbers, but it doesn't work with checking if it's to long. Instead, it just continues the code.

print('Hi! Whats your name?')
def nome():
    global pontos
    def hasNumbers(nomezito):
        return any(char.isdigit() for char in nomezito)
        print(nome + 'has numbers')
    def longName(longevidade):
        return len(nome) < 3 or len(nome) > 13
    nome = input().title()
    number = hasNumbers(nome)
    long = longName(nome)
    while number == True or long == True:
        if number == True:
            print('A name cant have any numbers. Please tell me your real name')
            nome = input().title()
            number = hasNumbers(nome)
            continue
        elif long == True:
            print('Your name is too long to be real. Please tell me your real name.')
            print(longName(nome))
            nome = input().title()
            long = longName(nome)
            continue

P.S: I translated it from portuguese to english so you can read it better, but I might have made some mistakes. nome()

  • should the return statement in `nomeLongo` refer to the `longevidade` local variable? I think so. Also, this function returns `True` if the length is *less than 3* or *greater than 13*. That logic seems probably backwards. – David Zemens Jul 16 '19 at 13:12
  • 1
    Unrelated to your problem, but don’t write `x == True`. Just write `x`, it’s the same: `while number or long:`, `if number:`, etc. – Konrad Rudolph Jul 16 '19 at 13:13
  • Don't use the same name: `nome` both to your function, and a variable inside it – Tomerikoo Jul 16 '19 at 13:18
  • @DavidZemens you're right. it's also for names that are to short but I forgot to change it –  Jul 16 '19 at 13:23
  • thanks, @KonradRudolph –  Jul 16 '19 at 13:24
  • 2
    You might be interested in [Asking the user for input until they give a valid response](https://stackoverflow.com/q/23294658/953482), which gives a number of approaches to problems like this. – Kevin Jul 16 '19 at 13:24
  • ok, ill change it @Tomerikoo –  Jul 16 '19 at 13:25

3 Answers3

2

If the user enters a name with digits, the code enters the if number == True: block. Then when the user enters another name, you calculate number = hasNumbers(nome), but you don't run nameLongo again. So long is still referencing whether the previous name is too long. You need to call nameLongo in both branches, and hasNumbers in both branches.

print('Name:')
def nome():
    global pontos
    def temNumeros(nomezito):
        return any(char.isdigit() for char in nomezito)
        print(nome + 'tem numeros')
    def nomeLongo(longevidade):
        return len(nome) < 3 or len(nome) > 13
    nome = input().title()
    number = temNumeros(nome)
    long = nomeLongo(nome)
    while number == True or long == True:
        if number == True:
            print('digits.')
            nome = input().title()
            number = temNumeros(nome)
            long = nomeLongo(nome)
            continue
        elif long == True:
            print('Too long.')
            print(nomeLongo(nome))
            nome = input().title()
            number = temNumeros(nome)
            long = nomeLongo(nome)
            continue

nome()

Result:

Name:
123
digits.
ffffffffffffffffffffffffffffffffffffffffffffff
Too long.
True
kevin

... But ultimately I think it would be easier to call input exactly once in the loop, rather than in each branch of the conditional.

def contains_digits(name):
    return any(char.isdigit() for char in name)

def is_wrong_length(name):
    return len(name) < 3 or len(name) > 13

while True:
    name = input("Name: ")
    if contains_digits(name):
        print("Name can't contain digits.")
    elif is_wrong_length(name):
        print("Name must be between 3 and 13 characters.")
    else:
        #name is valid, so exit the loop
        break

print("Welcome,", name)

Result:

Name: 123
Name can't contain digits.
Name: ffffffffffffffffffffffffffffffffffffffffff
Name must be between 3 and 13 characters.
Name: Kevin
Welcome, Kevin
Kevin
  • 74,910
  • 12
  • 133
  • 166
  • I only saw the second code now. Please explain: In the if contains_digits(name) it calls the function with the same name? This one is way more clear –  Jul 16 '19 at 15:42
  • Yes, `if contains_digits(name):` calls the function named `contains_digits`. You are allowed to put function calls inside a conditional. More generally, any [expression](https://docs.python.org/3/reference/expressions.html) can go inside a conditional. – Kevin Jul 16 '19 at 15:55
0
def longName(longevidade):
    return len(longevidade) < 3 or len(longevidade) > 13

you were not putting 'longevidade' as the arguments of len()

EDIT : Here is your problem you were also not updating the value of number and long

print('Hi! Whats your name?')
def nome():
    global pontos
    def hasNumbers(nomezito):
        return any(char.isdigit() for char in nomezito)
        print(nome + 'has numbers')
    def longName(longevidade):
        return len(longevidade) < 3 or len(longevidade) > 13
    nome = input().title()
    number = hasNumbers(nome)
    long = longName(nome)
    while number == True or long == True:
        if number == True:
            print('A name cant have any numbers. Please tell me your real name')
            nome = input().title()
            number = hasNumbers(nome)
            long = longName(nome)
            continue
        elif long == True:
            print('Your name is too long to be real. Please tell me your real name.')
            print(longName(nome))
            nome = input().title()
            long = longName(nome)
            number = hasNumbers(nome)
            continue
nome()
Benoit F
  • 479
  • 2
  • 10
  • 2
    While I agree that it's not great design to ignore the `longevidade` argument and call `len` on `nome` instead, this doesn't fix the problem by itself. `longvidade` is always equal to `nome` when `longName` is called, so the logic should be the same. – Kevin Jul 16 '19 at 13:16
  • The second part worked. The first one didn't alter nothing. I don't know why, as I'm still learning and I always had a problem with functions hehe. Thanks! –  Jul 16 '19 at 13:29
  • the second part was the problem actually, my bad – Benoit F Jul 16 '19 at 13:31
0

If a while loop didn't play again that means the statement you given for it isn't correct

Your statement is

while number == True or long == True:

so it only ran once because the second run after the loop all of your statements

number == True 

and this statement

long == True

is not working . What I suggest is you run each statement separately and see which one is working this way you can solve the issue

or create function for each like

def number():
    if number == True:
        print('A name cant have any numbers. Please tell me your real name')
        nome = input().title()
        number = hasNumbers(nome)
    else:
       pass

def long():
    if long == True:
        print('Your name is too long to be real. Please tell me your real name.')
        print(longName(nome))
        nome = input().title()
        long = longName(nome)
    else:
        pass

while True:
    number()
    long()
OfficialAhmed
  • 41
  • 1
  • 8
  • 1
    Calling `number()` and `long()` in a `while True:` doesn't seem like a very good design, because the loop will never end, even if the user enters a valid name. – Kevin Jul 16 '19 at 13:27